【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;

}

posted @ 2019-03-14 10:54  Joel_Wang  阅读(140)  评论(0编辑  收藏  举报