당신은 당신이 처음에 반복자를 얻어 sort
에 전달할 종료 std::begin
및 std::end
을 사용할 수, 배열을 정렬 할 std::sort
을 사용할 수 있으며, 비교 기능을 제공하기 위해 std::tuple
를 사용할 수 있습니다.
std::vector
과이 작업의 원시 배열 사이에 특히 관련있는 차이는 없습니다.
예제 코드 :
#include <algorithm> // std::sort
#include <iterator> // std::begin, std::end
#include <iostream> // std::cout, std::endl
#include <tuple> // std::tie
using namespace std;
struct Triplet { int values[3]; };
template< int n >
void display(Triplet const (&array)[n])
{
for(auto const& t : array)
{
cout << t.values[0] << ' ' << t.values[1] << ' ' << t.values[2] << endl;
}
}
auto main()
-> int
{
Triplet array[] =
{
{ 2, 30, 2000 },
{ 1, 40, 1000 },
{ 3, 10, 4000 },
{ 5, 20, 3000 },
{ 4, 50, 5000 }
};
auto const triplet_less = [](Triplet const& a, Triplet const& b) -> bool
{
return
(tie(a.values[2], a.values[0], a.values[1]) <
tie(b.values[2], b.values[0], b.values[1]));
};
cout << "Original data: " << endl;
display(array);
sort(begin(array), end(array), triplet_less);
cout << endl;
cout << "Sorted data: " << endl;
display(array);
}
가 어떻게 행과 열을 표시합니까? – juanchopanza