C++ partial_sort()
C++算法partial_sort()
C++算法 partial_sort()函数用于重新排列范围[first,last)中的元素,例如一种方式,将对第一个和中间之间的元素进行排序,而在中间和最后一个之间的元素将以不确定的顺序排列。
第一个版本使用运算符 进行比较,第二个版本使用 comp 进行元素比较。
语法
default (1) template <class RandomAccessIterator>
void partial_sort (RandomAccessIterator first, RandomAccessIterator middle,
RandomAccessIterator last);
custom (2) template <class RandomAccessIterator, class Compare>
void partial_sort (RandomAccessIterator first, RandomAccessIterator middle,
RandomAccessIterator last, Compare comp);
参数
first: 一个随机访问迭代器,指向要部分排序的范围中的第一个元素。
last: 指向要部分排序的范围中最后一个元素的随机访问迭代器。
middle: 指向一个随机访问迭代器
comp : 用户定义的二进制谓词函数,该函数接受两个参数,并且如果两个参数都位于其中,则返回true命令,否则返回false。
返回值
无
复杂度
在第一个和最后一个之间的距离中,平均复杂度小于线性运算。最多执行N * log(M)个元素比较,其中N =最后一个-第一个,M =中间-第一个。
数据竞争
范围内的对象[first,last)被更改。
异常
如果元素比较,元素交换(或移动)或对元素进行任何操作,则此函数将引发异常。迭代器引发异常。
注意: 无效的参数会导致未定义的行为。
示例1
让我们看看这个简单的示例演示了partial_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 << " ";
});
partial_sort(v.begin(), v.begin() + 2, 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 4 3 5
示例2
让我们看另一个简单的示例:
#include <iostream> // std::cout
#include <algorithm> // std::partial_sort
#include <vector> // std::vector
using namespace std;
bool myfunction (int i,int j) { return (i<j); }
int main () {
int myints[] = {9,8,7,6,5,4,3,2,1};
vector<int> myvector (myints, myints+9);
// using default comparison (operator <):
partial_sort (myvector.begin(), myvector.begin()+5, myvector.end());
// using function as comp
partial_sort (myvector.begin(), myvector.begin()+5, myvector.end(),myfunction);
// print out content:
cout << "myvector contains:";
for (vector<int>::iterator it=myvector.begin(); it!=myvector.end(); ++it)
cout << ' ' << *it;
cout << '\n';
return 0;
}
输出:
myvector contains: 1 2 3 4 5 9 8 7 6
示例3
我们来看一个默认版本的简单示例:
#include <iostream>
#include <algorithm>
#include <functional>
#include <vector>
using namespace std ;
int main()
{
const int VECTOR_SIZE = 8 ;
// Define a template class vector of int
typedef vector<int> IntVector ;
//Define an iterator for template class vector of strings
typedef IntVector::iterator IntVectorIt ;
IntVector Numbers(VECTOR_SIZE) ;
IntVectorIt start, end, it ;
// Initialize vector Numbers
Numbers[0] = 4 ;
Numbers[1] = 10;
Numbers[2] = 70 ;
Numbers[3] = 30 ;
Numbers[4] = 10;
Numbers[5] = 69 ;
Numbers[6] = 96 ;
Numbers[7] = 7;
start = Numbers.begin() ; // location of first
// element of Numbers
end = Numbers.end() ; // one past the location
// last element of Numbers
cout << "Before calling partial_sort\n" << endl ;
// print content of Numbers
cout << "Numbers { " ;
for(it = start; it != end; it++)
cout << *it << " " ;
cout << " }\n" << endl ;
// sort the smallest 4 elements in the sequence
partial_sort(start, start+4, end) ;
cout << "After calling partial_sort\n" << endl ;
cout << "Numbers { " ;
for(it = start; it != end; it++)
cout << *it << " " ;
cout << " }\n" << endl ;
return 0;
}
输出:
Before calling partial_sort
Numbers { 4 10 70 30 10 69 96 7 }
After calling partial_sort
Numbers { 4 7 10 10 70 69 96 30 }
示例4
让我们看一个自定义(谓词)版本的简单示例:
#include <iostream>
#include <algorithm>
#include <functional>
#include <vector>
using namespace std ;
int main()
{
const int VECTOR_SIZE = 8 ;
// Define a template class vector of int
typedef vector<int> IntVector ;
//Define an iterator for template class vector of strings
typedef IntVector::iterator IntVectorIt ;
IntVector Numbers(VECTOR_SIZE) ;
IntVectorIt start, end, it ;
// Initialize vector Numbers
Numbers[0] = 4 ;
Numbers[1] = 10;
Numbers[2] = 70 ;
Numbers[3] = 30 ;
Numbers[4] = 10;
Numbers[5] = 69 ;
Numbers[6] = 96 ;
Numbers[7] = 7;
start = Numbers.begin() ; // location of first
// element of Numbers
end = Numbers.end() ; // one past the location
// last element of Numbers
cout << "Before calling partial_sort\n" << endl ;
// print content of Numbers
cout << "Numbers { " ;
for(it = start; it != end; it++)
cout << *it << " " ;
cout << " }\n" << endl ;
// sort the smallest 4 elements in the sequence
partial_sort(start, start+4, end, less<int>()) ;
cout << "After calling partial_sort\n" << endl ;
cout << "Numbers { " ;
for(it = start; it != end; it++)
cout << *it << " " ;
cout << " }\n" << endl ;
return 0;
}
输出:
Before calling partial_sort
Numbers { 4 10 70 30 10 69 96 7 }
After calling partial_sort
Numbers { 4 7 10 10 70 69 96 30 }