1.find(
在输入迭代器所定义的范围内查找与第三个参数相等的第一个对象。
//多次利用find查找出现的次数
std::vector<int> numbers {5, 46, -5, -6, 23, 17, 5, 9, 6, 5};
size_t count {};
int five {5};
auto start_iter = std::begin(numbers);
auto end_iter = std::end(numbers);
while((start_iter = std::find(start_iter, end_iter, five)) != end_iter)
{
++count;
++start_iter;
}
std::cout << five << " was found " << count << " times." << std::endl;
2.find_if()
指定的范围内查找可以使第三个参数指定的谓词返回 true 的第一个对象。
返回值:返回一个指向被找到对象的迭代器,若没有,返回结束迭代器。
//find_if找到第一个大于5的元素
int value {5};
auto iter1 = std::find_if(std::begin(numbers), std::end(numbers),
[value](int n) { return n > value; });
if(iter1 != std::end(numbers))
std::cout << *iter1 << " was found greater than " << value << ".\n";
3.find_if_not()
查找使第三个参数指定的谓词 返回 false 的第一个对象。
返回值:返回一个指向被找到对象的迭代器,若没有,返回结束迭代器。
//查找小于等于5的元素
size_t count1 {};
int five1 {5};
auto start_iter1 = std::begin(numbers);
auto end_iter1 = std::end(numbers);
//当元素大于 five 时,才会返回 true
while((start_iter1 = std::find_if_not(start_iter1, end_iter1,
[five1](int n) {return n > five1; })) != end_iter1)
{
++count1;
++start_iter1;
}
std::cout << count1 << " elements were found that are not greater than "<< five1 << std::endl;
4.find_first_of()
在第一个序列中搜索第二个序列在第一个序列中第一次出现的任何元序列。
// text 中查找第一次出现的 vowels 中的任意字符。在这个示例中,返回的迭代器指向“The”的第三个字母
string text {"The world of searching"};
string vowels {"aeiou"};
auto iter = std::find_first_of (std::begin(text), std:: end(text),
std::begin(vowels),std::end(vowels));
if(iter != std::end(text))
std::cout << "We found '" << *iter << "'." << std::endl;
//用循环来查找 text 中所有匹配 vowels 中字符的字符
string found{};
for(auto iter = std::begin(text);(iter = std::find_first_of (iter,std::end(text),
std::begin(vowels),std::end(vowels)))!= std::end(text);)
found += *(iter++);
std::cout << "The characters \"" << found << "\" were found in text." << std::endl;//"eooeai"
且 find_first_of() 可以在第二个序列中搜索指定范围内可以使第 5 个参数指定的二元谓词返回 true 的元素。
// numbers 中第一个可以被 factors 数组中的元素 13 整除的元素
std::vector<long> numbers2{64L, 46L, -65L, -128L, 121L, 17L, 35L, 9L, 91L, 5L};
int factors[] {7, 11, 13};
auto iter2 = std::find_first_of(std::begin(numbers2),std::end(numbers2),std::begin(factors),
std::end(factors),[](long v, long d) { return v % d == 0; });
if (iter2 != std::end (numbers2))
std::cout << *iter2 << " was found." << std::endl;
5.adjacent_find()
用来搜索序列中两个连续相等的元素。
返回值:返回的迭代器指向前两个相等元素中的第一个。若无一对相等的元素,返回结束迭代器。
//搜索 saying 字符串的前两个相等字符
string saying{"Children should be seen and not heard."};
auto iter3 = std::adjacent_find(std::begin(saying), std::end(saying));
if (iter3 != std::end(saying))
std::cout <<"In the following text: \n\""<< saying << "\"\n'"<< *iter3 <<
"' is repeated starting at index position "<< std::distance(std::begin(saying), iter3) << std::endl;
且,adjacent_find()允许提供一个应用于连续元素的谓词。
//查找这个序列中第一对都为奇数的连续整数
std::vector<long> numbers3{64L, 46L, -65L, -128L, 121L, 17L, 35L, 9L, 91L, 5L};
auto iter4 = std::adjacent_find(std::begin(numbers3),std::end(numbers3),
[](long n1, long n2){ return n1 % 2 && n2 % 2; });
if(iter4 != std::end(numbers3))
std::cout <<"The first pair of odd numbers is "<< *iter4 << " and " << *(iter4+1) << std::endl;//121 17
6.find_end()
在一个序列中查找最后一个和另一个元素段匹配的匹配项,也可以看作在一个元素序列中查找子序列的最后一个匹配项。
返回值:返回一个指向子序列的最后一个匹配项的第一个元素的迭代器,或是一个指向这个序列的结束迭代器。
//从 text 中搜索“had had”的最后一个匹配项
string text{"Smith, where Jones had had \"had\", had had \"had had\"." "
\"Had had\" had had the examiners\' approval."};
std::cout << text << std::endl;
string phrase{"had had"};
auto iter = std::find_end(std::begin(text), std::end(text), std::begin(phrase),std::end(phrase));
if(iter != std::end(text))
std::cout << "The last \"" << phrase<< "\" was found at index "<<
std::distance(std::begin (text), iter) << std::endl;
在 text 中搜索 phrase 的所有匹配项。
//循环表达式会在 [std::begin(text),end_iter) 这个范围内搜索 phrase。最开始的搜索范围包含 text 中的全部元素
size_t count2{};
auto iter5 = std::end(text1);
auto end_iter2 = iter5;
while((iter5 = std::find_end(std::begin(text1), end_iter2, std::begin(phrase), std::end(phrase)))!= end_iter2)
{
++count2;
end_iter2 = iter5;
}
std::cout << "\n\""<< phrase << "\" was found " << count2 <<" times." << std::endl;
且, find_end() 接受一个用来比较元素的二元谓词作为第 5 个参数。
//在 text 中会找到与phrase相等 的 5 个实例,忽略大小写
size_t count3 {};
auto iter7 = std::end(text1);
auto end_iter3 = iter7;
while((iter = std::find_end(std::begin(text1), end_iter3, std::begin(phrase), std::end (phrase) ,
[] (char ch1, char ch2) { return std::toupper (ch1) == std::toupper(ch2);})) != end_iter3)
{
++count3;
end_iter3 = iter7;
}
std::cout << "\n\""<< phrase << "\" was found " << count3 <<" times." << std::endl;
7.search()
与 find_end() 算法相似,但它所查找的是第一个匹配项而不是最后一个。
//忽略大小写来搜索“had had”,但会正向查找第一个匹配项。search() 算法返回的迭代器指向找到的子序列的第一个元素
string text2{"Smith, where Jones had had \"had\", had had \"had had\"."" \"Had had\" had had the examiners\' approval."};
std::cout << text2 << std::endl;
string phrase1{"had had"};
size_t count4 {};
auto iter8 = std::begin(text2);
auto end_iter4 = end(text2);
while((iter8 = std::search(iter8, end_iter4, std::begin(phrase1), std::end (phrase1) ,
[](char ch1, char ch2) { return std::toupper (ch1) == std:: toupper (ch2); })) != end_iter4)
{
++count4;
//为了搜索 phrase 的第二个实例,iter 必须增加 phrase 中元素的个数,使它指向下一个找到的序列的第一个元素。
std::advance(iter8, phrase1.size());
}
std::cout << "\n\""<< phrase1 << "\" was found "<< count4<< " times." << std::endl;
8.search_n()
搜索给定元素的匹配项,它在序列中连续出现了给定的次数。
它的前两个参数是定义搜索范围的正向迭代器,第 3 个参数是想要查找的第 4 个元素的连续匹配次数。
std::vector<double> values{2.7, 2.7, 2.7, 3.14, 3.14, 3.14, 2.7, 2.7};
double value1{3.14};
int times {3};
auto iter9 = std::search_n(std::begin(values), std::end(values), times, value1);
if (iter9 != std::end(values))
std::cout << times << " successive instances of " << value1<<
" found starting index " << std::distance (std::begin (values), iter9) <<std::endl;
//一年中每个月份的平均温度
std::vector<int> temperatures{65, 75, 56, 48, 31, 28, 32, 29, 40, 41, 44, 50};
int max_temp{32};
int times1 {3};
//元素小于等于 max_temp 时,它会返回 true
auto iter10 = std::search_n(std::begin(temperatures), std::end(temperatures), times1, max_temp,
[](double v, double max){return v <= max; });
std::vector<string> months {"January", "February", "March", "April", "May", "June","July",
"August", "September", "October", "November", "December"};
if(iter10 != std::end(temperatures))
//distance返回 temperatures 这个序列的第一个元素的索引
std::cout << "It was " << max_temp << " degrees or below for " << times1<<
" months starting in " << months[std::distance(std::begin(temperatures), iter10)]<< std::endl;