Copying a vector, copy the contents of one vector into another. - C++ STL

C++ examples for STL:vector

Description

Copying a vector, copy the contents of one vector into another.

Demo Code

#include <iostream>
#include <vector>
#include <string>
#include <algorithm>

using namespace std;

// Util function for printing vector contents
template<typename T>
void vecPrint (const vector<T>& vec) {
   cout << "{";
   for (typename vector<T>::const_iterator p = vec.begin(); p != vec.end(); ++p) {
      cout << "{" << *p << "} ";
   }/*  w ww .ja v  a2 s.  c o  m*/
   cout << "}" << endl;
}

int main() {

   vector<string> vec(5);
   string foo[] = {"test", "hi", "that", "hello", "about"};

   vec[0] = "Today";
   vec[1] = "is";
   vec[2] = "a";
   vec[3] = "new";
   vec[4] = "test";

   vector<string> vec2(vec);
   vecPrint(vec2);

   vec.at(0) = "Tomorrow";

   vec2.assign(vec.begin(), vec.end()); // Copy each element over
   vecPrint(vec2);                      // with assign

   vec2.assign(&foo[0], &foo[5]); // Assign works for anything that
   vecPrint(vec2);                // behaves like an iterator


   vector<string>::iterator p;

   p = find(vec.begin(), vec.end(), "new");

   vec2.assign(vec.begin(), p); // Copy a subset of the full range of vec
   vecPrint(vec2);
}

Result


Related Tutorials