http://blog.163.com/xychenbaihu@yeah/blog/static/132229655201393092915831/
std::sort 对vector成员进行排序;
std::sort(v.begin(),v.end(),compare);
std::lower_bound 在排序的vector中进行二分查找,查找第一大于等于;
std::lower_bound(v.begin(),v.end(),v.element_type_obj,compare);
std::upper_bound 在排序的vector中进行二分查找,查找第一个大于;
std::upper_bound(v.begin(),v.end(),v.element_type_obj,compare);
std::binary_search 在排序的vector中进行二分查找;
std::
binary_search(v.begin(),v.end(),v.element_type_obj,compare
);
std::unique 将排序的vector
vector<element_type>::iterator iter = std::unique(v.begin(),v.end(),compare);
v.resize(iter-v.begin());
std::for_each 将迭代器指向区间的内容调用指定的函数
void show(element_type& obj);
std::for_each(v.begin(),v.end(),show);
这里也可以:
strcut showclass
{
void operator ()(element_type& obj){
//具体操作……
}
}showobj;
std::for_each(v.begin(),v.end(),showobj);
std::random_shuffle将迭代器指向的内容内容打散
std::random_shuffle(v.begin(),v.end());
或:
// random generator function: ptrdiff_t myrandom (ptrdiff_t i) { return rand()%i;} // pointer object to it: ptrdiff_t (*p_myrandom)(ptrdiff_t) = myrandom;
random_shuffle ( myvector.begin(), myvector.end(), p_myrandom);
具体使用: std::sort使用:
std::sort(vectorobj.begin(),vectorobj.end(),compare_fun);
std::
lower_bound 使用:
std::
lower_bound
(
vectorobj.begin(),vectorobj.end(),比较对象,compare_fun
);
比较对象的类型和vectorobj的成员类型一样。
示例: #include <vector> #include <algorithm> #include <algorithm> #include <iostream>
struct Student { int age; };
//sort和lower_bound使用比较函数的原型:
//boo function(const vector的成员类型 lit,const vector的成员类型 rit); bool CompareOperation(const Student& lit,const Student& rit) { if(lit.age<rit.age){ return true; }else{ return false; } }
int main(int argc,char *argv[]) { std::vector<Student> stuvec; Student a; a.age = 5; stuvec.push_back(a); Student b; b.age = 6; stuvec.push_back(b); Student c; c.age = 4; stuvec.push_back(c); Student d; d.age = 7; stuvec.push_back(d); std::cout<<“befort sort”<<std::endl; for(size_t index=0;index<stuvec.size();index++){ std::cout<<stuvec[index].age<<std::endl; }
std::sort(stuvec.begin(),stuvec.end(),CompareOperation); std::cout<<“after sort”<<std::endl; for(size_t index=0;index<stuvec.size();index++){ std::cout<<stuvec[index].age<<std::endl; } std::cout<<“binary search”<<std::endl;
std::vector<Student>::iterator iter =
std::lower_bound(stuvec.begin(),stuvec.end(),
b
,CompareOperation); if(iter != stuvec.end()){ std::cout<<iter->age<<std::endl; } return 0; } 运行结果:
stl算法库参看:
http://www.cplusplus.com/reference/algorithm/lower_bound/?kw=lower_bound >= 的第一个元素 和
http://www.cplusplus.com/reference/algorithm/upper_bound/?kw=upper_bound > 的第一个元素
示例代码: // lower_bound/upper_bound example #include <iostream> // std::cout #include <algorithm> // std::lower_bound, std::upper_bound, std::sort #include <vector> // std::vector
int main () { int myints[] = {10,20,30,30,20,10,10,20}; std::vector<int> v(myints,myints+8); // 10 20 30 30 20 10 10 20
std::sort (v.begin(), v.end()); // 10 10 10 20 20 20 30 30
std::vector<int>::iterator low,up; low=std::lower_bound (v.begin(), v.end(), 20); // 第一个 >= 20 的元素的迭代器 up= std::upper_bound (v.begin(), v.end(), 20); // 第一个 > 20 的元素的迭代器
std::cout << “lower_bound at position ” << (low- v.begin()) << ‘\n’; std::cout << “upper_bound at position ” << (up – v.begin()) << ‘\n’;
return 0; }
使用std::unique可以将有序的vector中的成员去重: #include <iostream> // std::cout #include <algorithm> // std::lower_bound, std::upper_bound, std::sort,std::unique, std::for_each #include <vector> // std::vector
void show(const int& i) { std::cout<<i<<std::endl; }
int main () { int myints[] = {10,20,30,30,20,10,10,20}; std::vector<int> v(myints,myints+8); // 10 20 30 30 20 10 10 20
std::sort (v.begin(), v.end()); // 10 10 10 20 20 20 30 30 std::vector<int>::iterator iter = std::unique(v.begin(),v.end()); v.resize(iter – v.begin()); std::for_each(v.begin(),v.end(),show);
return 0; }
使用std::binary_search对vector进行排序;
// binary_search example #include <iostream> #include <algorithm> #include <vector> using namespace std; bool myfunction (int i,int j) { return (i<j); } int main () { int myints[] = {1,2,3,4,5,4,3,2,1}; vector<int> v(myints,myints+9); // 1 2 3 4 5 4 3 2 1 // using default comparison: sort (v.begin(), v.end()); cout << "looking for a 3... "; if (binary_search (v.begin(), v.end(), 3)) cout << "found!\n"; else cout << "not found.\n"; // using myfunction as comp: sort (v.begin(), v.end(), myfunction); cout << "looking for a 6... "; if (binary_search (v.begin(), v.end(), 6, myfunction)) cout << "found!\n"; else cout << "not found.\n"; return 0; }
参看:http://www.cplusplus.com/reference/algorithm/binary_search/?kw=binary_search
std::random_shuffle将指定的迭代器区间的内容随机打散:
// random_shuffle example #include <iostream> #include <algorithm> #include <functional> #include <vector> #include <ctime> #include <cstdlib> using namespace std; // random generator function: ptrdiff_t myrandom (ptrdiff_t i) { return rand()%i;} // pointer object to it: ptrdiff_t (*p_myrandom)(ptrdiff_t) = myrandom; int main () { srand ( unsigned ( time (NULL) ) ); vector<int> myvector; vector<int>::iterator it; // set some values: for (int i=1; i<10; ++i) myvector.push_back(i); // 1 2 3 4 5 6 7 8 9 // using built-in random generator: random_shuffle ( myvector.begin(), myvector.end() ); // using myrandom: random_shuffle ( myvector.begin(), myvector.end(), p_myrandom); // print out content: cout << "myvector contains:"; for (it=myvector.begin(); it!=myvector.end(); ++it) cout << " " << *it; cout << endl; return 0; }