forked from sm5689/cppCodes
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcopy of vector.cpp
More file actions
41 lines (33 loc) · 1 KB
/
Copy pathcopy of vector.cpp
File metadata and controls
41 lines (33 loc) · 1 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
// C++ code to demonstrate copy of vector
// by iterative method.
#include<iostream>
#include<vector>
using namespace std;
int main()
{
// Initializing vector with values
vector<int> vect1{1, 2, 3, 4};
// Declaring new vector
vector<int> vect2;
// A loop to copy elements of
// old vector into new vector
// by Iterative method
for (int i=0; i<vect1.size(); i++)
vect2.push_back(vect1[i]);
cout << "Old vector elements are : ";
for (int i=0; i<vect1.size(); i++)
cout << vect1[i] << " ";
cout << endl;
cout << "New vector elements are : ";
for (int i=0; i<vect2.size(); i++)
cout << vect2[i] << " ";
cout<< endl;
// Changing value of vector to show that a new
// copy is created.
vect1[0] = 2;
cout << "The first element of old vector is :";
cout << vect1[0] << endl;
cout << "The first element of new vector is :";
cout << vect2[0] <<endl;
return 0;
}