equal
①.头文件
equal 算法定义在头文件 #include 中。
②.算法作用
equal 算法作用是比较指定范围的两个序列的各元素是否相同。
③.代码示例
vector<int> vec1 = { 1,2,3,4,5,6 };
vector<int> vec2 = { 4,5,6,7,8,9 };
cout << "原序列 vec1:" << endl;
for_each(vec1.begin(), vec1.end(), [](int i) {cout << i << " "; });
cout << endl << "原序列 vec2:" << endl;
for_each(vec2.begin(), vec2.end(), [](int i) {cout << i << " "; });
cout << endl << "比较结果:"<< boolalpha << equal(vec1.begin(), vec1.end(), vec2.begin());
mismatch
①.头文件
mismatch 算法定义在头文件 #include 中。
②.算法作用
mismatch 算法作用是比较指定范围的两个序列的各元素是否相同,并返回一组迭代器,分别指向两个序列中第一个不匹配的位置。
③.代码示例
vector<int> vec1 = { 1,2,3,4,5 };
vector<int> vec2 = { 1,2,6,7,8 };
cout << "原序列 vec1:" << endl;
for_each(vec1.begin(), vec1.end(), [](int i) {cout << i << " "; });
cout << endl << "原序列 vec2:" << endl;
for_each(vec2.begin(), vec2.end(), [](int i) {cout << i << " "; });
auto p = mismatch(vec1.begin(), vec1.end(), vec2.begin());
cout << endl << "vec1 第一个不匹配的值:"<< * p.first ;
cout << endl << "vec2 第一个不匹配的值:" << *p.second << endl;
lexicographical_compare
①.头文件
lexicographical_compare算法定义在头文件 #include 中。
②.算法作用
lexicographical_compare 算法作用是判断序列1的字典序是否小于序列2。
③.代码示例
vector<int> vec1 = { 1,2,3,4,5 };
vector<int> vec2 = { 5,6,7 };
cout << "原序列 vec1:" << endl;
for_each(vec1.begin(), vec1.end(), [](int i) {cout << i << " "; });
cout << endl << "原序列 vec2:" << endl;
for_each(vec2.begin(), vec2.end(), [](int i) {cout << i << " "; });
cout << endl << "比较结果:" << boolalpha;
cout << lexicographical_compare(vec1.begin(), vec1.end(), vec2.begin(), vec2.end()) << endl;
includes
①.头文件
includes 算法定义在头文件 #include 中。
②.算法作用
includes 算法作用是比较两个有序的序列,判断序列2中的元素是否全部在序列1中包含。
③.代码示例
vector<int> vec1 = { 1,2,3,4,5 };
vector<int> vec2 = { 2,5};
cout << "原序列 vec1:" << endl;
for_each(vec1.begin(), vec1.end(), [](int i) {cout << i << " "; });
cout << endl << "原序列 vec2:" << endl;
for_each(vec2.begin(), vec2.end(), [](int i) {cout << i << " "; });
cout << endl << "比较结果:" << boolalpha << includes(vec1.begin(), vec1.end(), vec2.begin(), vec2.end()) << endl;
all_of
①.头文件
all_of 算法定义在头文件 #include 中。
②.算法作用
all_of 算法作用是遍历序列中的所有元素,判断是否全部满足指定的条件。另外两个算法是 any_of、none_of。
③.代码示例
vector<int> vec1 = { 1,2,3,4,5 };
cout << "原序列 vec1:" << endl;
for_each(vec1.begin(), vec1.end(), [](int i) {cout << i << " "; });
cout << endl << "判断结果:" << boolalpha;
cout<< all_of(vec1.begin(), vec1.end(), [](int i) {return i > 0; }) << endl;