C++ 算法 stable_sort() 函數用於將 [first, last) 範圍內的元素按升序排序,與 sort 類似,但保持等價元素的順序。
第一個版本使用運算符比較元素,第二個版本使用 comp 進行比較。
用法
template <class RandomAccessIterator>
void stable_sort ( RandomAccessIterator first, RandomAccessIterator last );
template <class RandomAccessIterator, class Compare>
void stable_sort ( RandomAccessIterator first, RandomAccessIterator last,
Compare comp );
參數
first: 一個雙向迭代器,指向要排序的範圍內的第一個元素。
last:一個雙向迭代器,指向要排序的範圍中過去的最後一個元素。
comp: 一個用戶定義的二元謂詞函數,它接受兩個參數,如果兩個參數按順序返回真,否則返回假。它遵循嚴格的弱排序來對元素進行排序。
返回值
空
複雜度
運行時複雜度取決於可用內存量。
如果有足夠的額外內存可用,那麽複雜性在 first 和 last 之間的距離上是線性的。最多執行 N*log2(N) 元素比較,其中 N = last - first。
如果沒有額外的內存可用,那麽複雜性在 first 和 last 之間的距離上是多線性的。最多執行 N*log22(N) 元素比較,其中 N = last - first。
數據競爭
範圍 [first, last) 中的對象被修改。
異常
如果任何元素比較、元素交換或迭代器上的操作引發異常,則此函數將引發異常。
請注意,無效參數會導致未定義的行為。
例子1
讓我們看一個簡單的例子來演示 stable_sort() 的使用:
#include <iostream>
#include <vector>
#include <algorithm>
using namespace std;
int main()
{
vector<int> v = {3, 1, 4, 2, 5};
cout<<"Before sorting:";
for_each(v.begin(), v.end(), [](int x) {
cout << x << " ";
});
stable_sort(v.begin(), v.end());
cout<<"\nAfter sorting: ";
for_each(v.begin(), v.end(), [](int x) {
cout << x << " ";
});
return 0;
}
輸出:
Before sorting:3 1 4 2 5 After sorting: 1 2 3 4 5
例子2
讓我們看另一個簡單的例子:
#include <algorithm>
#include <iostream>
#include <string>
#include <vector>
using namespace std;
struct Employee {
Employee(int age, string name):age(age), name(name) { }
int age;
string name; // Does not particpate in comparisons
};
bool operator<(const Employee &lhs, const Employee &rhs) {
return lhs.age < rhs.age;
}
int main()
{
vector<Employee> v = {
Employee(58, "Robin"),
Employee(23, "Bob"),
Employee(60, "Devid"),
};
stable_sort(v.begin(), v.end());
cout<<"Age:Name "<<endl<<"-----------\n";
for (const Employee &e:v) {
cout << e.age << ":" << e.name << '\n';
}
return 0;
}
輸出:
Age:Name ----------- 23:Bob 58:Robin 60:Devid
例子3
讓我們看另一個簡單的例子:
#include <iostream>
#include <algorithm>
#include <string>
#include <vector>
using namespace std;
struct Student {
string name;
int sec;
char group;
};
bool compBySec(Student a, Student b)
{
return a.sec < b.sec;
}
bool compByGroup(Student a, Student b)
{
return a.group < b.group;
}
bool compByName(Student a, Student b)
{
return a.name < b.name;
}
void print(const vector <Student>& v)
{
cout << "Name \tSec\tGroup" << "\n-------------------------"<<endl;
for (unsigned int i = 0; i < v.size(); i++)
{
cout << v[i].name << "\t" << v[i].sec<< "\t"
<< v[i].group << endl;
}
}
int main()
{
vector <Student> Students;
string name[] = {"Anjali", "Bob", "Chinu ", "Faizal ",
"Nikita ", "Deep ", "Aman", "Rohit "};
int sec[] = {3, 4, 3, 3, 1, 4, 3, 2};
int group[] = {'A', 'C', 'A', 'A', 'A', 'B', 'B', 'A'};
for (int i = 0; i < 8; i++)
{
Student p;
p.name = name[i];
p.sec = sec[i];
p.group = group[i];
Students.push_back(p);
}
stable_sort(Students.begin(), Students.end(), compByName);
cout << "Stable Sort by name" << endl;
print(Students);
cout << endl;
stable_sort(Students.begin(), Students.end(), compBySec);
cout << "Stable Sort by section" << endl;
print(Students);
return 0;
}
輸出:
Stable Sort by name Name Sec Group ------------------------- Aman 3 B Anjali 3 A Bob 4 C Chinu 3 A Deep 4 B Faizal 3 A Nikita 1 A Rohit 2 A Stable Sort by section Name Sec Group ------------------------- Nikita 1 A Rohit 2 A Aman 3 B Anjali 3 A Chinu 3 A Faizal 3 A Bob 4 C Deep 4 B
示例 4
讓我們看另一個簡單的例子:
#include <vector>
#include <algorithm>
#include <functional> // For greater<int>( )
#include <iostream>
// Return whether first element is greater than the second
bool UDgreater (int elem1, int elem2 )
{
return elem1 > elem2;
}
int main( )
{
using namespace std;
vector <int> v1;
vector <int>::iterator Iter1;
int i;
for ( i = 0 ; i <= 5 ; i++ )
{
v1.push_back( 2 * i );
}
for ( i = 0 ; i <= 5 ; i++ )
{
v1.push_back( 2 * i );
}
cout << "Original vector v1 = ( " ;
for ( Iter1 = v1.begin( ) ; Iter1 != v1.end( ) ; Iter1++ )
cout << *Iter1 << " ";
cout << ")" << endl;
stable_sort(v1.begin( ), v1.end( ) );
cout << "Sorted vector v1 = ( " ;
for ( Iter1 = v1.begin( ) ; Iter1 != v1.end( ) ; Iter1++ )
cout << *Iter1 << " ";
cout << ")" << endl;
// To sort in descending order, specify binary predicate
stable_sort(v1.begin( ), v1.end( ), greater<int>( ) );
cout << "Resorted (greater) vector v1 = ( " ;
for ( Iter1 = v1.begin( ) ; Iter1 != v1.end( ) ; Iter1++ )
cout << *Iter1 << " ";
cout << ")" << endl;
// A user-defined (UD) binary predicate can also be used
stable_sort(v1.begin( ), v1.end( ), UDgreater );
cout << "Resorted (UDgreater) vector v1 = ( " ;
for ( Iter1 = v1.begin( ) ; Iter1 != v1.end( ) ; Iter1++ )
cout << *Iter1 << " ";
cout << ")" << endl;
return 0;
}
輸出:
Original vector v1 = ( 0 2 4 6 8 10 0 2 4 6 8 10 ) Sorted vector v1 = ( 0 0 2 2 4 4 6 6 8 8 10 10 ) Resorted (greater) vector v1 = ( 10 10 8 8 6 6 4 4 2 2 0 0 ) Resorted (UDgreater) vector v1 = ( 10 10 8 8 6 6 4 4 2 2 0 0 )
相關用法
- C++ Algorithm stable_partition()用法及代碼示例
- C++ Algorithm set_union()用法及代碼示例
- C++ Algorithm set_intersection()用法及代碼示例
- C++ Algorithm set_difference()用法及代碼示例
- C++ Algorithm set_symmetric_difference()用法及代碼示例
- C++ Algorithm swap_ranges()用法及代碼示例
- C++ Algorithm sort()用法及代碼示例
- C++ Algorithm sort_heap()用法及代碼示例
- C++ Algorithm shuffle()用法及代碼示例
- C++ Algorithm remove_if()用法及代碼示例
- C++ Algorithm remove()用法及代碼示例
- C++ Algorithm max_element()用法及代碼示例
- C++ Algorithm next_permutation()用法及代碼示例
- C++ Algorithm upper_bound()用法及代碼示例
- C++ Algorithm minmax()用法及代碼示例
- C++ Algorithm remove_copy_if()用法及代碼示例
- C++ Algorithm random_shuffle()用法及代碼示例
- C++ Algorithm pop_heap()用法及代碼示例
- C++ Algorithm replace()用法及代碼示例
- C++ Algorithm lower_bound()用法及代碼示例
注:本文由純淨天空篩選整理自 C++ Algorithm stable_sort()。非經特殊聲明,原始代碼版權歸原作者所有,本譯文未經允許或授權,請勿轉載或複製。