【C++进阶:STL常见性质3】

STL3个代表性函数:for_each(), random_shuffle(), sort()

vector<int> stuff;

random_shuffle(stuff.begin(),stuff.end());           随机排列两个迭代器之间的所有元素

sort(stuff.begin(),stuff.end(),  cmp);                      排列两个迭代器之间的所有元素,cmp没有的话默认升序,如下cmp为降序;

bool cmp(const int a,const int b){

  if(a>b)

    return true;

  else

    return false;

}

for_each(scores.begin(),scores.end(),show)    遍历两个迭代器之间的所有元素,并传递给函数show

void show(const int &rr){

  cout<<rr<<endl;

}

猜你喜欢

转载自www.cnblogs.com/joelwang/p/10528805.html