Advertisement
karbaev

stl-vector2

Mar 4th, 2016
78
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 2.79 KB | None | 0 0
  1. #include <iostream>
  2. #include <vector> // подключаем модель Векторов
  3. using namespace std;
  4.  
  5. void print_vector( vector<int> ivec ) {
  6.    if ( ivec.empty() )
  7.      return;
  8.    for ( int ix=0; ix< ivec.size(); ++ix )
  9.    cout << ivec[ ix ] << ' ';
  10. }
  11.  
  12. void vec_copy(){
  13.  
  14.     vector<int> myVector1(10);
  15.     // вывод элементов вектора на экран
  16.     cout << "Входной массив: ";
  17.     for(int i = 0; i < myVector1.size(); i++) {
  18.         myVector1[i] = i;
  19.     }
  20.     print_vector(myVector1);
  21.     cout << "\nСкопированный массив: ";
  22.     vector<int> myVector2(myVector1); // при объявлении второго вектора, копируется - первый
  23.     print_vector(myVector2);
  24. }
  25.  
  26. void vec_equals(){
  27.     vector<int> vec1(3);
  28.     // инициализируем элементы вектора vec1
  29.     vec1[0] = 4;
  30.     vec1[1] = 2;
  31.     vec1[2] = 1;
  32.     vector<int> vec2(3);
  33.     // инициализируем элементы вектора vec2
  34.     vec2[0] = 4;
  35.     vec2[1] = 2;
  36.     vec2[2] = 1;
  37.     // сравниваем массивы
  38.     if (vec1 == vec2) {
  39.         cout << "vec1 == vec2" << endl;
  40.     }
  41. }
  42.  
  43. void vec_methods(){ //динамическое заполнение массива
  44.     vector<int> vec;   // create an empty vector
  45.     vec.reserve(3);         // make room for 3 elements
  46.                           // at this point, capacity() is 3
  47.                           // and size() is 0
  48.  
  49.     cout<<"size: "<<vec.size()<<"; capacity: "<<vec.capacity()<<endl;
  50.     vec.push_back(999);     // append an element
  51.     vec.resize(5);          // resize the vector
  52.                           // at this point, the vector contains
  53.                           // 999, 0, 0, 0, 0
  54.     print_vector(vec); cout<<endl;
  55.     vec.push_back(333);     // append another element into the vector
  56.                           // at this point, the vector contains
  57.                           // 999, 0, 0, 0, 0, 333
  58.     print_vector(vec);  cout<<endl;
  59.     vec.reserve(1);         // will do nothing, as capacity() > 1
  60.     vec.resize(3);          // at this point, the vector contains
  61.                           // 999, 0, 0
  62.                           // capacity() remains 6
  63.                           // size() is 3
  64.     print_vector(vec); cout<<endl;
  65.     cout<<"size: "<<vec.size()<<"; capacity: "<<vec.capacity()<<endl;
  66.     vec.resize(6, 1);       // resize again, fill up with ones
  67.                           // at this point the vector contains
  68.                           // 999, 0, 0, 1, 1, 1
  69.     print_vector(vec); cout<<endl;
  70. }
  71.  
  72. int main(){
  73.     setlocale(LC_ALL,"Russian");
  74.     vec_copy();
  75.     cout<<endl<<"==="<<endl;
  76.     vec_equals();
  77.     cout<<endl<<"==="<<endl;
  78.     vec_methods();
  79.     return 0;
  80. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement