समस्याओं को हल करने के लिए प्रतिस्पर्धी प्रोग्रामिंग में ऐरे और वैक्टर बहुत महत्वपूर्ण डेटा संरचनाएं हैं। और एसटीएल (मानक टेम्पलेट लाइब्रेरी ) c++ प्रोग्रामिंग में सरणियों और वैक्टर के संचालन को करने के लिए कुछ कार्य प्रदान करते हैं।
आइए इनमें से कुछ कार्यों को क्रिया में देखें,
सरणी/वेक्टर का योग, न्यूनतम और अधिकतम ज्ञात करना - एसटीएल में ऐसे फ़ंक्शन हैं जो आपको सरणी/वेक्टर के योग, अधिकतम और न्यूनतम को खोजने में मदद करते हैं। वहाँ समारोह के साथ कार्य,
राशि ढूँढना
accumulate(startIndex, endIndex, initialSum)
सरणी/वेक्टो का सबसे बड़ा तत्व
*max_element(startIndex, endIndex)
सरणी/वेक्टर का सबसे छोटा तत्व
*min_element(startIndex, endIndex)
सरणी पर संचालन करने का कार्यक्रम -
उदाहरण
#include <bits/stdc++.h>
using namespace std;
int main(){
int array[] = {65, 7,12, 90, 31, 113 };
int l = sizeof(array) / sizeof(array[0]);
cout<<"The elments of the array are : ";
for(int i = 0; i<l; i++)
cout<<array[i]<<"\t";
cout<<endl;
cout<<"The sum of all elements of the array: "<<accumulate(array, array + l, 0)<<endl;
cout<<"The element with maximum value in array: "<<*max_element(array, array + l)<<endl;
cout<<"The element with minimum value in array: "<<*min_element(array, array + l)<<endl;
return 0;
} आउटपुट
The elments of the array are : 65 7 12 90 31 113 The sum of all elements of the array: 318 The element with maximum value in array: 113 The element with minimum value in array: 7
वेक्टर पर संचालन करने का कार्यक्रम -
उदाहरण
#include <bits/stdc++.h>
using namespace std;
int main(){
vector<int> vec= {65, 7,12, 90, 31, 113 };
cout<<"The sum of all elments of the vector: "<<accumulate(vec.begin(), vec.end() + 1, 0)<<endl;
cout<<"The element with maximum value in vector: "<<*max_element(vec.begin(), vec.end())<<endl;
cout<<"The element with minimum value in vector: "<<*min_element(vec.begin(), vec.end())<<endl;
return 0;
} आउटपुट
The sum of all elments of the vector: 318 The element with maximum value in vector: 113 The element with minimum value in vector: 7
सरणी/वेक्टर के तत्वों को छांटना -
एसटीएल में, एक फ़ंक्शन सॉर्ट () होता है जिसका उपयोग सरणी/वेक्टर के तत्वों को सॉर्ट करने के लिए किया जा सकता है। फ़ंक्शन सरणी/वेक्टर को सॉर्ट करने के लिए त्वरित सॉर्ट तकनीक का उपयोग करता है।
सिंटैक्स
sort(startIndex, endIndex)
सरणी के तत्वों को छाँटने का कार्यक्रम -
उदाहरण
#include <bits/stdc++.h>
using namespace std;
int main(){
int array[] = {65, 7,12, 90, 31, 113 };
int l = sizeof(array) / sizeof(array[0]);
cout<<"The elments of the array are : ";
for(int i = 0; i<l; i++)
cout<<array[i]<<"\t";
cout<<endl;
cout<<"\nSorting elements of the array…\n\n";
sort(array, array+l);
cout<<"The sorted array is : ";
for(int i = 0; i<l; i++)
cout<<array[i]<<"\t";
cout<<endl;
return 0;
} आउटपुट
The elments of the array are : 65 7 12 90 31 113 Sorting elements of the array... The sorted array is : 7 12 31 65 90 113
वेक्टर के तत्वों को छाँटने का कार्यक्रम -
उदाहरण
#include <bits/stdc++.h>
using namespace std;
int main(){
vector<int> vec = {65, 7,12, 90, 31, 113 };
cout<<"The elments of the vector are : ";
for(int i = 0; i<vec.size(); i++)
cout<<vec[i]<<"\t";
cout<<endl;
cout<<"\nSorting elements of the vector...\n\n";
sort(vec.begin(), vec.end());
cout<<"The sorted vector is : ";
for(int i = 0; i<vec.size(); i++)
cout<<vec[i]<<"\t";
cout<<endl;
return 0;
} आउटपुट
The elments of the vector are : 65 7 12 90 31 113 Sorting elements of the vector... The sorted vector is : 7 12 31 65 90 113