Vector之是否含有重复元素和乱序排列
2013-10-10 20:55:05| 分类: C++/C | 标签:random_shuffle find vector c++ 乱序 |字号大中小 订阅
1.是否含有重复元素
vector没有查找是否含有相同元素的方法,但是头文件algorithm中有find方法。
2.乱序排列
同样的使用
algorithm中的random_shuffle方法
3.示例
vector<string> allAnswers; while(allAnswers.size()<3){ int answer_i = rand()%RANGE; char answer_str[ARRAYSIZE]={0}; sprintf(answer_str,"%d",answer_i); vector<string>::iterator result = find( allAnswers.begin( ), allAnswers.end( ), answer_str); if(result == allAnswers.end( ) && answer_str!=answer){ allAnswers.push_back(answer_str); } } allAnswers.push_back(answer); random_shuffle (allAnswers.begin(), allAnswers.end());