কম্পিউটার

C++ এ STL ব্যবহার করে অ্যারে এবং ভেক্টরের সাথে কাজ করা


সমস্যা সমাধানের জন্য প্রতিযোগিতামূলক প্রোগ্রামিংয়ে অ্যারে এবং ভেক্টর খুবই গুরুত্বপূর্ণ ডেটা স্ট্রাকচার। এবং STL (স্ট্যান্ডার্ড টেমপ্লেট লাইব্রেরি ) c++ প্রোগ্রামিং-এ অ্যারে এবং ভেক্টরের অপারেশন করার জন্য কিছু ফাংশন প্রদান করে।

চলুন দেখা যাক এই ফাংশনগুলির মধ্যে কিছু কার্যাবলি,

অ্যারে/ভেক্টরের যোগফল, ন্যূনতম এবং সর্বোচ্চ সন্ধান করা হচ্ছে − STL-এ এমন ফাংশন রয়েছে যা আপনাকে অ্যারে/ভেক্টরের যোগফল, সর্বোচ্চ এবং মিনিট খুঁজে পেতে সাহায্য করে। সেখানে ফাংশন সহ ফাংশন,

সমষ্টি খোঁজা

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

অ্যারে/ভেক্টরের উপাদান বাছাই −

STL এ, একটি ফাংশন sort() আছে যা অ্যারে/ভেক্টরের উপাদানগুলিকে সাজাতে ব্যবহার করা যেতে পারে। ফাংশন অ্যারে/ভেক্টর সাজানোর জন্য দ্রুত সাজানোর কৌশল ব্যবহার করে।

সিনট্যাক্স

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

  1. C++ এ সংখ্যা, + এবং - সহ একটি অ্যারের অভিব্যক্তি মূল্যায়ন করুন

  2. একটি অ্যারে প্যালিনড্রোম কিনা বা C++ এ STL ব্যবহার করছে না তা পরীক্ষা করার জন্য প্রোগ্রাম

  3. STL ব্যবহার করে C++ এ অ্যারে পণ্য

  4. অ্যারে ব্যবহার করে সারি বাস্তবায়নের জন্য C++ প্রোগ্রাম