Computer >> कंप्यूटर ट्यूटोरियल >  >> प्रोग्रामिंग >> C++

C++ STL में vector insert() फ़ंक्शन


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

  1. - C++ STL में atanh() फ़ंक्शन

    atanh() फ़ंक्शन atanh() फ़ंक्शन आर्क हाइपरबोलिक टैंजेंट (व्युत्क्रम हाइपरबोलिक टैंजेंट) लौटाता है। <cmath> हेडर में परिभाषित (C++11 से)। सिंटैक्स atanh(var) पैरामीटर: var - float, double, या long double। मान -1 और 1 के बीच होना चाहिए (अनन्य)। वापसी मान आर्क हाइपरबोलिक टैंजेंट मान (रेडियन मे

  1. - C++ STL में cosh() फ़ंक्शन

    cosh() फ़ंक्शन cosh() फ़ंक्शन दिए गए कोण (रेडियन में) का हाइपरबोलिक कोसाइन लौटाता है। <cmath> हेडर में परिभाषित। सिंटैक्स cosh(var) पैरामीटर: var - float, double, या long double। वापसी मान हाइपरबोलिक कोसाइन मान, सीमा: [1, ∞)। उदाहरण 1: रेडियन इनपुट #include <iostream> #include <cm

  1. - C++ STL में sinh() फ़ंक्शन

    sinh() फ़ंक्शन sinh() फ़ंक्शन दिए गए कोण (रेडियन में) का हाइपरबोलिक साइन लौटाता है। <cmath> हेडर में परिभाषित। सिंटैक्स sinh(var) पैरामीटर: var - float, double, या long double। वापसी मान हाइपरबोलिक साइन मान, सीमा: (-∞, ∞)। उदाहरण 1: रेडियन इनपुट #include <iostream> #include <cmath