実戦c++におけるvectorシリーズ--sortアルゴリズムを用いてvectorをソートする(vectorをソートし、安定したソートstdを使用する::stable_sort()
8753 ワード
vectorの操作についてたくさん書きましたが、ちょうど仕事中にvectorをソートする問題があったので、ここで議論します.
sortアルゴリズムを直接使用するには、まず次のことを理解します.
Sorts the elements in the range[first,last]into ascending order.The elements are compared using operator直接コード:
このときstd::stable_sort()は、安定した順序付けを実現するために使用する:Sorts the elements in the range[first,last]into ascending order,like sort,but stable_sort preserves the relative order of the elements with equivalent values.
sortアルゴリズムを直接使用するには、まず次のことを理解します.
template <class RandomAccessIterator>
void sort (RandomAccessIterator first, RandomAccessIterator last);
template <class RandomAccessIterator, class Compare>
void sort (RandomAccessIterator first, RandomAccessIterator last, Compare comp);
Sorts the elements in the range[first,last]into ascending order.The elements are compared using operator
#include // std::cout
#include // std::sort
#include // std::vector
#include
bool myfunction(int i, int j) { return (istruct myclass {
bool operator() (int i, int j) { return (iint main() {
int myints[] = { 32,71,12,45,26,80,53,33 };
std::vector<int> myvector(myints, myints + 8);// 32 71 12 45 26 80 53 33
// using default comparison (operator
std::sort(myvector.begin(), myvector.begin() + 4); //(12 32 45 71)26 80 53 33
// using function as comp
std::sort(myvector.begin() + 4, myvector.end(), myfunction); // 12 32 45 71(26 33 53 80)
// using object as comp
std::sort(myvector.begin(), myvector.end(), myobject); //(12 26 32 33 45 53 71 80)
// print out content:
std::cout << "myvector contains:";
for (std::vector<int>::iterator it = myvector.begin(); it != myvector.end(); ++it)
std::cout << ' ' << *it;
std::cout << '
';
// Sorting the string vector
std::vector<std::string> stringVec = { "John", "Bob", "Joe", "Zack", "Randy" };
sort(stringVec.begin(), stringVec.end());
for (std::string &s : stringVec)
std::cout << s << " ";
return 0;
}
// :
myvector contains: 12 26 32 33 45 53 71 80
Bob Joe John Randy Zack
このときstd::stable_sort()は、安定した順序付けを実現するために使用する:Sorts the elements in the range[first,last]into ascending order,like sort,but stable_sort preserves the relative order of the elements with equivalent values.
#include // std::cout
#include // std::stable_sort
#include // std::vector
bool compare_as_ints(double i, double j)
{
return (int(i)<int(j));
}
int main() {
double mydoubles[] = { 3.14, 1.41, 2.72, 4.67, 1.73, 1.32, 1.62, 2.58 };
std::vector<double> myvector;
myvector.assign(mydoubles, mydoubles + 8);
std::cout << "using default comparison:";
std::stable_sort(myvector.begin(), myvector.end());
for (std::vector<double>::iterator it = myvector.begin(); it != myvector.end(); ++it)
std::cout << ' ' << *it;
std::cout << '
';
myvector.assign(mydoubles, mydoubles + 8);
std::cout << "using 'compare_as_ints' :";
std::stable_sort(myvector.begin(), myvector.end(), compare_as_ints);
for (std::vector<double>::iterator it = myvector.begin(); it != myvector.end(); ++it)
std::cout << ' ' << *it;
std::cout << '
';
return 0;
}
// :
using default comparison: 1.32 1.41 1.62 1.73 2.58 2.72 3.14 4.67
using 'compare_as_ints' : 1.41 1.73 1.32 1.62 2.72 2.58 3.14 4.67