检查在算法应用到序列中的元素上时,什么时候使谓词返回 true。
参数定义:前两个参数定义谓词应用范围的输入迭代器;第三个参数指定了谓词。
返回true的情况:
前两个参数指定的范围内,有多少满足指定的第三个参数条件的元素。
count() 会返回等同于第三个参数的元素的个数。count_if() 会返回可以使作为第三个参数的谓词返回 true 的元素个数。
1-2代码实例:
#include <iostream>
using namespace std;
#include <vector>
#include <algorithm>
int main()
{
std::vector<int> ages{22, 19, 46, 75, 54, 19, 27, 66, 61, 33, 22, 19};
int min_age{18};
//none_of
std::cout << "There are "<< (std::none_of(std::begin(ages), std::end(ages),
[min_age](int age) { return age < min_age; }) ? "no": "some") << " people under " << min_age << std::endl;
//any_of
std::cout << "There are "<< (std::any_of(std::begin(ages), std::end(ages),
[min_age] (int age) { return age < min_age;}) ? "some":"no") <<" people under " << min_age << std::endl;
//all_of
int good_age{100};
std::cout << (std::all_of(std::begin(ages), std::end(ages),
[good_age] (int age) { return age < good_age; }) ? "None": "Some") << " of the people are centenarians." << std::endl;
int the_age{19};
std::cout << "There are "<< std::count(std::begin(ages),std::end(ages),the_age)<< " people aged "<< the_age << std::endl;
int max_age{60};
std::cout << "There are "<< std::count_if(std::begin(ages), std::end(ages),
[max_age](int age) { return age > max_age; }) << " people aged over " << max_age << std::endl;
}结果显示:

3.equal
用和比较字符串类似的方式来比较序列。
返回值:如果两个序列的长度相同,并且对应元素都相等,返回 true。
不应该用 equal() 来比较来自于无序 map 或 set 容器中的元素序列。在无序容器中,一组给定元素的顺序可能和保存在另一个无序容器中的一组相等元素不同,因为不同容器的元素很可能会被分配到不同的格子中。
代码举例:
#include <iostream>
#include <vector>
#include <algorithm>
#include <iterator>
#include <string>
using std::string;
int main()
{
std::vector<string> words1 {"one", "two", "three", "four", "five", "six", "seven", "eight", "nine"};
std::vector<string> words2 {"two", "three", "four", "five", "six", "seven", "eight", "nine", "ten"};
auto iter1 = std::begin(words1);
auto end_iter1 = std::end(words1);
auto iter2 = std::begin(words2);
auto end_iter2 = std::end(words2);
std::cout << "Container - words1:";
std::copy(iter1, end_iter1, std::ostream_iterator<string>{std::cout, " "});
std::cout << "\nContainer - words2:";
std::copy(iter2, end_iter2, std::ostream_iterator<string>{std::cout, " "});
std::cout << std::endl;
std::cout << "\n1. Compare from words1[1] to end with words2:";
//words1 的第二个元素到最后一个元素都从 words2 的第一个元素开始匹
std::cout << std::boolalpha << std::equal(iter1 + 1, end_iter1, iter2) << std::endl;
std::cout << "2. Compare from words2[0] to second-to-last with words1:";
//有直接的不匹配;words2 和 words1 的第一个元素不同
std::cout << std::boolalpha << std::equal(iter2, end_iter2 - 1, iter1) << std::endl;
std::cout << "3. Compare from words1[1] to words1[5] with words2:";
// word1 中从第二个元素开始的 5 个元素和 words2 的前五个元素相等
std::cout << std::boolalpha << std::equal(iter1 + 1, iter1 + 6, iter2) << std::endl;
std::cout << "5. Compare all words1 with words2:";
//两个序列的第一个元素直接就不匹配,所以结果为 false
std::cout << std::boolalpha << std::equal(iter1, end_iter1, iter2) << std::endl;
//验证函数
std::vector<string> r1 { "three", "two", "ten"};
std::vector<string> r2 {"twelve", "ten", "twenty" };
//谓词是一个在字符串 参数的第一个字符相等时返回 true
std::cout << std::boolalpha<< std::equal (std::begin (r1) , std::end (r1) , std::begin (r2),
[](const string& s1, const string& s2) { return s1[0] == s2[0]; })<< std::endl; // true
}结果显示:

4.mismatch
告诉我们两个序列是否匹配,而且如果不匹配,它还能告诉我们不匹配的位置。
返回值:返回的 pair 对象包含两个迭代器。它的 first 成员是一个来自前两个参数所指定序列的迭代器,second 是来自于第二个序列的迭代器。
当序列不匹配时,pair 包含的迭代器指向第一对不匹配的元素;因此这个 pair 对象为 pair<iter1+n,iter2 + n>,这两个序列中索引为 n 的元素是第一个不匹配的元素。
mismatch(iter1,end_iter1,iter2):
代码实例:
#include <iostream>
#include <vector>
#include <algorithm>
#include <string>
#include <iterator>
using std::string;
using word_iter = std::vector<string>::iterator;
int main()
{
std::vector<string> words1 {"one", "two", "three", "four", "five", "six", "seven", "eight", "nine"};
std::vector<string> words2 {"two", "three", "four", "five", "six", "eleven", "eight", "nine", "ten"};
auto iter1 = std::begin(words1);
auto end_iter1 = std::end(words1);
auto iter2 = std::begin(words2);
auto end_iter2 = std::end(words2);
// Lambda expression to output mismatch() result
auto print_match = [](const std::pair<word_iter, word_iter>& pr, const word_iter& end_iter)
{
if(pr.first != end_iter)
std::cout << "\nFirst pair of words that differ are "<< *pr.first << " and " << *pr.second << std::endl;
else
std::cout << "\nRanges are identical." << std::endl;
};
std::cout << "Container - words1: ";
std::copy(iter1, end_iter1, std::ostream_iterator<string>{std::cout, " "});
std::cout << "\nContainer - words2: ";
std::copy(iter2, end_iter2, std::ostream_iterator<string>{std::cout, " "});
std::cout << std::endl;
std::cout << "\nCompare from words1[1] to end with words2:";
print_match(std::mismatch(iter1 + 1, end_iter1, iter2), end_iter1);
std::cout << "\nCompare from words2[0] to second-to-last with words1:";
print_match(std::mismatch(iter2, end_iter2 - 1, iter1), end_iter2 - 1);
std::cout << "\nCompare from words1[1] to words1[5] with words2:";
print_match(std::mismatch(iter1 + 1, iter1 + 6, iter2), iter1 + 6);
}结果显示:

可以比较由开始和结束迭代器定义的两个序列。
局限性:两个字符串的字母排序是通过从第一个字符开始比较对应字符得到的。第一对不同的对应字符决定了哪个字符串排在首位。字符串的顺序就是不同字符的顺序。
如果字符串的长度相同,而且所有的字符都相等,那么这些字符串就相等。如果字符串的长度不同,短字符串的字符序列和长字符串的初始序列是相同的,那么短字符串小于长字符串。
因此“age” 在“beauty” 之前,“a lull” 在 “a storm” 之前。显然,“the chicken” 而不是 “the egg” 会排在首位。
参数定义:前两个参数定义了第一个序列,第 3 和第 4 个参数分别是第二个序列的开始和结束迭代器。默认用 < 运算符来比较元素,但在需要时,也可以提供一个实现小于比较的函数对象作为可选的第 5 个参数。
返回值:如果第一个序列的字典序小于第二个,这个算法会返回 true,否则返回 false。所以,返回 false 表明第一个序列大于或等于第二个序列。
技巧:序列是逐个元素比较的。第一对不同的对应元素决定了序列的顺序。如果序列的长度不同,而且短序列和长序列的初始元素序列匹配,那么短序列小于长序列。长度相同而且对应元素都相等的两个序列是相等的。空序列总是小于非空序列。
代码实例:
int main()
{
std::vector<string> phrase1 {"the", "tigers", "of", "wrath"};
std::vector<string> phrase2 {"the", "horses", "of", "instruction"};
//这些序列的第二个元素不同,而且“tigers”大于“horses”
auto less = std::lexicographical_compare (std::begin (phrase1), std::end (phrase1),
std::begin(phrase2), std::end(phrase2));
std::copy(std::begin(phrase1), std::end(phrase1), std::ostream_iterator<string>{std::cout, " "});
std::cout << (less ? "are":"are not") << " less than ";
std::copy(std::begin(phrase2), std::end(phrase2), std::ostream_iterator <string>{std::cout, " "});
std::cout << std::endl;
//得到相反的结果
auto less1 = std::lexicographical_compare (std::begin (phrase1), std::end (phrase1),std::begin(phrase2), std::end(phrase2),
[](const string& s1, const string& s2) { return s1.length() < s2.length(); });
}结果显示:
