Answers for "c++ vector assign"

C++
0

cplusplus vector assign

// vector assign
#include <iostream>
#include <vector>

int main ()
{
  std::vector<int> first;
  std::vector<int> second;
  std::vector<int> third;

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

  std::vector<int>::iterator it;
  it=first.begin()+1;

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

  int myints[] = {1776,7,4};
  third.assign (myints,myints+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;
}
Posted by: Guest on November-25-2020
0

vector assign

#include <iostream>
#include <vector>

using namespace std;

int main()
{
    int arr[] = {1, 2, 3, 4, 7};
    vector<int> v1;
    vector<int> v2;
    v1.assign(arr, arr+5);
    v2.assign(arr, arr+2);

    //printing values of v1
    cout << "elements of v1" << endl;
    for(int i = 0; i < v1.size(); i++)
    {
        cout << v1[i] << endl;
    }

    //printing values of v2
    cout << "elements of v2" << endl;
    for(int i = 0; i < v2.size(); i++)
    {
        cout << v2[i] << endl;
    }
    return 0;
}
Posted by: Guest on June-27-2021

Browse Popular Code Answers by Language