daily pastebin goal
22%
SHARE
TWEET

Untitled

a guest May 27th, 2018 56 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. vec2.reserve( vec1.size() );
  2. copy(vec1.begin(), vec1.end(), vec2.begin());
  3.    
  4. /**
  5.  * vectest.cpp
  6.  */
  7.  
  8. #include <iostream>
  9. #include <vector>
  10.  
  11. using namespace std;
  12.  
  13. int main()
  14. {
  15.     vector<int> vec1;
  16.     vector<int> vec2;
  17.  
  18.     vec1.push_back(1);
  19.     vec1.push_back(2);
  20.     vec1.push_back(3);
  21.     vec1.push_back(4);
  22.     vec1.push_back(5);
  23.     vec1.push_back(6);
  24.     vec1.push_back(7);
  25.  
  26.     vec2.reserve( vec1.size() );
  27.     copy(vec1.begin(), vec1.end(), vec2.begin());
  28.  
  29.     cout << "vec1.size()     = " << vec1.size() << endl;
  30.     cout << "vec1.capacity() = " << vec1.capacity() << endl;
  31.  
  32.     cout << "vec1: ";
  33.     for( vector<int>::const_iterator iter = vec1.begin(); iter < vec1.end(); ++iter ) {
  34.         cout << *iter << " ";
  35.     }
  36.     cout << endl;
  37.  
  38.     cout << "vec2.size()     = " << vec2.size() << endl;
  39.     cout << "vec2.capacity() = " << vec2.capacity() << endl;
  40.     cout << "vec2: ";
  41.     for( vector<int>::const_iterator iter = vec2.begin(); iter < vec2.end(); ++iter ) {
  42.         cout << *iter << endl;
  43.     }
  44.  
  45.     cout << endl;
  46. }
  47.    
  48. vec1.size()     = 7
  49. vec1.capacity() = 8
  50. vec1: 1 2 3 4 5 6 7
  51. vec2.size()     = 0
  52. vec2.capacity() = 7
  53. vec2:
  54.    
  55. vec2.reserve( vec1.size() );
  56. copy(vec1.begin(), vec1.end(), std::back_inserter(vec2.begin()));
  57.    
  58. vec2(vec1);
  59. vec2 = vec1;
  60.    
  61. vec2(vec1.begin(), vec2.end()); // range-based constructor
  62.  
  63. vec2.assign(vec1.begin(), vec1.end()); // range-based assignment
  64.    
  65. copy(vec1.begin(), vec1.end(), back_inserter(vec2));
  66.    
  67. vec2.resize(vec1.size(), '');
  68. copy(vec1.begin(), vec1.end(), vec2.begin());
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy. OK, I Understand
 
Top