ソート順の比較関数
2744 ワード
羅東彬と一緒に噛んで食べるc++の内容を参考にしました
標準は、パラメータの最初の値です.
1.降順でソート
->関数を見ると、前に進むほど大きく感じます.
:aはbの前の状態では大きいので降順です.
2.昇順でソート
->関数を見ると、後ろに進むほど大きく感じます.
3.変数が3の場合のソート
pairはこれらの宣言を利用する.
関連アルゴリズムの問題
:プログラマ-失敗率
4.2 Dベクトルを整列するとき...
大きなものや少ないものを使用しないで、関数を個別に作成します.
2つの方法があります.
行きましょう。グローバル関数の作成
降順
昇順
私。classの演算子関数の作成
降順
昇順
関連ソース #include <algorithm>
#include <vector>
#include <queue>
#include <iostream>
using namespace std;
bool check(int a, int b)
{
return a < b;
}
struct comp {
public :
bool operator()(int a, int b) {
return a < b;
}
};
int main()
{
vector<int>v = { 2,1,-1,88,17,5,-19 };
sort(v.begin(), v.end(), check);
cout << "sort를 이용한 정렬" << endl;
for (const auto i : v)
cout << i << endl;
cout << "우선순위 큐에서의 정렬" << endl;
priority_queue<int, vector<int>, comp>pq(v.begin(), v.end());
while (!pq.empty())
{
cout << pq.top() << endl;
pq.pop();
}
}
Reference
この問題について(ソート順の比較関数), 我々は、より多くの情報をここで見つけました
https://velog.io/@kwt0124/sort를-위한-compare-함수
テキストは自由に共有またはコピーできます。ただし、このドキュメントのURLは参考URLとして残しておいてください。
Collection and Share based on the CC Protocol

->関数を見ると、後ろに進むほど大きく感じます.
3.変数が3の場合のソート
pairはこれらの宣言を利用する.
関連アルゴリズムの問題
:プログラマ-失敗率
4.2 Dベクトルを整列するとき...
大きなものや少ないものを使用しないで、関数を個別に作成します.
2つの方法があります.
行きましょう。グローバル関数の作成
降順
昇順
私。classの演算子関数の作成
降順
昇順
関連ソース #include <algorithm>
#include <vector>
#include <queue>
#include <iostream>
using namespace std;
bool check(int a, int b)
{
return a < b;
}
struct comp {
public :
bool operator()(int a, int b) {
return a < b;
}
};
int main()
{
vector<int>v = { 2,1,-1,88,17,5,-19 };
sort(v.begin(), v.end(), check);
cout << "sort를 이용한 정렬" << endl;
for (const auto i : v)
cout << i << endl;
cout << "우선순위 큐에서의 정렬" << endl;
priority_queue<int, vector<int>, comp>pq(v.begin(), v.end());
while (!pq.empty())
{
cout << pq.top() << endl;
pq.pop();
}
}
Reference
この問題について(ソート順の比較関数), 我々は、より多くの情報をここで見つけました
https://velog.io/@kwt0124/sort를-위한-compare-함수
テキストは自由に共有またはコピーできます。ただし、このドキュメントのURLは参考URLとして残しておいてください。
Collection and Share based on the CC Protocol

関連アルゴリズムの問題
:プログラマ-失敗率
4.2 Dベクトルを整列するとき...
大きなものや少ないものを使用しないで、関数を個別に作成します.
2つの方法があります.
行きましょう。グローバル関数の作成
降順

昇順

私。classの演算子関数の作成
降順

昇順

関連ソース #include <algorithm>
#include <vector>
#include <queue>
#include <iostream>
using namespace std;
bool check(int a, int b)
{
return a < b;
}
struct comp {
public :
bool operator()(int a, int b) {
return a < b;
}
};
int main()
{
vector<int>v = { 2,1,-1,88,17,5,-19 };
sort(v.begin(), v.end(), check);
cout << "sort를 이용한 정렬" << endl;
for (const auto i : v)
cout << i << endl;
cout << "우선순위 큐에서의 정렬" << endl;
priority_queue<int, vector<int>, comp>pq(v.begin(), v.end());
while (!pq.empty())
{
cout << pq.top() << endl;
pq.pop();
}
}
#include <algorithm>
#include <vector>
#include <queue>
#include <iostream>
using namespace std;
bool check(int a, int b)
{
return a < b;
}
struct comp {
public :
bool operator()(int a, int b) {
return a < b;
}
};
int main()
{
vector<int>v = { 2,1,-1,88,17,5,-19 };
sort(v.begin(), v.end(), check);
cout << "sort를 이용한 정렬" << endl;
for (const auto i : v)
cout << i << endl;
cout << "우선순위 큐에서의 정렬" << endl;
priority_queue<int, vector<int>, comp>pq(v.begin(), v.end());
while (!pq.empty())
{
cout << pq.top() << endl;
pq.pop();
}
}
Reference
この問題について(ソート順の比較関数), 我々は、より多くの情報をここで見つけました https://velog.io/@kwt0124/sort를-위한-compare-함수テキストは自由に共有またはコピーできます。ただし、このドキュメントのURLは参考URLとして残しておいてください。
Collection and Share based on the CC Protocol