「std::sort」の版間の差分
提供: C++入門
(ページの作成:「<!-- vim: filetype=mediawiki --> 読み方 ;std::sort:えすてぃーでぃー そーと __TOC__ == 概要 == std::sort を利用して、std::vector ...」) |
|||
| (同じ利用者による、間の5版が非表示) | |||
| 行1: | 行1: | ||
| − | + | [[std::sort]] を利用して、[[std::vector]] をソートできます。[[std::vector]]をソートしたり、構造体を入れた[[std::vector]]のソートを紹介します。 | |
| − | + | ||
| − | + | ||
| − | 読み方 | + | '''読み方''' |
;[[std::sort]]:えすてぃーでぃー そーと | ;[[std::sort]]:えすてぃーでぃー そーと | ||
| 行9: | 行7: | ||
== 概要 == | == 概要 == | ||
| − | |||
[[std::sort]] を利用して、[[std::vector]] をソートできます。 | [[std::sort]] を利用して、[[std::vector]] をソートできます。 | ||
もっと簡単なコードにするには、[[boost::sort]]を利用します。 | もっと簡単なコードにするには、[[boost::sort]]を利用します。 | ||
| + | == ヘッダファイル == | ||
| + | <syntaxhighlight lang="cpp"> | ||
| + | #include <algorithm> | ||
| + | </syntaxhighlight> | ||
| + | == std::sortでstd::vectorを昇順でソートする例 == | ||
| + | === ソースコード std_sort_vector.cpp === | ||
| + | <syntaxhighlight lang="cpp"> | ||
| + | #include <iostream> | ||
| + | #include <boost/foreach.hpp> | ||
| + | #include <vector> | ||
| + | #include <algorithm> | ||
| + | using namespace std; | ||
| − | + | void | |
| + | dump(vector<int>& v) | ||
| + | { | ||
| + | BOOST_FOREACH(int x, v) { | ||
| + | cout << x << endl; | ||
| + | } | ||
| + | } | ||
| − | === | + | int |
| + | main (int argc, char *argv[]) | ||
| + | { | ||
| + | vector<int> v; | ||
| + | |||
| + | v.push_back ( 3 ); | ||
| + | v.push_back ( 4 ); | ||
| + | v.push_back ( 1 ); | ||
| + | v.push_back ( 2 ); | ||
| + | |||
| + | cout << "Before sort" << std::endl; | ||
| + | dump (v); | ||
| + | |||
| + | std::sort(v.begin(), v.end() ); | ||
| + | |||
| + | cout << "After sort" << std::endl; | ||
| + | dump (v); | ||
| + | return 0; | ||
| + | } | ||
| + | </syntaxhighlight> | ||
| + | |||
| + | === コンパイル === | ||
| + | <syntaxhighlight lang="bash"> | ||
| + | g++ -I/usr/local/include std_sort_vector.cpp -o std_sort_vector | ||
| + | </syntaxhighlight> | ||
| + | |||
| + | === 実行例 === | ||
| + | <syntaxhighlight lang="bash"> | ||
| + | % ./std_sort_vector | ||
| + | Before sort | ||
| + | 3 | ||
| + | 4 | ||
| + | 1 | ||
| + | 2 | ||
| + | After sort | ||
| + | 1 | ||
| + | 2 | ||
| + | 3 | ||
| + | 4 | ||
| + | </syntaxhighlight> | ||
| + | == 降順でソートする例 == | ||
| + | === ソースコード std_sort_greater_vector.cpp === | ||
<syntaxhighlight lang="cpp"> | <syntaxhighlight lang="cpp"> | ||
#include <iostream> | #include <iostream> | ||
#include <boost/foreach.hpp> | #include <boost/foreach.hpp> | ||
#include <vector> | #include <vector> | ||
| + | #include <algorithm> | ||
using namespace std; | using namespace std; | ||
| 行45: | 行102: | ||
dump (v); | dump (v); | ||
| − | std::sort(v.begin(), v.end() ); | + | std::sort(v.begin(), v.end(), std::greater<int>() ); |
cout << "After sort" << std::endl; | cout << "After sort" << std::endl; | ||
| 行56: | 行113: | ||
<syntaxhighlight lang="bash"> | <syntaxhighlight lang="bash"> | ||
| − | g++ -I/usr/local/include | + | g++ -I/usr/local/include std_sort_greater_vector.cpp -o std_sort_greater_vector |
</syntaxhighlight> | </syntaxhighlight> | ||
| 行62: | 行119: | ||
<syntaxhighlight lang="bash"> | <syntaxhighlight lang="bash"> | ||
| − | % ./ | + | % ./std_sort_greater_vector |
Before sort | Before sort | ||
3 | 3 | ||
4 | 4 | ||
| + | 1 | ||
| + | 2 | ||
| + | After sort | ||
| + | 4 | ||
| + | 3 | ||
| + | 2 | ||
| + | 1 | ||
| + | </syntaxhighlight> | ||
| + | |||
| + | == std::vector<構造体>をソートする例 == | ||
| + | |||
| + | === ソースコード std_sort_vector_struct.cpp === | ||
| + | |||
| + | <syntaxhighlight lang="cpp"> | ||
| + | #include <iostream> | ||
| + | #include <boost/foreach.hpp> | ||
| + | #include <vector> | ||
| + | #include <algorithm> | ||
| + | using namespace std; | ||
| + | |||
| + | struct S { | ||
| + | int value; | ||
| + | }; | ||
| + | |||
| + | void | ||
| + | dump(vector<S>& v) | ||
| + | { | ||
| + | BOOST_FOREACH(S x, v) { | ||
| + | cout << x.value << endl; | ||
| + | } | ||
| + | } | ||
| + | |||
| + | bool operator<(const S& left, const S& right) { | ||
| + | return left.value < right .value; | ||
| + | } | ||
| + | |||
| + | S | ||
| + | make_S(int x) { | ||
| + | S s = {x}; | ||
| + | return s; | ||
| + | } | ||
| + | |||
| + | int | ||
| + | main (int argc, char *argv[]) | ||
| + | { | ||
| + | vector<S> v; | ||
| + | |||
| + | v.push_back ( make_S(4) ); | ||
| + | v.push_back ( make_S(3) ); | ||
| + | v.push_back ( make_S(1) ); | ||
| + | v.push_back ( make_S(2) ); | ||
| + | |||
| + | cout << "Before sort" << std::endl; | ||
| + | dump (v); | ||
| + | |||
| + | std::sort(v.begin(), v.end() ); | ||
| + | |||
| + | cout << "After sort" << std::endl; | ||
| + | dump (v); | ||
| + | return 0; | ||
| + | } | ||
| + | </syntaxhighlight> | ||
| + | |||
| + | === コンパイル === | ||
| + | |||
| + | <syntaxhighlight lang="bash"> | ||
| + | g++ -I/usr/local/include std_sort_vector_struct.cpp -o std_sort_vector_struct | ||
| + | </syntaxhighlight> | ||
| + | |||
| + | === 実行例 === | ||
| + | |||
| + | <syntaxhighlight lang="bash"> | ||
| + | % ./std_sort_vector_struct | ||
| + | Before sort | ||
| + | 4 | ||
| + | 3 | ||
1 | 1 | ||
2 | 2 | ||
| 行75: | 行208: | ||
</syntaxhighlight> | </syntaxhighlight> | ||
| − | + | == ラムダ式を使ったソートの例 == | |
| + | [[ラムダ式]]を使用する場合には、[[C++11]]に対応したコンパイラが必要です。 | ||
| + | === ソースコード lambda_sort1.cpp === | ||
| + | <syntaxhighlight lang="cpp"> | ||
| + | #include <algorithm> | ||
| + | #include <iostream> | ||
| + | #include <algorithm> | ||
| + | using namespace std; | ||
| + | int main(int argc, char const* argv[]) | ||
| + | { | ||
| + | int i_array[] = {1,3,5,9,8,6,4,0}; | ||
| + | sort(i_array, std::end(i_array), | ||
| + | [](int x, int y) -> int { | ||
| + | return ( x < y ); | ||
| + | } ); | ||
| + | for (auto i: i_array) { | ||
| + | cout << i << " "; | ||
| + | } | ||
| + | cout << endl; | ||
| + | return 0; | ||
| + | } | ||
| + | </syntaxhighlight> | ||
| + | === コンパイル === | ||
| + | <syntaxhighlight lang="bash"> | ||
| + | g++49 -std=c++11 -I/usr/local/lib/gcc49/include/c++/ \ | ||
| + | -Wl,-rpath=/usr/local/lib/gcc49 lambda_sort1.cpp -o lambda_sort1 | ||
| + | </syntaxhighlight> | ||
| + | === 実行例 === | ||
| + | <syntaxhighlight lang="bash"> | ||
| + | % ./lambda_sort1 | ||
| + | 0 1 3 4 5 6 8 9 | ||
| + | </syntaxhighlight> | ||
== 関連項目 == | == 関連項目 == | ||
| − | |||
* [[boost::sort]] | * [[boost::sort]] | ||
* [[std::vector]] | * [[std::vector]] | ||
| + | * [[std::stable_sort]] | ||
| + | * [[auto]] | ||
| + | * [[ラムダ式]] | ||
| + | <!-- vim: filetype=mediawiki --> | ||
2013年12月28日 (土) 16:41時点における最新版
std::sort を利用して、std::vector をソートできます。std::vectorをソートしたり、構造体を入れたstd::vectorのソートを紹介します。
読み方
- std::sort
- えすてぃーでぃー そーと
目次
概要
std::sort を利用して、std::vector をソートできます。 もっと簡単なコードにするには、boost::sortを利用します。
ヘッダファイル
#include <algorithm>
std::sortでstd::vectorを昇順でソートする例
ソースコード std_sort_vector.cpp
#include <iostream> #include <boost/foreach.hpp> #include <vector> #include <algorithm> using namespace std; void dump(vector<int>& v) { BOOST_FOREACH(int x, v) { cout << x << endl; } } int main (int argc, char *argv[]) { vector<int> v; v.push_back ( 3 ); v.push_back ( 4 ); v.push_back ( 1 ); v.push_back ( 2 ); cout << "Before sort" << std::endl; dump (v); std::sort(v.begin(), v.end() ); cout << "After sort" << std::endl; dump (v); return 0; }
コンパイル
g++ -I/usr/local/include std_sort_vector.cpp -o std_sort_vector
実行例
% ./std_sort_vector Before sort 3 4 1 2 After sort 1 2 3 4
降順でソートする例
ソースコード std_sort_greater_vector.cpp
#include <iostream> #include <boost/foreach.hpp> #include <vector> #include <algorithm> using namespace std; void dump(vector<int>& v) { BOOST_FOREACH(int x, v) { cout << x << endl; } } int main (int argc, char *argv[]) { vector<int> v; v.push_back ( 3 ); v.push_back ( 4 ); v.push_back ( 1 ); v.push_back ( 2 ); cout << "Before sort" << std::endl; dump (v); std::sort(v.begin(), v.end(), std::greater<int>() ); cout << "After sort" << std::endl; dump (v); return 0; }
コンパイル
g++ -I/usr/local/include std_sort_greater_vector.cpp -o std_sort_greater_vector
実行例
% ./std_sort_greater_vector Before sort 3 4 1 2 After sort 4 3 2 1
std::vector<構造体>をソートする例
ソースコード std_sort_vector_struct.cpp
#include <iostream> #include <boost/foreach.hpp> #include <vector> #include <algorithm> using namespace std; struct S { int value; }; void dump(vector<S>& v) { BOOST_FOREACH(S x, v) { cout << x.value << endl; } } bool operator<(const S& left, const S& right) { return left.value < right .value; } S make_S(int x) { S s = {x}; return s; } int main (int argc, char *argv[]) { vector<S> v; v.push_back ( make_S(4) ); v.push_back ( make_S(3) ); v.push_back ( make_S(1) ); v.push_back ( make_S(2) ); cout << "Before sort" << std::endl; dump (v); std::sort(v.begin(), v.end() ); cout << "After sort" << std::endl; dump (v); return 0; }
コンパイル
g++ -I/usr/local/include std_sort_vector_struct.cpp -o std_sort_vector_struct
実行例
% ./std_sort_vector_struct Before sort 4 3 1 2 After sort 1 2 3 4
ラムダ式を使ったソートの例
ラムダ式を使用する場合には、C++11に対応したコンパイラが必要です。
ソースコード lambda_sort1.cpp
#include <algorithm> #include <iostream> #include <algorithm> using namespace std; int main(int argc, char const* argv[]) { int i_array[] = {1,3,5,9,8,6,4,0}; sort(i_array, std::end(i_array), [](int x, int y) -> int { return ( x < y ); } ); for (auto i: i_array) { cout << i << " "; } cout << endl; return 0; }
コンパイル
g++49 -std=c++11 -I/usr/local/lib/gcc49/include/c++/ \ -Wl,-rpath=/usr/local/lib/gcc49 lambda_sort1.cpp -o lambda_sort1
実行例
% ./lambda_sort1 0 1 3 4 5 6 8 9