一、合并排序算法 - merge 函數(shù)
1、函數(shù)原型分析
在 C++ 語(yǔ)言 的 標(biāo)準(zhǔn)模板庫(kù) ( STL , STL Standard Template Library ) 中 , 提供了 merge 合并排序算法函數(shù) 用于 將 兩個(gè)已排序好的容器 合并成一個(gè)新的已排序的容器 ;
merge 合并排序算法 函數(shù)原型 如下 :
template <class InputIterator1, class InputIterator2, class OutputIterator>
OutputIterator merge(InputIterator1 first1, InputIterator1 last1,
InputIterator2 first2, InputIterator2 last2,
OutputIterator result);
-
參數(shù)解析 :
- InputIterator1 first1 參數(shù) : 有序 輸入 容器 1 的 迭代器范圍 的 起始迭代器 ( 包含該迭代器指向的元素 ) ;
- InputIterator1 last1 參數(shù) : 有序 輸入 容器 1 的 迭代器范圍 的 終止迭代器 ( 不包含該迭代器指向的元素 ) ;
- InputIterator2 first2 參數(shù) : 有序 輸入 容器 2 的 迭代器范圍 的 起始迭代器 ( 包含該迭代器指向的元素 ) ;
- InputIterator2 last2 參數(shù) : 有序 輸入 容器 2 的 迭代器范圍 的 終止迭代器 ( 不包含該迭代器指向的元素 ) ;
- 返回值解析 : 將上述 兩個(gè)輸入容器 迭代器的范圍 的元素 進(jìn)行 合并排序 , 放入到 輸出容器中 , 返回的迭代器 是 指向 " 有序輸出容器 首元素 " 的迭代器 ;
2、代碼示例
代碼示例 :
#include "iostream"
using namespace std;
#include <vector>
#include <algorithm>
#include "functional"
int main() {
// 輸入容器
vector<int> v1 = { 1, 3, 5 };
vector<int> v2 = { 2, 4, 6 };
// 輸出容器
vector<int> v3(v1.size() + v2.size());
// 合并 v1 和 v2 到 v3 , 默認(rèn)使用 < 操作符進(jìn)行比較
merge(v1.begin(), v1.end(), v2.begin(), v2.end(), v3.begin());
// 向 foreach 循環(huán)中傳入 Lambda 表達(dá)式
for_each(v3.begin(), v3.end(), [](int a) {
cout << a << " ";
});
cout << endl;
// 控制臺(tái)暫停 , 按任意鍵繼續(xù)向后執(zhí)行
system("pause");
return 0;
};
執(zhí)行結(jié)果 :
1 2 3 4 5 6
Press any key to continue . . .
二、隨機(jī)排序算法 - random_shuffle 函數(shù)
1、函數(shù)原型分析
在 C++ 語(yǔ)言 的 標(biāo)準(zhǔn)模板庫(kù) ( STL , STL Standard Template Library ) 中 , 提供了 random_shuffle 隨機(jī)排序算法函數(shù) 用于 對(duì)容器中的元素進(jìn)行隨機(jī)排序 ;
random_shuffle 隨機(jī)排序算法 函數(shù)原型 如下 :
template <class RandomAccessIterator>
void random_shuffle(RandomAccessIterator first, RandomAccessIterator last);
-
參數(shù)解析 :
- RandomAccessIterator first 參數(shù) : 迭代器范圍 的 起始迭代器 ( 包含該迭代器指向的元素 ) ;
- RandomAccessIterator last 參數(shù) : 迭代器范圍 的 終止迭代器 ( 不包含該迭代器指向的元素 ) ;
- 返回值解析 : 沒(méi)有返回值 ;
2、代碼示例
在下面的示例中 ,
vector 單端數(shù)組 容器中 , 初始元素順序?yàn)?:
9 5 2 7
調(diào)用 如下代碼 :
// 隨機(jī)排序
random_shuffle(myVector.begin(), myVector.end());
進(jìn)行隨機(jī)排序后 , 再次查看元素順序 , 變?yōu)?:
9 5 7 2
代碼示例 :
#include "iostream"
using namespace std;
#include <vector>
#include <algorithm>
#include "functional"
int main() {
// 要隨機(jī)排序的容器
vector<int> myVector{9, 5, 2, 7};
// 遍歷打印容器中元素內(nèi)容
for_each(myVector.begin(), myVector.end(), [](int a) {
cout << a << " ";
});
cout << endl;
// 隨機(jī)排序
random_shuffle(myVector.begin(), myVector.end());
// 遍歷打印容器中元素內(nèi)容
for_each(myVector.begin(), myVector.end(), [](int a) {
cout << a << " ";
});
cout << endl;
// 控制臺(tái)暫停 , 按任意鍵繼續(xù)向后執(zhí)行
system("pause");
return 0;
};
執(zhí)行結(jié)果 :
9 5 2 7
9 5 7 2
Press any key to continue . . .
三、反轉(zhuǎn)序列算法 - reverse 函數(shù)
1、函數(shù)原型分析
在 C++ 語(yǔ)言 的 標(biāo)準(zhǔn)模板庫(kù) ( STL , STL Standard Template Library ) 中 , 提供了 reverse 反轉(zhuǎn)序列算法函數(shù) 用于 對(duì)容器中的元素進(jìn)行 反轉(zhuǎn)序列 ;
reverse 反轉(zhuǎn)序列 函數(shù) 的 作用是 是 將序列 [first, last) 中的元素順序顛倒 , 使得原來(lái)序列的第一個(gè)元素成為最后一個(gè) , 原來(lái)序列的最后一個(gè)元素成為第一個(gè) , 依此類推 ;
該算法函數(shù) , 并不涉及到 排序操作 , 只是單純的將 元素順序 進(jìn)行反轉(zhuǎn) ;
reverse 反轉(zhuǎn)序列算法 函數(shù)原型 如下 :
template <class BidirectionalIterator>
void reverse(BidirectionalIterator first, BidirectionalIterator last);
-
參數(shù)解析 :
- BidirectionalIterator first 參數(shù) : 迭代器范圍 的 起始迭代器 ( 包含該迭代器指向的元素 ) ;
- BidirectionalIterator last 參數(shù) : 迭代器范圍 的 終止迭代器 ( 不包含該迭代器指向的元素 ) ;
- 返回值解析 : 沒(méi)有返回值 ;
2、代碼示例
代碼示例 :
#include "iostream"
using namespace std;
#include <vector>
#include <algorithm>
#include "functional"
int main() {
// 要隨機(jī)排序的容器
vector<int> myVector{9, 5, 2, 7};
// 遍歷打印容器中元素內(nèi)容
for_each(myVector.begin(), myVector.end(), [](int a) {
cout << a << " ";
});
cout << endl;
// 倒序排序
reverse(myVector.begin(), myVector.end());
// 遍歷打印容器中元素內(nèi)容
for_each(myVector.begin(), myVector.end(), [](int a) {
cout << a << " ";
});
cout << endl;
// 控制臺(tái)暫停 , 按任意鍵繼續(xù)向后執(zhí)行
system("pause");
return 0;
};
執(zhí)行結(jié)果 :
9 5 2 7
7 2 5 9
Press any key to continue . . .文章來(lái)源:http://www.zghlxwxcb.cn/news/detail-801724.html
文章來(lái)源地址http://www.zghlxwxcb.cn/news/detail-801724.html
到了這里,關(guān)于【C++】STL 算法 - 排序算法 ( 合并排序算法 - merge 函數(shù) | 隨機(jī)排序算法 - random_shuffle 函數(shù) | 反轉(zhuǎn)序列算法 - reverse 函數(shù) )的文章就介紹完了。如果您還想了解更多內(nèi)容,請(qǐng)?jiān)谟疑辖撬阉鱐OY模板網(wǎng)以前的文章或繼續(xù)瀏覽下面的相關(guān)文章,希望大家以后多多支持TOY模板網(wǎng)!