关于C++ STL中的partial_sort()函数

来源:百度知道 编辑:UC知道 时间:2024/06/01 06:15:23
函数原型是这样的partial_sort(first,middle,last).C++编程宝典上是这样解释的:对迭代器first和last所指定范围中的元素进行偏序排序.经过排序的元素放到序列的前面部分,其余元素放到迭代器middle和last指定的范围内.书上并且提供了一个例子,
#include <iostream>
#include <vector>
#include <algorithm>
#include <string>

////////////////////////////////////////
// The function to be called by
// for_each().
////////////////////////////////////////
void show_val(string val)
{
std::cout << val << std::endl;
}

////////////////////////////////////////
// The main() function.
////////////////////////////////////////
int main()
{
// Create the vector object.
std::vector<string> strVector;

// Populate the vector with values.
strVector.push_back("Zebra");
strVector.push_back("Deer");
strVector.push_back("Fish");
strVector.push_back("Snake");

partial_sort() 部分排序,不是全部排序。

因为 strVector.begin() + 5

排出前5个,其它的放到后面不管

这和partial_sort的实现有关系。
他只保证参数1和参数2之间的内容是排序的,而其它的部分的顺序,是不排序的,至于为什么是这样,你可以参考其实现。

XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX