Computer >> Máy Tính >  >> Lập trình >> C ++

Làm việc với Mảng và Vectơ bằng STL trong C ++

Mảng và vectơ là cấu trúc dữ liệu rất quan trọng trong lập trình cạnh tranh để giải quyết vấn đề. Và STL ( Thư viện mẫu chuẩn ) trong lập trình c ++ cung cấp một số hàm để thực hiện các hoạt động của mảng và vectơ.

Hãy xem một số chức năng này đang hoạt động,

Tìm tổng, tối thiểu và tối đa của mảng / vectơ - Trong STL có chức năng giúp bạn tìm tổng, max và min của mảng / vector. Chức năng với chức năng đó,

Tìm tổng

accumulate(startIndex, endIndex, initialSum)

Phần tử lớn nhất của mảng / vecto

*max_element(startIndex, endIndex)

Phần tử nhỏ nhất của mảng / vectơ

*min_element(startIndex, endIndex)

Chương trình thực hiện các phép toán trên mảng -

Ví dụ

#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;
}

Đầu ra

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

Chương trình thực hiện các phép toán trên vector -

Ví dụ

#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;
}

Đầu ra

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

Sắp xếp các phần tử của mảng / vectơ -

Trong STL, có một hàm sort () có thể được sử dụng để sắp xếp các phần tử của mảng / vectơ. Hàm sử dụng kỹ thuật sắp xếp nhanh để sắp xếp mảng / vectơ.

Cú pháp

sort(startIndex, endIndex)

Chương trình sắp xếp các phần tử của mảng -

Ví dụ

#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;
}

Đầu ra

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

Chương trình sắp xếp các phần tử của vector -

Ví dụ

#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;
}

Đầu ra

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