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

एसटीएल में वेक्टर लागू करने के लिए सी++ प्रोग्राम

जब कोई तत्व डाला या हटाया जाता है तो वेक्टर स्वचालित रूप से गतिशील सरणी की तरह आकार बदलने की क्षमता रखते हैं, कंटेनर स्वचालित रूप से अपने भंडारण को संभालता है। वेक्टर तत्वों को सन्निहित भंडारण में रखा जाता है ताकि उन्हें इटरेटर्स का उपयोग करके एक्सेस और ट्रैवर्स किया जा सके। डेटा को वेक्टर के आरंभ, मध्य या अंत में डाला या मिटाया जा सकता है।

कार्य और विवरण:

List of functions used here:
   v.size() = Returns the size of vector.
   v.push_back() = It is used to insert elements to the vector from end.
   v.pop_back() = To pop out the value from the vector from back.
   v.capacity() = Returns the size of the storage space currently allocated to the vector as number of elements.
   v.clear() = Clears the vector.

उदाहरण कोड

#include <iostream>
#include <vector>
using namespace std;
int main() {
   vector<int> v;
   vector<int>::iterator it;
   int c, i;
   while (1) {
      cout<<"1.Size of the Vector"<<endl;
      cout<<"2.Insert Element into the Vector"<<endl;
      cout<<"3.Delete Last Element of the Vector"<<endl;
      cout<<"4.Display the capacity of vector"<<endl;
      cout<<"5.Display by Iterator"<<endl;
      cout<<"6.Clear the Vector"<<endl;
      cout<<"7.Exit"<<endl;
      cout<<"Enter your Choice: ";
      cin>>c;
      switch(c) {
         case 1:
            cout<<"Size of Vector: ";
            cout<<v.size()<<endl;
         break;
         case 2:
            cout<<"Enter value to be inserted: ";
            cin>>i;
            v.push_back(i);
         break;
         case 3:
            cout<<"Delete Last Element Inserted:"<<endl;
            v.pop_back();
         break;
         case 4:
            cout<<"Displaying capacity of vector: ";
            cout<<v.capacity()<<endl;
         break;
         case 5:
            cout<<"Displaying Vector by Iterator: ";
            for (it = v.begin(); it != v.end(); it++) {
               cout<<*it<<" ";
            }
            cout<<endl;
         break;
         case 6:
            v.clear();
            cout<<"Vector Cleared"<<endl;
         break;
         case 7:
            exit(1);
         break;
         default:
            cout<<"Wrong Choice"<<endl;
      }
   }
   return 0;
}

आउटपुट

1.Size of the Vector
2.Insert Element into the Vector
3.Delete Last Element of the Vector
4.Display the capacity of vector
5.Display by Iterator
6.Clear the Vector
7.Exit
Enter your Choice: 1
Size of Vector: 0
1.Size of the Vector
2.Insert Element into the Vector
3.Delete Last Element of the Vector
4.Display the capacity of vector
5.Display by Iterator
6.Clear the Vector
7.Exit
Enter your Choice: 2
Enter value to be inserted: 7
1.Size of the Vector
2.Insert Element into the Vector
3.Delete Last Element of the Vector
4.Display the capacity of vector
5.Display by Iterator
6.Clear the Vector
7.Exit
Enter your Choice: 2
Enter value to be inserted: 6
1.Size of the Vector
2.Insert Element into the Vector
3.Delete Last Element of the Vector
4.Display the capacity of vector
5.Display by Iterator
6.Clear the Vector
7.Exit
Enter your Choice: 2
Enter value to be inserted: 4
1.Size of the Vector
2.Insert Element into the Vector
3.Delete Last Element of the Vector
4.Display the capacity of vector
5.Display by Iterator
6.Clear the Vector
7.Exit
Enter your Choice: 2
Enter value to be inserted: 3
1.Size of the Vector
2.Insert Element into the Vector
3.Delete Last Element of the Vector
4.Display the capacity of vector
5.Display by Iterator
6.Clear the Vector
7.Exit
Enter your Choice: 2
Enter value to be inserted: 5
1.Size of the Vector
2.Insert Element into the Vector
3.Delete Last Element of the Vector
4.Display the capacity of vector
5.Display by Iterator
6.Clear the Vector
7.Exit
Enter your Choice: 4
Displaying capacity of vector: 8
1.Size of the Vector
2.Insert Element into the Vector
3.Delete Last Element of the Vector
4.Display the capacity of vector
5.Display by Iterator
6.Clear the Vector
7.Exit
Enter your Choice: 5
Displaying Vector by Iterator: 7 6 4 3 5
1.Size of the Vector
2.Insert Element into the Vector
3.Delete Last Element of the Vector
4.Display the capacity of vector
5.Display by Iterator
6.Clear the Vector
7.Exit
Enter your Choice: 3
Delete Last Element Inserted:
1.Size of the Vector
2.Insert Element into the Vector
3.Delete Last Element of the Vector
4.Display the capacity of vector
5.Display by Iterator
6.Clear the Vector
7.Exit
Enter your Choice: 5
Displaying Vector by Iterator: 7 6 4 3
1.Size of the Vector
2.Insert Element into the Vector
3.Delete Last Element of the Vector
4.Display the capacity of vector
5.Display by Iterator
6.Clear the Vector
7.Exit
Enter your Choice: 6
Vector Cleared
1.Size of the Vector
2.Insert Element into the Vector
3.Delete Last Element of the Vector
4.Display the capacity of vector
5.Display by Iterator
6.Clear the Vector
7.Exit
Enter your Choice: 7
Exit.

  1. STL में Set_Symmetric_difference को लागू करने के लिए C++ प्रोग्राम

    यह सेट_सिमेट्रिक_डिफरेंस को लागू करने के लिए एक सी ++ प्रोग्राम है। दो सेटों का सममित अंतर उन तत्वों द्वारा निर्मित होता है जो एक सेट में मौजूद होते हैं, लेकिन दूसरे में नहीं। सामान्य सेट ऑपरेशन हैं - संघ सेट करें चौराहे सेट करें सममित सेट अंतर या अनन्य-या अंतर या घटाव सेट करें एल्गोरिदम Begin

  1. सी++ कार्यक्रम एसटीएल में Set_Intersection लागू करने के लिए

    दो समुच्चयों का प्रतिच्छेदन केवल उन तत्वों से बनता है जो दोनों समुच्चयों में उभयनिष्ठ हैं। फ़ंक्शन द्वारा कॉपी किए गए तत्व हमेशा पहले सेट से उसी क्रम में आते हैं। दोनों सेटों के तत्वों को पहले ही ऑर्डर कर दिया जाएगा। सामान्य सेट ऑपरेशन हैं - संघ सेट करें चौराहे सेट करें सममित सेट अंतर या अनन्य-या

  1. सी++ प्रोग्राम एसटीएल में Set_Difference को लागू करने के लिए

    दो समुच्चयों का अंतर केवल पहले सेट में मौजूद तत्वों से बनता है, दूसरे सेट में नहीं। फ़ंक्शन द्वारा कॉपी किए गए तत्व हमेशा पहले सेट से उसी क्रम में आते हैं। दोनों सेटों के तत्वों को पहले ही ऑर्डर कर दिया जाएगा। सामान्य सेट ऑपरेशन हैं - संघ सेट करें चौराहे सेट करें सममित सेट अंतर या अनन्य-या अंतर या