Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <iostream>
- #include <vector> // подключаем модель Векторов
- using namespace std;
- void print_vector( vector<int> ivec ) {
- if ( ivec.empty() )
- return;
- for ( int ix=0; ix< ivec.size(); ++ix )
- cout << ivec[ ix ] << ' ';
- }
- void vec_copy(){
- vector<int> myVector1(10);
- // вывод элементов вектора на экран
- cout << "Входной массив: ";
- for(int i = 0; i < myVector1.size(); i++) {
- myVector1[i] = i;
- }
- print_vector(myVector1);
- cout << "\nСкопированный массив: ";
- vector<int> myVector2(myVector1); // при объявлении второго вектора, копируется - первый
- print_vector(myVector2);
- }
- void vec_equals(){
- vector<int> vec1(3);
- // инициализируем элементы вектора vec1
- vec1[0] = 4;
- vec1[1] = 2;
- vec1[2] = 1;
- vector<int> vec2(3);
- // инициализируем элементы вектора vec2
- vec2[0] = 4;
- vec2[1] = 2;
- vec2[2] = 1;
- // сравниваем массивы
- if (vec1 == vec2) {
- cout << "vec1 == vec2" << endl;
- }
- }
- void vec_methods(){ //динамическое заполнение массива
- vector<int> vec; // create an empty vector
- vec.reserve(3); // make room for 3 elements
- // at this point, capacity() is 3
- // and size() is 0
- cout<<"size: "<<vec.size()<<"; capacity: "<<vec.capacity()<<endl;
- vec.push_back(999); // append an element
- vec.resize(5); // resize the vector
- // at this point, the vector contains
- // 999, 0, 0, 0, 0
- print_vector(vec); cout<<endl;
- vec.push_back(333); // append another element into the vector
- // at this point, the vector contains
- // 999, 0, 0, 0, 0, 333
- print_vector(vec); cout<<endl;
- vec.reserve(1); // will do nothing, as capacity() > 1
- vec.resize(3); // at this point, the vector contains
- // 999, 0, 0
- // capacity() remains 6
- // size() is 3
- print_vector(vec); cout<<endl;
- cout<<"size: "<<vec.size()<<"; capacity: "<<vec.capacity()<<endl;
- vec.resize(6, 1); // resize again, fill up with ones
- // at this point the vector contains
- // 999, 0, 0, 1, 1, 1
- print_vector(vec); cout<<endl;
- }
- int main(){
- setlocale(LC_ALL,"Russian");
- vec_copy();
- cout<<endl<<"==="<<endl;
- vec_equals();
- cout<<endl<<"==="<<endl;
- vec_methods();
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement