「std::sort」の版間の差分
提供: C++入門
(ページの作成:「<!-- vim: filetype=mediawiki --> 読み方 ;std::sort:えすてぃーでぃー そーと __TOC__ == 概要 == std::sort を利用して、std::vector ...」) |
|||
| 行13: | 行13: | ||
もっと簡単なコードにするには、[[boost::sort]]を利用します。 | もっと簡単なコードにするには、[[boost::sort]]を利用します。 | ||
| − | == std::sortでstd:: | + | == std::sortでstd::vectorを昇順でソートする例 == |
=== ソースコード std_sort_vector.cpp === | === ソースコード std_sort_vector.cpp === | ||
| 行75: | 行75: | ||
</syntaxhighlight> | </syntaxhighlight> | ||
| + | == 降順でソートする例 == | ||
| + | |||
| + | === ソースコード std_sort_greater_vector.cpp === | ||
| + | |||
| + | <syntaxhighlight lang="cpp"> | ||
| + | #include <iostream> | ||
| + | #include <boost/foreach.hpp> | ||
| + | #include <vector> | ||
| + | |||
| + | 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; | ||
| + | } | ||
| + | </syntaxhighlight> | ||
| + | |||
| + | === コンパイル === | ||
| + | |||
| + | <syntaxhighlight lang="bash"> | ||
| + | g++ -I/usr/local/include std_sort_greater_vector.cpp -o std_sort_greater_vector | ||
| + | </syntaxhighlight> | ||
| + | |||
| + | === 実行例 === | ||
| + | |||
| + | <syntaxhighlight lang="bash"> | ||
| + | % ./std_sort_greater_vector | ||
| + | Before sort | ||
| + | 3 | ||
| + | 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> | ||
| + | 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 | ||
| + | 2 | ||
| + | After sort | ||
| + | 1 | ||
| + | 2 | ||
| + | 3 | ||
| + | 4 | ||
| + | </syntaxhighlight> | ||
== 関連項目 == | == 関連項目 == | ||
| 行80: | 行216: | ||
* [[boost::sort]] | * [[boost::sort]] | ||
* [[std::vector]] | * [[std::vector]] | ||
| + | * [[std::stable_sort]] | ||
2013年3月20日 (水) 17:10時点における版
読み方
- std::sort
- えすてぃーでぃー そーと
目次
概要
std::sort を利用して、std::vector をソートできます。 もっと簡単なコードにするには、boost::sortを利用します。
std::sortでstd::vectorを昇順でソートする例
ソースコード std_sort_vector.cpp
#include <iostream> #include <boost/foreach.hpp> #include <vector> 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> 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> 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