C++ में वेक्टर को कॉपी करने के विभिन्न तरीके हैं।
1) एसटीडी::कॉपी
एक वेक्टर से दूसरे वेक्टर में तत्वों को कॉपी करने के लिए std::copy इनबिल्ट है।
सिंटैक्स
std::copy(first_iterator_o, last_iterator_o, back_inserter()): first_iteratot_0 = First iterator of first vector. last_iteratot_0 = Last iterator of first vector. back_inserter() = To insert values from back.
एल्गोरिदम
Begin Declare v1 of vector type. Initialize some values into v1 vector in array pattern. Declare v2 of vector type. Call copy(v1.begin(), v1.end(), back_inserter(v2)) to copy all elements of v1 to v2. Print “v1 vector elements are :”. for (int i=0;i<1.size; i++) print the all element of v2 vector. Print “v2 vector elements are :”. for (int i=0;i<2.size; i++) print the all element of v2 vector. End.
उदाहरण कोड
#include<iostream> #include<vector> #include<algorithm> // for copy. #include<iterator> // for back_inserter using namespace std; int main() { vector<int> v1{ 7, 6, 4, 5 }; vector<int> v2; copy(v1.begin(), v1.end(), back_inserter(v2)); cout << "v1 vector elements are : "; for (int i=0; i<v1.size(); i++) cout << v1[i] << " "; cout << endl; cout << "v2 vector elements are : "; for (int i=0; i<v2.size(); i++) cout << v2[i] << " "; cout<< endl; return 0; }
आउटपुट
v1 vector elements are : 7 6 4 5 v2 vector elements are : 7 6 4 5
2) ऑपरेटर असाइन करें
इसका उपयोग वेक्टर 1 से वेक्टर 2 में मानों की प्रतिलिपि बनाने के लिए भी किया जाता है।
सिंटैक्स
std::assign(first_iterator_o, last_iterator_o): first_iteratot_0 = First iterator of first vector. last_iteratot_0 = Last iterator of first vector.
एल्गोरिदम
Begin Initialize a vector v1 with its elements. Declare another vector v2. Call assign() to copy the elements of v1 to v2. Print the elements of v1. Print the elements of v2. End.
उदाहरण कोड
#include<iostream> #include<vector> // for vector #include<iostream> #include<vector>// for vector #include<algorithm>// for copy() and assign() #include<iterator>// for back_inserter using namespace std; int main() { vector<int> v1{7,6,4,5}; vector<int> v2; v2.assign(v1.begin(), v1.end()); cout << "v1 vector elements are : "; for (int i=0; i<v1.size(); i++) cout << v1[i] << " "; cout << endl; cout << "v2 vector elements are : "; for (int i=0; i<v2.size(); i++) cout << v2[i] << " "; cout<< endl; return 0; }
आउटपुट
v1 vector elements are : 7 6 4 5 v2 vector elements are : 7 6 4 5
3) असाइनमेंट "=" ऑपरेटर द्वारा
वेक्टर 1 से वेक्टर 2 में मानों की प्रतिलिपि बनाने का यह एक आसान तरीका है
एल्गोरिदम
Begin Initialize a vector v1 with its elements. Declare another vector v2. Call assignment operator “=” to copy the elements of v1 to v2. Print the elements of v1. Print the elements of v2. End.
उदाहरण कोड
#include<iostream> #include<vector> // for vector #include<algorithm>// for copy() and assign() #include<iterator>// for back_inserter using namespace std; int main() { vector<int> v1{7,6,4,5}; vector<int> v2; v2 = v1 ; cout << "v1 vector elements are : "; for (int i=0; i<v1.size(); i++) cout << v1[i] << " "; cout << endl; cout << "v2 vector elements are : "; for (int i=0; i<v2.size(); i++) cout << v2[i] << " "; cout<< endl; return 0; }
आउटपुट
v1 vector elements are : 7 6 4 5 v2 vector elements are : 7 6 4 5
4) push_back विधि द्वारा
एल्गोरिदम
Begin Initialize a vector v1 with its elements. Declare another vector v2. Make a for loop to copy elements of first vector into second vector by Iterative method using push_back(). Print the elements of v1. Print the elements of v2. End.
उदाहरण कोड
#include<iostream> #include<vector> // for vector #include<iostream> #include<vector>// for vector #include<algorithm>// for copy() and assign() #include<iterator>// for back_inserter using namespace std; int main() { vector<int> v1{7,6,4,5}; vector<int> v2; for (int i=0; i<v1.size(); i++) v2.push_back(v1[i]); cout << "v1 vector elements are : "; for (int i=0; i<v1.size(); i++) cout << v1[i] << " "; cout << endl; cout << "v2 vector elements are : "; for (int i=0; i<v2.size(); i++) cout << v2[i] << " "; cout<< endl; return 0; }
आउटपुट
v1 vector elements are : 7 6 4 5 v2 vector elements are : 7 6 4 5
5) वेक्टर को कंस्ट्रक्टर के रूप में पास करके
एल्गोरिदम
Begin Initialize a vector v1 with its elements. Declare another vector v2 and copying elements of first vector to second vector using constructor method and they are deeply copied. Print the elements of v1. Print the elements of v2. End.
उदाहरण कोड
#include<iostream> #include<vector>// for vector #include<algorithm>// for copy() and assign() #include<iterator>// for back_inserter using namespace std; int main() { vector<int> v1{7,6,4,5}; vector<int> v2(v1); cout << "v1 vector elements are : "; for (int i=0; i<v1.size(); i++) cout << v1[i] << " "; cout << endl; cout << "v2 vector elements are : "; for (int i=0; i<v2.size(); i++) cout << v2[i] << " "; cout<< endl; return 0; }
आउटपुट
v1 vector elements are : 7 6 4 5 v2 vector elements are : 7 6 4 5