1 STL算法概述
- STL 提供了大量的算法,用于对容器中的元素进行各种操作,包括排序、搜索、复制、移动、变换等。这些算法在使用时不需要关心容器的具体类型,只需要指定要操作的范围即可。
- 算法主要由头文件
<algorithm> <functional> <numeric>
组成。 <algorithm>
是所有STL头文件中最大的一个,范围涉及到比较、交换、查找、遍历、复制、修改等。<functional>
定义了一些模板类,用于声明函数对象。<numeric>
包含几个在序列上面进行简单数学运算的模板函数。
2 遍历 for_each
- 功能描述:实现遍历容器。
- 函数原型
-
/* * beg: 开始迭代器 * end: 结束迭代器 * _func: 普通函数或函数对象 */ for_each(iterator beg, iterator end, _func);
-
- 使用示例
-
#include <iostream> #include <string> #include <algorithm> #include <functional> #include <vector> void myPrint(int data) { std::cout << data << " "; } class MyPrint2 { public: void operator()(int data) { std::cout << data << " "; } }; int main() { std::vector<int> v; v.push_back(10); v.push_back(20); v.push_back(30); v.push_back(40); v.push_back(50); // for_each遍历 - 使用普通函数 std::for_each(v.begin(), v.end(), myPrint); std::cout << std::endl; // for_each遍历 - 使用仿函数 std::for_each(v.begin(), v.end(), MyPrint2()); std::cout << std::endl; system("pause"); return 0; }
-
- 打印结果
-
10 20 30 40 50 10 20 30 40 50 请按任意键继续. . .
-
3 搬运数据 transform
- 搬运容器中的数据到另一个容器中
- 函数原型
-
/* * beg1: 源容器开始迭代器 * end1: 源容器结束迭代器 * beg2: 目标容器开始迭代器 * _func: 普通函数或函数对象,搬运时可通过该函数进行一些逻辑运算 */ transform(iterator beg1, iterator end1, iterator beg2, _func);
-
- 使用示例
-
#include <iostream> #include <string> #include <algorithm> #include <functional> #include <vector> class Transform1 { public: int operator()(int data) { return data; } }; class Transform2 { public: int operator()(int data) { return data * 10; } }; int main() { std::vector<int> v; v.push_back(10); v.push_back(20); v.push_back(30); v.push_back(40); v.push_back(50); // 搬运数据 std::vector<int> vObj; vObj.resize(v.size()); std::transform(v.begin(), v.end(), vObj.begin(), Transform1()); for (int i = 0; i < vObj.size(); i++) { std::cout << vObj.at(i) << " "; } std::cout << std::endl; // 搬运时进行逻辑运算 std::vector<int> vObj2; vObj2.resize(v.size()); std::transform(v.begin(), v.end(), vObj2.begin(), Transform2()); for (int i = 0; i < vObj2.size(); i++) { std::cout << vObj2.at(i) << " "; } std::cout << std::endl; system("pause"); return 0; }
-
- 打印结果
-
10 20 30 40 50 100 200 300 400 500 请按任意键继续. . .
-
4 查找算法
4.1 查找元素 find
- 查找指定元素。找到返回指定元素的迭代器,未找到返回结束迭代器。
- 函数原型
-
/* * beg: 开始迭代器 * end: 结束迭代器 * value: 查找的元素 */ find(iterator beg, iterator end, value);
-
- 使用示例
-
#include <iostream> #include <string> #include <algorithm> #include <functional> #include <vector> class Person { public: Person(int code, std::string name) { mCode = code; mName = name; } // 查找时要重载== bool operator==(const Person &p) { if (this->mCode == p.mCode && this->mName == p.mName) { return true; } else { return false; } } int mCode; std::string mName; }; int main() { std::vector<int> v; v.push_back(10); v.push_back(20); v.push_back(30); v.push_back(40); v.push_back(50); // 查找 std::vector<int>::iterator iter1 = std::find(v.begin(), v.end(), 30); if (iter1 != v.end()) { std::cout << "*iter1: " << *iter1 << std::endl; } else { std::cout << "not find " << std::endl; } std::vector<Person> v1; Person p1(10010, "jack"); Person p2(10020, "Tom"); Person p3(10030, "Lucy"); Person p4(10040, "Mary"); v1.push_back(p1); v1.push_back(p2); v1.push_back(p3); v1.push_back(p4); // 查找自定义数据类型 std::vector<Person>::iterator iter2 = std::find(v1.begin(), v1.end(), p2); if (iter1 != v.end()) { std::cout << "code: "<< iter2->mCode << ", name "<< iter2->mName << std::endl; } else { std::cout << "not find " << std::endl; } system("pause"); return 0; }
-
- 打印结果
-
*iter1: 30 code: 10020, name Tom 请按任意键继续. . .
-
4.2 根据条件查找 find_if
- 按条件查找元素。找到返回指定位置迭代器,找不到返回结束位置迭代器。
- 函数原型
-
/* * beg: 开始迭代器 * end: 结束迭代器 * _pred: _pred函数或谓词 */ find_if(iterator beg, iterator end, _pred);
-
- 使用示例
-
#include <iostream> #include <string> #include <algorithm> #include <functional> #include <vector> class Person { public: Person(int code, std::string name) { mCode = code; mName = name; } int mCode; std::string mName; }; class GreaterTwo { public: bool operator()(int data) { return data > 2; } }; class Greater10020 { public: bool operator()(const Person &p) { return p.mCode > 10020; } }; int main() { std::vector<int> v; v.push_back(1); v.push_back(2); v.push_back(3); v.push_back(4); v.push_back(5); // 查找大于2的数 std::vector<int>::iterator iter1 = std::find_if(v.begin(), v.end(), GreaterTwo()); if (iter1 != v.end()) { std::cout << "*iter1: " << *iter1 << std::endl; } else { std::cout << "not find " << std::endl; } std::vector<Person> v1; Person p1(10010, "jack"); Person p2(10020, "Tom"); Person p3(10030, "Lucy"); Person p4(10040, "Mary"); v1.push_back(p1); v1.push_back(p2); v1.push_back(p3); v1.push_back(p4); // 查找code大于10020的数据 std::vector<Person>::iterator iter2 = std::find_if(v1.begin(), v1.end(), Greater10020()); if (iter1 != v.end()) { std::cout << "code: "<< iter2->mCode << ", name "<< iter2->mName << std::endl; } else { std::cout << "not find " << std::endl; } system("pause"); return 0; }
-
- 打印结果
-
iter1: 3 code: 10030, name Lucy 请按任意键继续. . .
-
4.3 查找相邻重复元素 adjacent_find
- 查找相邻重复元素,返回相邻元素的第一个位置的迭代器。
- 函数原型
-
/* * beg: 开始迭代器 * end: 结束迭代器 */ adjacent_find(iterator beg, iterator end);
-
- 使用示例
-
#include <iostream> #include <string> #include <algorithm> #include <functional> #include <vector> int main() { std::vector<int> v; v.push_back(0); v.push_back(1); v.push_back(2); v.push_back(2); v.push_back(3); v.push_back(1); v.push_back(5); // 查找相邻重复元素 std::vector<int>::iterator iter1 = std::adjacent_find(v.begin(), v.end()); if (iter1 != v.end()) { std::cout << "*iter1: " << *iter1 << std::endl; } else { std::cout << "not find " << std::endl; } system("pause"); return 0; }
-
- 打印结果
-
*iter1: 2 请按任意键继续. . .
-
4.4 查找指定元素 binary_search
- 二分法查找指定元素是否存在,存在返回true,不存在返回false。
- 容器必须是一个有序序列,如果是无序序列,则结果未知。
- 函数原型
-
/* * beg: 开始迭代器 * end: 结束迭代器 * value: 查找的元素 */ bool binary_search(iterator beg, iterator end, value);
-
- 使用示例
-
#include <iostream> #include <string> #include <algorithm> #include <functional> #include <vector> int main() { std::vector<int> v; v.push_back(10); v.push_back(200); v.push_back(55); v.push_back(2); v.push_back(666); v.push_back(9); v.push_back(5000); // 无序序列先排序 std::sort(v.begin(), v.end()); // 查找指定元素 bool ret = std::binary_search(v.begin(), v.end(), 55); if (ret) { std::cout << "find success. " << std::endl; } else { std::cout << "find failed. " << std::endl; } system("pause"); return 0; }
-
- 打印结果
-
find success. 请按任意键继续. . .
-
5 统计元素
5.1 统计元素个数 count
- 统计元素个数
- 函数原型
-
/* * beg: 开始迭代器 * end: 结束迭代器 * value: 统计的元素 */ count(iterator beg, iterator end, value);
-
- 使用示例
-
#include <iostream> #include <string> #include <algorithm> #include <functional> #include <vector> class Person { public: Person(int code, std::string name) { mCode = code; mName = name; } // 查找名字相同的人个数 bool operator==(const Person& p) { return this->mName == p.mName; } int mCode; std::string mName; }; int main() { std::vector<int> v; v.push_back(10); v.push_back(20); v.push_back(20); v.push_back(30); v.push_back(20); v.push_back(50); // 查找有几个20 std::cout << "20 count: " << std::count(v.begin(), v.end(), 20) << std::endl; std::vector<Person> v1; Person p1(10010, "jack"); Person p2(10020, "mary"); Person p3(10030, "jack"); Person p4(10040, "tom"); Person p5(10050, "lucy"); v1.push_back(p1); v1.push_back(p2); v1.push_back(p3); v1.push_back(p4); v1.push_back(p5); // 查找和jack同名的人有几个 Person pp(10070, "jack"); std::cout << "pp count: " << std::count(v1.begin(), v1.end(), pp) << std::endl; system("pause"); return 0; }
-
- 打印结果
-
20 count: 3 pp count: 2 请按任意键继续. . .
-
5.2 按条件统计元素个数 count_if
- 按条件统计元素个数
- 函数原型
-
/* * beg: 开始迭代器 * end: 结束迭代器 * _pred: 谓词 */ count_if(iterator beg, iterator end, _pred);
-
6 排序算法
6.1 排序 sort
- 对容器内元素进行排序
- 函数原型
-
/* * beg: 开始迭代器 * end: 结束迭代器 * _pred: 谓词 */ sort(iterator beg, iterator end, _pred);
-
- 使用示例
-
#include <iostream> #include <string> #include <algorithm> #include <functional> #include <vector> int main() { std::vector<int> v; v.push_back(10); v.push_back(100); v.push_back(5); v.push_back(999); v.push_back(1024); v.push_back(50); // 默认为升序 std::sort(v.begin(), v.end()); for (int i = 0; i < v.size(); i++) { std::cout << v.at(i) << " "; } std::cout << std::endl; // 降序排列 std::sort(v.begin(), v.end(), std::greater<int>()); for (int i = 0; i < v.size(); i++) { std::cout << v.at(i) << " "; } std::cout << std::endl; system("pause"); return 0; }
-
- 打印结果
-
5 10 50 100 999 1024 1024 999 100 50 10 5 请按任意键继续. . .
-
6.2 打乱数据 random_shuffle
- 洗牌,指定范围内的元素随机调整次序。
- 函数原型
-
/* * beg: 开始迭代器 * end: 结束迭代器 */ random_shuffle(iterator beg, iterator end);
-
- 使用示例
-
#include <iostream> #include <string> #include <algorithm> #include <functional> #include <vector> #include <ctime> int main() { std::vector<int> v; for (int i = 0; i < 10; i++) { v.push_back(i); } for (int i = 0; i < v.size(); i++) { std::cout << v.at(i) << " "; } std::cout << std::endl; // 设置随机种子 srand((unsigned int)time(NULL)); // 打乱数据 std::random_shuffle(v.begin(), v.end()); for (int i = 0; i < v.size(); i++) { std::cout << v.at(i) << " "; } std::cout << std::endl; system("pause"); return 0; }
-
- 打印结果
-
0 1 2 3 4 5 6 7 8 9 3 7 5 2 9 1 6 0 4 8 请按任意键继续. . .
-
6.3 元素合并 merge
- 容器元素合并,并存储到另一容器中。两个容器必须是有序的。
- 函数原型
-
/* * beg1: 容器1开始迭代器 * end1: 容器1结束迭代器 * beg2: 容器2开始迭代器 * end2: 容器2结束迭代器 * dest: 目标容器开始迭代器 */ merge(iterator beg1, iterator end1, iterator beg2, iterator end2, iterator dest);
-
- 使用示例
-
#include <iostream> #include <string> #include <algorithm> #include <functional> #include <vector> #include <ctime> int main() { std::vector<int> v1; std::vector<int> v2; for (int i = 0; i < 10; i++) { v1.push_back(i); v2.push_back(i + 5); } std::cout << "v1: "; for (int i = 0; i < v1.size(); i++) { std::cout << v1.at(i) << " "; } std::cout << std::endl; std::cout << "v1: "; for (int i = 0; i < v2.size(); i++) { std::cout << v2.at(i) << " "; } std::cout << std::endl; // 合并容器 std::vector<int> vobj; vobj.resize(v1.size() + v2.size()); // 开辟空间 std::merge(v1.begin(), v1.end(), v2.begin(), v2.end(), vobj.begin()); std::cout << "vobj: "; for (int i = 0; i < vobj.size(); i++) { std::cout << vobj.at(i) << " "; } std::cout << std::endl; system("pause"); return 0; }
-
- 打印结果
-
v1: 0 1 2 3 4 5 6 7 8 9 v1: 5 6 7 8 9 10 11 12 13 14 vobj: 0 1 2 3 4 5 5 6 6 7 7 8 8 9 9 10 11 12 13 14
-
6.4 反转 reverse
- 反转指定范围的元素。
- 函数原型
-
/* * beg: 开始迭代器 * end: 结束迭代器 */ reverse(iterator beg, iterator end);
-
- 使用示例
-
#include <iostream> #include <string> #include <algorithm> #include <functional> #include <vector> int main() { std::vector<int> v1; for (int i = 0; i < 10; i++) { v1.push_back(i); } std::cout << "v1: "; for (int i = 0; i < v1.size(); i++) { std::cout << v1.at(i) << " "; } std::cout << std::endl; // 反转数据 std::reverse(v1.begin(), v1.end()); std::cout << "reverse v1: "; for (int i = 0; i < v1.size(); i++) { std::cout << v1.at(i) << " "; } std::cout << std::endl; system("pause"); return 0; }
-
- 打印结果
-
v1: 0 1 2 3 4 5 6 7 8 9 reverse v1: 9 8 7 6 5 4 3 2 1 0 请按任意键继续. . .
-
7 拷贝和替换
7.1 拷贝 copy
- 容器指定范围内的元素拷贝到另一容器中。
- 函数原型
-
/* * beg: 开始迭代器 * end: 结束迭代器 * dest: 目标容器开始迭代器 */ copy(iterator beg, iterator end, iterator dest);
-
- 使用示例
-
#include <iostream> #include <string> #include <algorithm> #include <functional> #include <vector> int main() { std::vector<int> v1; v1.push_back(10); v1.push_back(30); v1.push_back(-2); v1.push_back(-5); v1.push_back(99); // 拷贝数据 std::vector<int> v2; v2.resize(v1.size()); std::copy(v1.begin(), v1.end(), v2.begin()); std::cout << "v2: "; for (int i = 0; i < v2.size(); i++) { std::cout << v2.at(i) << " "; } std::cout << std::endl; system("pause"); return 0; }
-
- 打印结果
-
v2: 10 30 -2 -5 99 请按任意键继续. . .
-
7.2 替换 replace
- 将容器内指定范围的旧元素修改为新元素。
- 函数原型
-
/* * beg: 开始迭代器 * end: 结束迭代器 * oldvalue: 旧元素 * newvalue: 新元素 */ replace(iterator beg, iterator end, oldvalue, newvalue);
-
- 使用示例
-
#include <iostream> #include <string> #include <algorithm> #include <functional> #include <vector> int main() { std::vector<int> v1; v1.push_back(10); v1.push_back(30); v1.push_back(-2); v1.push_back(256); v1.push_back(80); v1.push_back(256); v1.push_back(99); std::cout << "before v1: "; for (int i = 0; i < v1.size(); i++) { std::cout << v1.at(i) << " "; } std::cout << std::endl; // 将256替换为1024 std::replace(v1.begin(), v1.end(), 256, 1024); std::cout << "after v1: "; for (int i = 0; i < v1.size(); i++) { std::cout << v1.at(i) << " "; } std::cout << std::endl; system("pause"); return 0; }
-
- 打印结果
-
before v1: 10 30 -2 256 80 256 99 after v1: 10 30 -2 1024 80 1024 99 请按任意键继续. . .
-
7.3 按条件替换 replace_if
- 将容器内满足条件的元素替换成指定元素。
- 函数原型
-
/* * beg: 开始迭代器 * end: 结束迭代器 * _pred: 谓词 * newvalue: 新元素 */ replace_if(iterator beg, iterator end, _pred, newvalue);
-
- 使用示例
-
#include <iostream> #include <string> #include <algorithm> #include <functional> #include <vector> class Greater50 { public: bool operator()(int data) { return data > 50; } }; int main() { std::vector<int> v1; v1.push_back(10); v1.push_back(30); v1.push_back(-2); v1.push_back(256); v1.push_back(80); v1.push_back(20); v1.push_back(256); v1.push_back(99); std::cout << "before v1: "; for (int i = 0; i < v1.size(); i++) { std::cout << v1.at(i) << " "; } std::cout << std::endl; // 大于50的数据替换为1024 std::replace_if(v1.begin(), v1.end(), Greater50(), 1024); std::cout << "after v1: "; for (int i = 0; i < v1.size(); i++) { std::cout << v1.at(i) << " "; } std::cout << std::endl; system("pause"); return 0; }
-
- 打印结果
-
before v1: 10 30 -2 256 80 20 256 99 after v1: 10 30 -2 1024 1024 20 1024 1024 请按任意键继续. . .
-
7.4 互换元素 swap
- 互换两个容器的元素。两个容器必须是同一种类型。
- 函数原型
-
/* * c1: 容器c1 * c2: 容器c2 */ swap(container c1, container c2);
-
- 使用示例
-
#include <iostream> #include <string> #include <algorithm> #include <functional> #include <vector> class Greater50 { public: bool operator()(int data) { return data > 50; } }; int main() { std::vector<int> v1; v1.push_back(1); v1.push_back(2); v1.push_back(3); v1.push_back(4); std::vector<int> v2; v2.push_back(10); v2.push_back(20); v2.push_back(30); v2.push_back(40); v2.push_back(50); v2.push_back(60); std::cout << "before: " << std::endl; std::cout << "v1: "; for (int i = 0; i < v1.size(); i++) { std::cout << v1.at(i) << " "; } std::cout << std::endl; std::cout << "v2: "; for (int i = 0; i < v2.size(); i++) { std::cout << v2.at(i) << " "; } std::cout << std::endl; // 互换元素 std::swap(v1, v2); std::cout << "after: " << std::endl; std::cout << "v1: "; for (int i = 0; i < v1.size(); i++) { std::cout << v1.at(i) << " "; } std::cout << std::endl; std::cout << "v2: "; for (int i = 0; i < v2.size(); i++) { std::cout << v2.at(i) << " "; } std::cout << std::endl; system("pause"); return 0; }
-
- 打印结果
-
before: v1: 1 2 3 4 v2: 10 20 30 40 50 60 after: v1: 10 20 30 40 50 60 v2: 1 2 3 4
-
8 算数生成算法
- 算数生成算法属于小型算法,使用时包含的头文件为
#include <numeric>
8.1 求和 accumulate
- 容器内指定区间所有元素求和。
- 函数原型
-
/* * beg: 开始迭代器 * end: 结束迭代器 * value: 起始值。在此数基础上累加。 */ accumulate(iterator beg, iterator end, value);
-
- 使用示例
-
#include <iostream> #include <string> #include <numeric> #include <vector> int main() { std::vector<int> v1; v1.push_back(10); v1.push_back(-2); v1.push_back(5); v1.push_back(-4); v1.push_back(8); int sum = std::accumulate(v1.begin(), v1.end(), 0); std::cout << "sum: " << sum <<std::endl; system("pause"); return 0; }
-
- 打印结果
-
sum: 17 请按任意键继续. . .
-
8.2 填充元素 fill
- 将容器中指定范围的元素填充为指定值。
- 函数原型
-
/* * beg: 开始迭代器 * end: 结束迭代器 * value: 填充的值。 */ fill(iterator beg, iterator end, value);
-
- 使用示例
-
#include <iostream> #include <string> #include <numeric> #include <vector> int main() { std::vector<int> v1; v1.resize(8); // 填充元素 std::fill(v1.begin(), v1.end(), 256); std::cout << "v1: "; for (int i = 0; i < v1.size(); i++) { std::cout << v1.at(i) << " "; } std::cout << std::endl; system("pause"); return 0; }
-
- 打印结果
-
v1: 256 256 256 256 256 256 256 256 请按任意键继续. .
-
9 集合算法
9.1 求交集 set_intersection
- 求两个容器的交集。两个容器必须是有序序列。
- 函数原型
-
/* * beg1: 容器1开始迭代器 * end1: 容器1结束迭代器 * beg1: 容器2开始迭代器 * end1: 容器2结束迭代器 * objbeg: 目标容器开始迭代器 * 返回交集结束迭代器 */ iterator set_intersection(iterator beg1, iterator end1, iterator beg2, iterator end2, iterator objbeg);
-
- 使用示例
-
#include <iostream> #include <string> #include <algorithm> #include <vector> int main() { std::vector<int> v1; v1.push_back(1); v1.push_back(2); v1.push_back(3); v1.push_back(4); v1.push_back(5); std::vector<int> v2; v2.push_back(3); v2.push_back(4); v2.push_back(5); v2.push_back(6); v2.push_back(7); std::vector<int> vobj; // 开辟大小为小容器的大小 vobj.resize(std::min(v1.size(), v2.size())); // 求交集 std::vector<int>::iterator itEnd = std::set_intersection(v1.begin(), v1.end(), v2.begin(), v2.end(), vobj.begin()); std::cout << "vobj: "; for (std::vector<int>::iterator iter = vobj.begin(); iter != itEnd; iter++) { std::cout << *iter << " "; } std::cout << std::endl; system("pause"); return 0; }
-
- 打印结果
-
vobj: 3 4 5 请按任意键继续. . .
-
9.2 求并集 set_union
- 求两个容器的并集。两个容器必须是有序序列。
- 函数原型
-
/* * beg1: 容器1开始迭代器 * end1: 容器1结束迭代器 * beg1: 容器2开始迭代器 * end1: 容器2结束迭代器 * objbeg: 目标容器开始迭代器 * 返回并集结束迭代器 */ iterator set_union(iterator beg1, iterator end1, iterator beg2, iterator end2, iterator objbeg);
-
- 使用示例
-
#include <iostream> #include <string> #include <algorithm> #include <vector> int main() { std::vector<int> v1; v1.push_back(1); v1.push_back(2); v1.push_back(3); v1.push_back(4); v1.push_back(5); std::vector<int> v2; v2.push_back(3); v2.push_back(4); v2.push_back(5); v2.push_back(6); v2.push_back(7); std::vector<int> vobj; // 开辟大小为两个容器的总大小 vobj.resize(v1.size() + v2.size()); // 求并集 std::vector<int>::iterator itEnd = std::set_union(v1.begin(), v1.end(), v2.begin(), v2.end(), vobj.begin()); std::cout << "vobj: "; for (std::vector<int>::iterator iter = vobj.begin(); iter != itEnd; iter++) { std::cout << *iter << " "; } std::cout << std::endl; system("pause"); return 0; }
-
- 打印结果
-
vobj: 1 2 3 4 5 6 7 请按任意键继续. . .
-
9.3 求差集 set_difference
- 求两个容器的差集(不属于交集的部分)。两个容器必须是有序序列。
- 函数原型
-
/* * beg1: 容器1开始迭代器 * end1: 容器1结束迭代器 * beg1: 容器2开始迭代器 * end1: 容器2结束迭代器 * objbeg: 目标容器开始迭代器 * 返回差集结束迭代器 */ iterator set_difference(iterator beg1, iterator end1, iterator beg2, iterator end2, iterator objbeg);
-
- 使用示例
-
#include <iostream> #include <string> #include <algorithm> #include <vector> int main() { std::vector<int> v1; v1.push_back(1); v1.push_back(2); v1.push_back(3); v1.push_back(4); v1.push_back(5); std::vector<int> v2; v2.push_back(3); v2.push_back(4); v2.push_back(5); v2.push_back(6); v2.push_back(7); std::vector<int> vobj; // 开辟大小为大容器的大小 vobj.resize(std::max(v1.size(), v2.size())); // 求v1和v2差集 std::vector<int>::iterator itEnd; itEnd = std::set_difference(v1.begin(), v1.end(), v2.begin(), v2.end(), vobj.begin()); std::cout << "vobj: "; for (std::vector<int>::iterator iter = vobj.begin(); iter != itEnd; iter++) { std::cout << *iter << " "; } std::cout << std::endl; // 求v2和v1差集 itEnd = std::set_difference(v2.begin(), v2.end(), v1.begin(), v1.end(), vobj.begin()); std::cout << "vobj: "; for (std::vector<int>::iterator iter = vobj.begin(); iter != itEnd; iter++) { std::cout << *iter << " "; } std::cout << std::endl; system("pause"); return 0; }
-
- 打印结果
-
vobj: 1 2 vobj: 6 7 请按任意键继续. . .
-