Computer >> 컴퓨터 >  >> 프로그램 작성 >> C++

C++에서 STL을 사용하여 배열 및 벡터 작업

<시간/>

배열과 벡터는 문제 해결을 위한 경쟁 프로그래밍에서 매우 중요한 데이터 구조입니다. 그리고 STL(표준 템플릿 라이브러리 ) C++ 프로그래밍에서 배열 및 벡터 작업을 수행하는 몇 가지 기능을 제공합니다.

이러한 기능 중 일부가 실제로 작동하는지 살펴보겠습니다.

배열/벡터의 합계, 최소값 및 최대값 찾기 − STL에는 배열/벡터의 합, 최대값, 최소값을 찾는 데 도움이 되는 기능이 있습니다. 기능이 있는 기능,

합계 구하기

accumulate(startIndex, endIndex, initialSum)

배열/vecto의 가장 큰 요소

*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