C++ STL में vector insert() फ़ंक्शन निर्दिष्ट स्थिति पर एलिमेंट्स से पहले नए एलिमेंट्स डालकर कंटेनर का आकार बढ़ाने में मदद करता है।
यह C++ STL में एक पूर्वनिर्धारित फ़ंक्शन है।
हम तीन प्रकार के सिंटैक्स के साथ मान डाल सकते हैं
1. केवल स्थिति और मान बताकर मान डालें:
vector_name.insert(pos,value);
2. स्थिति, मान और आकार बताकर मान डालें:
vector_name.insert(pos,size,value);
3. भरे हुए वेक्टर से खाली वेक्टर में मान डालें, स्थिति बताकर जहां मान डाले जाने हैं और भरे हुए वेक्टर के इटरेटर्स:
empty_eector_name.insert(pos,iterator1,iterator2);
एल्गोरिथम
Begin Declare a vector v with values. Declare another empty vector v1. Declare another vector iter as iterator. Insert a value in v vector before the beginning. Insert another value with mentioning its size before the beginning. Print the values of v vector. Insert all values of v vector in v1 vector with mentioning the iterator of v vector. Print the values of v1 vector. End.
उदाहरण
#include<iostream> #include <bits/stdc++.h> using namespace std; int main() { vector<int> v = { 50,60,70,80,90},v1; //v(मानों के साथ), v1 को वेक्टर के रूप में घोषित करना। vector<int>::iterator iter; //एक इटरेटर घोषित करना iter = v.insert(v.begin(), 40); //v वेक्टर में शुरुआत से पहले एक मान डालना। iter = v.insert(v.begin(), 1, 30); //v वेक्टर में शुरुआत से पहले उसके आकार के साथ एक मान डालना। cout << "The vector1 elements are: \n"; for (iter = v.begin(); iter != v.end(); ++iter) cout << *iter << " "<<endl; // v वेक्टर के मान प्रिंट करना v1.insert(v1.begin(), v.begin(), v.end()); //v के सभी मान v1 वेक्टर में डालना। cout << "The vector2 elements are: \n"; for (iter = v1.begin(); iter != v1.end(); ++iter) cout << *iter << " "<<endl; // v1 वेक्टर के मान प्रिंट करना return 0; }
आउटपुट
The vector1 elements are: 30 40 50 60 70 80 90 The vector2 elements are: 30 40 50 60 70 80 90