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

C++ STL में deque_insert()

Deque क्या है?

Deque डबल एंडेड कतारें हैं जो अनुक्रम कंटेनर हैं जो दोनों छोरों पर विस्तार और संकुचन की कार्यक्षमता प्रदान करती हैं। एक कतार डेटा संरचना उपयोगकर्ता को केवल अंत में डेटा डालने और सामने से डेटा हटाने की अनुमति देती है। बस स्टॉप पर कतारों की सादृश्यता लें जहां व्यक्ति को केवल अंत से कतार में डाला जा सकता है और सामने खड़ा व्यक्ति सबसे पहले हटाया जाता है, जबकि डबल एंडेड कतार में डेटा का सम्मिलन और विलोपन दोनों छोरों पर संभव है।

insert() क्या है?

deque insert() फ़ंक्शन का उपयोग deque में तत्वों को डालने के लिए किया जाता है।

  • यह फ़ंक्शन निर्दिष्ट स्थिति पर तत्व डालने के लिए उपयोग किया जाता है।
  • यह फ़ंक्शन deque में n संख्या के तत्व डालने के लिए भी उपयोग किया जाता है।
  • यह निर्दिष्ट सीमा में तत्वों को डालने के लिए भी उपयोग किया जाता है।

सिंटैक्स

deque_name.insert (iterator position, const_value_type& value) deque_name.insert (iterator position, size_type n, const_value_type& value) deque_name.insert (iterator position, iterator first, iterator last)

पैरामीटर

  • मान - डालने के लिए नए तत्व को निर्दिष्ट करता है।
  • n - डालने के लिए तत्वों की संख्या निर्दिष्ट करता है।
  • first, last - यह पुनरावर्तक को निर्दिष्ट करता है जो डालने के लिए तत्वों की एक श्रृंखला निर्दिष्ट करता है।

रिटर्न मान

यह पुनरावर्तक लौटाता है जो नए डाले गए तत्व के पहले को इंगित करता है।

उदाहरण

इनपुट Deque - 1 2 3 4 5

आउटपुट नया Deque - 1 1 2 3 4 5

इनपुट Deque - 11 12 13 14 15

आउटपुट नया Deque - 11 12 12 12 13 14 15

अनुसरण किया जाने वाला दृष्टिकोण

  • पहले हम deque घोषित करते हैं।
  • फिर हम deque प्रिंट करते हैं।
  • फिर हम insert() फ़ंक्शन घोषित करते हैं।

उपरोक्त दृष्टिकोण का उपयोग करके हम नया तत्व डाल सकते हैं।

उदाहरण

// C++ code to demonstrate the working of deque insert( ) function #include<iostream.h> #include<deque.h> Using namespace std; int main ( ){    // declaring the deque    Deque<int> deque = { 55, 84, 38, 66, 67 };    // print the deque    cout<< " Deque: ";    for( auto x = deque.begin( ); x != deque.end( ); ++x)       cout<< *x << " ";    // declaring insert( ) function    x = deque.insert(x, 22);    // printing deque after inserting new element    cout<< " New Deque:";    for( x = deque.begin( ); x != deque.end( ); ++x)       cout<< " " <<*x;    return 0; }

आउटपुट

यदि हम उपरोक्त कोड चलाते हैं तो यह निम्न आउटपुट उत्पन्न करेगा

Input - Deque: 55 84 38 66 67 Output - New Deque: 22 55 84 38 66 67

उदाहरण

// C++ code to demonstrate the working of deque insert( ) function #include<iostream.h> #include<deque.h> Using namespace std; int main( ){    deque<char> deque ={ 'B' , 'L' , 'D' };    cout<< " Deque: ";    for( auto x = deque.begin( ); x != deque.end( ); ++x)       cout<< *x << " ";    deque.insert(x + 1, 2, 'O');    // printing deque after inserting new element    cout<< " New Deque:";    for( auto x = deque.begin( ); x != deque.end( ); ++x)       cout<< " " <<*x;    return 0; }

आउटपुट

यदि हम उपरोक्त कोड चलाते हैं तो यह निम्न आउटपुट उत्पन्न करेगा

Input – Deque: B L D Output – New Deque: B L O O D

उदाहरण

// C++ code to demonstrate the working of deque insert( ) function #include<iostream.h> #include<deque.h> #include<vector.h> Using namespace std; int main( ){    deque<int> deque ={ 65, 54, 32, 98, 55 };    cout<< " Deque: ";    for( auto x = deque.begin( ); x != deque.end( ); ++x)       cout<< *x << " ";    vector<int7gt; v(3, 19);    deque.insert(x, v.begin( ), v.end( ) );    // printing deque after inserting new element    cout<< " New Deque:";    for( auto x = deque.begin( ); x != deque.end( ); ++x)       cout<< " " <<*x;    return 0; }

आउटपुट

यदि हम उपरोक्त कोड चलाते हैं तो यह निम्न आउटपुट उत्पन्न करेगा

Input – Deque: 65 54 32 98 55 Output – New Deque: 65 19 19 19 65 54 32 98 55
  1. - C++ STL में vector insert() फ़ंक्शन

    C++ STL में vector insert() फ़ंक्शन निर्दिष्ट स्थिति पर एलिमेंट्स से पहले नए एलिमेंट्स डालकर कंटेनर का आकार बढ़ाने में मदद करता है। यह C++ STL में एक पूर्वनिर्धारित फ़ंक्शन है। हम तीन प्रकार के सिंटैक्स के साथ मान डाल सकते हैं 1. केवल स्थिति और मान बताकर मान डालें: vector_name.insert(pos,value); 2.

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

    C++ STL में multiset insert() फंक्शन जो एक पोजीशन से दूसरी पोजीशन तक एक मल्टीसेट से दूसरे मल्टीसेट में एलिमेंट्स इन्सर्ट करता है। उपयोग किए गए फंक्शन्स की सूची: ms.size() = मल्टीसेट का आकार लौटाता है। ms.insert() = मल्टीसेट में एलिमेंट्स इन्सर्ट करने के लिए उपयोग किया जाता है। उदाहरण कोड #includ

  1. - C++ STL में emplace बनाम insert

    emplace ऑपरेशन ऑब्जेक्ट की अनावश्यक कॉपी से बचाता है और इन्सर्ट ऑपरेशन की तुलना में अधिक कुशलता से इन्सर्शन करता है। इन्सर्ट ऑपरेशन एक ऑब्जेक्ट के संदर्भ को लेता है। एल्गोरिदम Begin सेट घोषित करें। पेयर इन्सर्ट करने के लिए emplace() का उपयोग करें। emplace() का उपयोग करके पेयर इन्सर्ट कर