C++에서 벡터 정렬은 std::sort()를 사용하여 수행할 수 있습니다.
알고리즘
Begin Decalre v of vector type. Initialize some values into v in array pattern. Print “Elements before sorting”. for (const auto &i: v) print all the values of variable i. Print “Elements after sorting”. Call sort(v.begin(), v.end()) function to sort all the elements of the v vector. for (const auto &i: v) print all the values of variable i. End.
다음은 C++에서 벡터를 정렬하는 간단한 예입니다.
예시
#include <iostream> #include <vector> #include <algorithm> using namespace std; int main() { vector<int> v = { 10, 9, 8, 6, 7, 2, 5, 1 }; cout<<"Elements before sorting"<<endl; for (const auto &i: v) cout << i << ' '<<endl; cout<<"Elements after sorting"<<endl; sort(v.begin(), v.end()); for (const auto &i: v) cout << i << ' '<<endl; return 0; }
출력
Elements before sorting 10 9 8 6 7 2 5 1 Elements after sorting 1 2 5 6 7 8 9 10