1、adjacent_find()
adjacent_find() 返回容器中第一个相邻的重复值。
adjacent_find()有两个版本:
- 判断重复(相等)依据是数据类型的“==”运算符。
QStringList list;
list<<"aa"<<"bb"<<"cc"<<"dd"<<"aa"<<"ee"<<"ee"<<"kk"<<"kk";
auto result = std::adjacent_find(list.begin(),list.end());
qDebug()<<*result;
- 传入一个自定义函数(二元谓词(2个参数)函数)作为判断重复(相等)的依据。
QStringList list;
list<<"aa"<<"bb"<<"xc"<<"xd";
auto result = std::adjacent_find(list.begin(),list.end(),[](const QString & str01,const QString & str02)
{
if(str01[0] == str02[0])
return true;
return false;
});
qDebug()<<*result;
这里自定义了判断重复(相等)的规则:字符串的第一个字符相等则认为字符串相等。
如果没有重复值,则返回第二个迭代器:
2、search_n()
QStringList list;
list<<"aa"<<"bb"<<"cc"<<"dd"<<"aa"<<"aa"<<"aa";
auto result = std::search_n(list.begin(),list.end(),3,"aa");
qDebug()<< *result;
list中是否存在3个相邻重复的“aa”的情况,存在则返回第一个相邻元素的迭代器。
search_n()还有自定义判断重复标准的函数是版本: