Skip to content

vector_pro::assign

C++
// range
void assign(iterator_pro<value_type> from, iterator_pro<value_type> exclude_to);
// fill
void assign(size_type num, const value_type& val);
// initializer list
void assign(std::initializer_list<value_type> another);

Assign vector content

Assigns new contents to the container, replacing its current contents, and modifying its size accordingly.

  1. range version
    the new contents are elements constructed from each of the elements in the range [from, exclude_to) , in the same order.

  2. fill version
    the new contents are num elements, each initialized to a copy of val.

  3. initializer list version
    the new contents are copies of the values passed as initializer list, in the same order.

Parameters

  1. from, exclude_to
    Input iterators to the initial and final positions in a sequence. The range used is [from, exclude_to), which includes all the elements between from and exclude_to , including the element pointed by from but not the element pointed by exclude_to.
  2. num
    New size for the container.
  3. val
    Value to fill the container with.
  4. another
    An std::inializer_list object of the same type.

Example

C++
// vector_pro assign
#include <iostream>
#include "vector_pro.h"

/**
 * Output:
 * Size of first: 7
 * Size of second: 5
 * Size of third: 3
 */

int main ()
{
  vector_pro<int> first;
  vector_pro<int> second;
  vector_pro<int> third;

  first.assign (7,100);             // 7 ints with a value of 100

  iterator_pro<int> it;
  it=first.begin()+1;

  second.assign (it,first.end()-1); // the 5 central values of first

  third.assign ({1, 2, 3});   // assigning from array.

  std::cout << "Size of first: " << int (first.size()) << '\n';
  std::cout << "Size of second: " << int (second.size()) << '\n';
  std::cout << "Size of third: " << int (third.size()) << '\n';
  return 0;
}

Complexity

Linear on initial and final sizes (destructions, constructions).