-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy path54.cpp
45 lines (39 loc) · 1.02 KB
/
54.cpp
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
42
43
44
45
#include<iostream>
#include<vector>
#include<algorithm>
int main()
{
//program to understand the copy() function
std::vector<int> v = {23,45,12,56,78,90};
std::vector<int> v2(4);
//printing our vector
std::cout<<"Vector : ";
std::vector<int>::iterator p = v.begin();
while(p!=v.end())
{
std::cout<<*p<<" ";
p++;
}
std::cout<<std::endl;
//copy function
//for that creating another vector in which we will add a copy of element
std::copy(v.begin(),v.begin()+3,v2.begin());
std::cout<<"Copied three elements of vector\n";
//printing the copied vector
std::cout<<"Vector 2 : ";
for(int i=0;i<v2.size();i++)
{
std::cout<<v2[i]<<" ";
}
std::cout<<std::endl;
std::vector<int> v3(5);
//copying using the copy_n() function
std::copy_n(v.begin(),5,v3.begin());
std::cout<<"Vector 2 (using copy_n) : ";
for(int i=0;i<v3.size();i++)
{
std::cout<<v3[i]<<" ";
}
std::cout<<std::endl;
return 0;
}