重新系统学习c++语言,并将学习过程中的知识在这里抄录、总结、沉淀。同时希望对刷到的朋友有所帮助,一起加油哦!

每一次学习都是为了追求智慧!

写在前面,本篇章主要介绍STL中常用算法。

算法主要由头文件<algorithm><functional><numeric>组成。

<algorithm>是所有STL头文件中最大的一个,范围包括比较、交换、查找、遍历、复制、修改等。

<functional>定义了一些模版类,常用的仿函数等。

<numeric>体积很小,只包括几个在序列上面进行简单数学运算的模版函数。

1.1 常用遍历算法

1.1.1 for_each

能力:

        遍历容器的算法

函数原型:

        for_each(iterator begin, iterator end, _func);

begin: 开始迭代器

end: 结束迭代器

_func: 函数或函数对象(即仿函数)

上面_func参数写法:

如果是普通函数:就写  函数名

如果是仿函数:需要写  类名()

示例:

#include <iostream>
#include <string>
#include<vector>
#include<algorithm>
using namespace std; void print1(int val) {
cout << val << " ";
} class print2 {
public:
void operator()(int val) {
cout << val << " ";
}
}; void test() {
vector<int> v;
for (int i = 0; i < 10; i++) {
v.push_back(i);
} // 利用普通和函数 print1
for_each(v.begin(), v.end(), print1);
cout << endl; // 利用仿函数 print2()
for_each(v.begin(), v.end(), print2());
} int main() {
test(); system("pause");
return 0;
}

1.1.2 transform

能力:

搬运容器到另一个容器中

函数原型:

transform(iterator begin1, iterator end1, iterator begin2, _func);

begin1:源容器开始迭代器

end1: 源容器结束迭代器

begin2: 目标容器开始迭代器

_func: 普通函数或仿函数

注意:

        在使用transform搬运容器之前,必须要给目标容器提前开辟空间,不然会报错。通常用 resize()

示例:

#include <iostream>
#include <string>
#include<vector>
#include<algorithm>
using namespace std; class Transform {
public:
int operator()(int val) {
return val + 100;
}
}; class MyPrint{
public:
void operator()(int val) {
cout << val << " ";
}
}; void test() {
vector<int> v;
for (int i = 0; i < 5; i++) {
v.push_back(i);
} vector<int> v2;//目标容器
v2.resize(v.size());// 必须要给目标容器提前开辟空间,开辟搬运容器空间的大小即可。
transform(v.begin(), v.end(), v2.begin(), Transform()); for_each(v2.begin(), v2.end(), MyPrint());
cout << endl;
} int main() {
test(); system("pause");
return 0;
}

1.2 常用查找算法

主要包含:

  • find                             查找元素
  • find_if                         按条件查找元素
  • adjacent_find           查找相邻重复元素
  • binary_search          二分查找法
  • count                        统计元素个数
  • count_if                   按条件统计元素个数

1.2.1 find

能力:

        查找指定元素,若找到,返回指定元素迭代器;若未找到,返回结束迭代器end().

函数原型:

find(iterator begin, iterator end, value);

begin: 开始迭代器

end: 结束迭代器

value: 要查找的元素

示例:

#include <iostream>
#include <string>
#include<vector>
#include<algorithm>
using namespace std; // 常用算法 find // find查找内置数据类型
void test() {
vector<int> v;
for (int i = 0; i < 5; i++) {
v.push_back(i);
} vector<int>::iterator pos = find(v.begin(), v.end(), 2);
if (pos != v.end()) {
cout << "find " << *pos << endl;
}
else {
cout << "not find 2" << endl;
} pos = find(v.begin(), v.end(), 10);
if (pos != v.end()) {
cout << "find " << *pos << endl;
}
else {
cout << "not find 10" << endl;
} } // 查找自定义数据类型
class Person {
public:
Person(string name, int age) {
this->m_name = name;
this->m_age = age;
} // 需要重载==,才能判断查找的值
bool operator==(const Person& p) {
if (this->m_name == p.m_name && this->m_age == p.m_age) {
return true;
}
else {
return false;
}
}
string m_name;
int m_age;
};
void test2() {
vector<Person> v;
v.push_back(Person("echo", 20));
v.push_back(Person("tom", 19));
v.push_back(Person("lucy", 18)); Person p("tom", 19); vector<Person>::iterator pos = find(v.begin(), v.end(), p);
if (pos !=v.end()) {
cout << " find p name = "<<( * pos).m_name
<<" age = "<<pos->m_age
<< endl;
}
else {
cout << "not find p" << endl;
}
} int main() {
test2(); system("pause");
return 0;
}

1.2.2 find_if

能力:

        按条件查找元素。

按值查找元素,若找到,返回指定元素迭代器,若未找到,返回结束迭代器。

函数原型:

find_if(iterator begin, iterator end, _pred);

begin: 开始迭代器

end: 结束迭代器

_pred: 普通函数或仿函数(需要是返回bool类型的函数)

示例:

#include <iostream>
#include <string>
#include<vector>
#include<algorithm>
// find_if using namespace std; // 查找内置数据类型
bool GreaterFive(int a) {
return a > 5;
} void test() {
vector<int> v;
for (int i = 0; i < 10; i++) {
v.push_back(i);
} vector<int>::iterator pos=find_if(v.begin(), v.end(), GreaterFive); if (pos != v.end()) {
cout << "找到大于5的元素:" << *pos << endl;
}
else {
cout << "未找到" << endl;
}
} //自定义数据类型
class Person {
public:
Person(string name, int age) {
this->m_name = name;
this->m_age = age;
} string m_name;
int m_age;
}; // 仿函数
class GreaterAge18 {
public:
bool operator()(const Person& p) {
// 年龄大于18岁
return p.m_age > 18;
}
}; void test2() {
vector<Person> v;
v.push_back(Person("tom", 18));
v.push_back(Person("echo", 20));
v.push_back(Person("lucy", 19)); vector<Person>::iterator pos = find_if(v.begin(), v.end(), GreaterAge18());
if (pos != v.end()) {
cout << "找到年龄大于18的人。姓名:" << pos->m_name
<< " 年龄:" << pos->m_age << endl;
}
else {
cout << "未找到" << endl;
}
} int main() {
// test();
test2(); system("pause");
return 0;
}

1.2.3 adjacent_find

能力:

        查找相邻重复元素,若存在,返回指定位置迭代器,若不存在,返回迭代器end()。

函数原型:

adjacent_find(iterator begin, iterator end);

begin: 开始迭代器

end: 结束迭代器

示例:

#include <iostream>
#include <string>
#include<vector>
#include<algorithm>
using namespace std; // 查找相邻重复元素 adjacent_find
void test() {
vector<int> v;
v.push_back(0);
v.push_back(1);
v.push_back(0);
v.push_back(3);
v.push_back(4);
v.push_back(3);
v.push_back(3); vector<int>::iterator pos = adjacent_find(v.begin(), v.end());
if (pos != v.end()) {
cout << "查找到相邻元素重复:" << *pos << endl;
}
else {
cout << "未找到相邻重复元素" << endl;
} } int main() {
test(); system("pause");
return 0;
}

1.2.4 binary_search

能力:

        查找指定元素是否存在。若存在,返回true。若不存在,返回false。

注意:

        只能在有序序列才可用。

函数原型:

bool binary_search(iterator begin, iterator end, value);

begin: 开始迭代器

end: 结束迭代器

value: 要查找的元素

示例:

#include <iostream>
#include <string>
#include<vector>
#include<algorithm>
using namespace std; // binary_search 查找元素是否存在
void test() {
vector<int> v;
v.push_back(1);
v.push_back(3);
v.push_back(2);
v.push_back(5);
v.push_back(4); // 只有在有序序列中才是准确的,如果没有sort排序测试出 未找到
sort(v.begin(), v.end());
bool ret = binary_search(v.begin(), v.end(), 3);
if (ret) {
cout << "找到元素" << endl;
}
else {
cout << "未找到元素" << endl;
}
} int main() {
test(); system("pause");
return 0;
}

1.2.5 count

能力:

统计元素个数

函数原型:

count(iterator begin, iterator end, value);

begin: 开始迭代器

end: 结束迭代器

value: 要统计的元素

示例:

#include <iostream>
#include <string>
#include<vector>
#include<algorithm>
using namespace std;
// count 统计 // 内置数据类型
void test() {
vector<int> v;
v.push_back(1);
v.push_back(1);
v.push_back(2);
v.push_back(3);
v.push_back(1); int n=count(v.begin(), v.end(), 1);
cout << "容器中1的个数为:" << n << endl;
} // 自定义数据类型
class Person {
public:
Person(string name, int age) {
this->m_name = name;
this->m_age = age;
} bool operator==(const Person& p) {
if ( this->m_name == p.m_name && this->m_age == p.m_age) {
return true;
}
else {
return false;
}
} string m_name;
int m_age;
};
void test2() {
vector<Person> v;
v.push_back(Person("test1", 10));
v.push_back(Person("test2", 20));
v.push_back(Person("test1", 30));
v.push_back(Person("test1", 10)); Person p("test1", 10); int n = count(v.begin(), v.end(), p);
cout << "name = test1, age = 10 的元素个数为:" << n << endl;
} int main() {
//test();
test2(); system("pause");
return 0;
}

1.2.6 count_if

能力:

按条件统计元素个数。

函数原型:

count_if(iterator begin, iterator end, _pred);

begin: 开始迭代器

end: 结束迭代器

_pred: 谓词

示例:

#include <iostream>
#include <string>
#include<vector>
#include<algorithm>
using namespace std;
// count 统计 // 内置数据类型
class Greater3 {
public:
bool operator()(int val) {
return val > 3;
}
}; void test() {
vector<int> v;
v.push_back(1);
v.push_back(2);
v.push_back(3);
v.push_back(4);
v.push_back(5); int n = count_if(v.begin(), v.end(), Greater3());
cout << "容器中大于3个数为:" << n << endl;
} // 自定义数据类型
class Person {
public:
Person(string name, int age) {
this->m_name = name;
this->m_age = age;
} string m_name;
int m_age;
}; class GreaterAge20 {
public:
bool operator()(const Person& p) {
// 年龄大于20
return p.m_age > 20;
}
}; void test2() {
vector<Person> v;
v.push_back(Person("test1", 10));
v.push_back(Person("test2", 20));
v.push_back(Person("test3", 30));
v.push_back(Person("test4", 50));
v.push_back(Person("test5", 40)); Person p("test1", 10); int n = count_if(v.begin(), v.end(), GreaterAge20());
cout << "年龄大于20的元素个数为:" << n << endl;
} int main() {
//test();
test2(); system("pause");
return 0;
}

1.3 常用排序算法

1.3.1 sort

能力:

        对容器内元素排序

函数原型:

sort(iterator begin, iterator end, _pred);

begin: 开始迭代器

end: 结束迭代器

_pred:谓词

示例:

#include <iostream>
#include <string>
#include<vector>
#include<algorithm>
using namespace std; void print(int val) {
cout << val << " ";
} void test() {
vector<int> v;
v.push_back(1);
v.push_back(5);
v.push_back(4);
v.push_back(2);
v.push_back(3); // 利用sort 默认升序
sort(v.begin(), v.end());
for_each(v.begin(), v.end(), print);
cout << endl; // 改为降序
sort(v.begin(), v.end(), greater<int>());
for_each(v.begin(), v.end(), print);
} int main() {
test(); system("pause");
return 0;
}

1.3.2 random_shuffe

能力:

也叫洗牌。将指定范围内的元素随机打乱次序。

函数原型:

        random_shuffle(iterator begin, iterator end);

//也叫洗牌。将指定范围内的元素随机打乱次序。

begin: 开始迭代器

end: 结束迭代器

注意:

使用时,需要加随机数种子,才能真实的随机起来,每次不一样。

#include<ctime>

srand((unsigned int)time(NULL));

示例:

#include <iostream>
#include <string>
#include<vector>
#include<algorithm>
#include<ctime> using namespace std; // random_shuffle 洗牌 将制定范围内元素打乱次序 void print(int val) {
cout << val << " ";
} void test() {
// 随机数种子,让random_shuffle真实的随机起来
srand((unsigned int)time(NULL)); vector<int> v;
for (int i = 0; i < 10; i++) {
v.push_back(i);
} for_each(v.begin(), v.end(), print);
cout << endl; // 洗牌打乱顺序
random_shuffle(v.begin(), v.end()); for_each(v.begin(), v.end(), print);
} int main() {
test(); system("pause");
return 0;
}

输出:

0 1 2 3 4 5 6 7 8 9

8 1 9 2 0 5 7 3 4 6

1.3.3 merge

能力:

两个有序容器元素合并,并存储到目标容器中。

注意:

        两个容器必须是有序的,才能合并。

两个容器的排序必须是一致的,都是升序或降序。

需要先给目标容器开辟空间。

函数原型:

        merge(iterator begin1, iterator end1, iterator begin2, iterator end2, iterator target_begin);

begin1: 容器1开始迭代器

end1: 容器1结束迭代器

begin2:容器2开始迭代器

end2:容器2结束迭代器

target_begin:目标容器开始迭代器

示例:

#include <iostream>
#include <string>
#include<vector>
#include<algorithm>
using namespace std; // merge 将两个有序容器元素合并到一个目标容器中
void print(int val) {
cout << val << " ";
} void test() {
vector<int> v1;
vector<int> v2; // 两个容器需要是有序,且排序一致,都是升序或降序
for (int i = 0; i < 10; i++) {
v1.push_back(i);
v2.push_back(100 + i);
// v2.push_back(100 - i); // 程序会崩溃
} vector<int> v3;
// 需要提前给目标容器分配空间
v3.resize(v1.size() + v2.size()); merge(v1.begin(), v1.end(), v2.begin(), v2.end(), v3.begin()); for_each(v3.begin(), v3.end(), print);
} int main() {
test(); system("pause");
return 0;
}

1.3.4 reverse

能力:

将容器元素进行反转。

函数原型:

reverse(iterator begin, iterator end);

begin: 容器1开始迭代器

end: 容器1结束迭代器

示例:

#include <iostream>
#include <string>
#include<vector>
#include<algorithm>
using namespace std; // reverse 将容器元素反转 // 将string内容反转
void test() {
string a = "abcdefg"; reverse(a.begin(), a.end()); cout << a << endl;
} // vector容器元素反转
void print(int val) {
cout << val << " ";
} void test2() {
vector<int>v;
for (int i = 0; i < 10; i++) {
v.push_back(i);
}
reverse(v.begin(), v.end()); for_each(v.begin(), v.end(), print);
} int main() {
test();
test2(); system("pause");
return 0;
}

1.4 常用拷贝和替换算法

1.4.1 copy

能力:

        将容器内指定范围内的元素拷贝到目标容器。

注意:

使用copy前需要提前给目标容器开辟空间。

函数原型:

copy(iterator begin, iterator end, iterator target_begin);

begin:容器开始迭代器

end:容器结束迭代器

target_begin:目标容器开始迭代器

示例:

#include <iostream>
#include <string>
#include<vector>
#include<algorithm> using namespace std; // copy 将容器内指定范围内元素拷贝到目标容器
void print(int val) {
cout << val << " ";
} void test() {
vector<int> v;
for (int i = 0; i < 10; i++) {
v.push_back(i);
} vector<int> tv;
// 使用copy前需要提前给目标容器开辟空间
tv.resize(v.size());
copy(v.begin(), v.end(), tv.begin()); for_each(tv.begin(), tv.end(), print);
} int main() {
test(); system("pause");
return 0;
}

1.4.2 replace

能力:

        将容器指定范围的旧元素替换为新元素

函数原型:

        replace(iterator begin, iterator end, oldvalue, newvalue);

begin: 开始迭代器

end: 结束迭代器

oldvalue: 旧元素

newvalue: 新元素

示例:

#include <iostream>
#include <string>
#include<vector>
#include<algorithm> using namespace std; // replace 将容器内旧元素替换为新元素
void print(int val) {
cout << val << " ";
} void test() {
vector<int> v;
v.push_back(1);
v.push_back(2);
v.push_back(1);
v.push_back(1);
v.push_back(3); replace(v.begin(), v.end(), 1, 10); for_each(v.begin(), v.end(), print);
} int main() {
test(); system("pause");
return 0;
}

1.4.3 replace_if

能力:

        将容器区间内满足条件的元素,替换成新元素

函数原型:

replace_if(iterator begin, iterator end,  _pred, newvalue);

begin: 开始迭代器

end: 结束迭代器

_pred: 谓词

newvalue: 新元素

示例:

#include <iostream>
#include <string>
#include<vector>
#include<algorithm>
using namespace std; // replace_if 将容器区间内满足条件的元素,替换成新元素
bool greater3less9(int val) {
// 大于三且小于9
return val > 3 && val < 9;
} void print(int val) {
cout << val << " ";
} void test() {
vector<int>v;
for (int i = 0; i < 10; i++) {
v.push_back(i);
} for_each(v.begin(), v.end(), print);
cout << endl; // 将大于3且小于9的元素替换成100
replace_if(v.begin(), v.end(), greater3less9,100); for_each(v.begin(), v.end(), print);
} int main() {
test(); system("pause");
return 0;
}

1.4.4 swap

能力:

        互换两个容器的元素

注意:

交互的两个容器是同种类型

函数原型:

swap(container c1, container c2);

c1: 容器1

c2: 容器2

示例:

#include <iostream>
#include <string>
#include<vector>
#include<algorithm>
using namespace std; // swap 互换两个容器的元素 void print(int val) {
cout << val << " ";
} void test() {
vector<int>v1;
vector<int>v2; for (int i = 0; i < 10; i++) {
v1.push_back(i);
v2.push_back(100 + i);
}
for_each(v1.begin(), v1.end(), print);
cout << endl;
for_each(v2.begin(), v2.end(), print);
cout << endl; swap(v1, v2); cout <<"交换容器后:" << endl;
for_each(v1.begin(), v1.end(), print);
cout << endl;
for_each(v2.begin(), v2.end(), print);
cout << endl;
} int main() {
test(); system("pause");
return 0;
}

1.5 常用算术生成算法

1.5.1 accumulate

能力:

        计算容器区间内元素之和

函数原型:

accumlate(iterator begin, iterator end, value);

begin: 开始迭代器

end: 结束迭代器

value: 起始值

示例:

#include <iostream>
#include <string>
#include<vector>
#include<numeric>
using namespace std;
//accumulate 计算容器区间内元素之和
void test() {
vector<int>v;
for (int i = 0; i < 10; i++) {
v.push_back(i);
} vector<int>::iterator beg = v.begin();
vector<int>::iterator end = v.begin()+5;
// 计算前4个元素之和
int ret=accumulate(beg,end, 0); cout << ret << endl;
} int main() {
test(); system("pause");
return 0;
}

1.5.2 fill

能力:

        向容器指定范围区间,填充指定的元素

注意:

填充的元素数据类型,与容器中元素的数据类型要保持一致。

函数原型:

fill(iterator begin, iterator end, value);

begin: 开始迭代器

end: 结束迭代

value: 填充的值

示例:

#include <iostream>
#include <string>
#include<vector>
#include<numeric>
#include<algorithm> using namespace std;
// fill 向容器中指定范围区间,填充指定元素 void print(int val) {
cout << val << " ";
} void test() {
vector<int>v;
v.resize(10); vector<int>::iterator beg = v.begin();
vector<int>::iterator end = v.begin() + 5; fill(beg, end, 1000); for_each(v.begin(), v.end(), print);
cout << endl;
} int main() {
test(); system("pause");
return 0;
}

1.6 常用集合算法

集合的概念:

        交集:包含两个集合相同的元素

        并集:包含两个集合全部元素

        差集:一个集合去掉也存在另一个集合中的元素,剩下来的元素。

​编辑

注意,本章节三个求集合算法使用时:

  • 求集合的两个容器,都必须是有序序列,且排序规则一致。
  • 目标容器需要提前开辟空间。

1.6.1 set_intersection

能力:

        求两个容器的交集

注意:

  •          两个容器必须是有序序列,且排序规则一致;
  • 要先开辟目标容器空间,可以取目标容器中中较小的空间。(最特殊情况,大容器包含小容器元素,目标容器空间元素个数等于小容器元素个数)

函数原型:

    vector<int>::iterator taget_end= set_intersection(iteraor begin1, iterator end1, iterator begin2, iterator end2, iterator taget_begin);

taget_end:返回目标容器的最后一个元素的迭代器地址

begin1: 容器1开始迭代器

end1: 容器1结束迭代器

begin2:容器2开始迭代器

end2:容器2结束迭代器

target_begin:目标容器开始迭代器

示例:

#include <iostream>
#include <string>
#include<vector>
#include<algorithm>
//#include<numeric>
using namespace std; //set_intersection 求容器交集 void print(int val) {
cout << val << " ";
} void test() {
vector<int> v1;
vector<int>v2;
for (int i = 0; i < 10; i++) {
v1.push_back(i);
v2.push_back(i + 5);
}
for_each(v1.begin(), v1.end(), print);
cout << endl;
for_each(v2.begin(), v2.end(), print);
cout << endl; vector<int> vt;
// 求交集 要先开辟目标容器空间
// 可以取目标容器中中较小的空间
vt.resize(min(v1.size(),v2.size())); // 返回目标容器的最后一个元素的迭代器地址
vector<int>::iterator itEnd = set_intersection(v1.begin(), v1.end(),
v2.begin(), v2.end(), vt.begin()); cout << "交集:" << endl;
for_each(vt.begin(), itEnd, print);
} int main() {
test(); system("pause");
return 0;
}

输出:

0 1 2 3 4 5 6 7 8 9

5 6 7 8 9 10 11 12 13 14

交集:

5 6 7 8 9

1.6.2 set_union

能力:

        求两个容器的并集

注意:

  • 两个容器必须是有序序列,且排序规则一致;
  • 目标容器需要先开辟空间,要等于两个容器空间之和。(最特殊情况,两个容器没有交集,并集元素个数等于两个容器元素个数之和)

函数原型:

vector<int>::iterator taget_end= set_union(iteraor begin1, iterator end1, iterator begin2, iterator end2, iterator taget_begin);

taget_end:返回目标容器的最后一个元素的迭代器地址

begin1: 容器1开始迭代器

end1: 容器1结束迭代器

begin2:容器2开始迭代器

end2:容器2结束迭代器

target_begin:目标容器开始迭代器

示例:

#include <iostream>
#include <string>
#include<vector>
#include<algorithm>
using namespace std; //set_union 求容器并集 void print(int val) {
cout << val << " ";
} void test() {
vector<int> v1;
vector<int>v2;
for (int i = 0; i < 10; i++) {
v1.push_back(i);
v2.push_back(i + 5);
}
for_each(v1.begin(), v1.end(), print);
cout << endl;
for_each(v2.begin(), v2.end(), print);
cout << endl; vector<int> vt;
// 求并集 需要先开辟目标容器空间
// 可以取两个容器空间之和
vt.resize(v1.size() + v2.size()); // 返回目标容器的最后一个元素的迭代器地址
vector<int>::iterator itEnd = set_union(v1.begin(), v1.end(),
v2.begin(), v2.end(), vt.begin()); cout << "并集:" << endl;
for_each(vt.begin(), itEnd, print);
cout << endl;
} int main() {
test(); system("pause");
return 0;
}

输出:

0 1 2 3 4 5 6 7 8 9

5 6 7 8 9 10 11 12 13 14

并集:

0 1 2 3 4 5 6 7 8 9 10 11 12 13 14

1.6.3 set_difference

能力:

        求两个容器的差集

注意:

  • 两个容器必须是有序序列,且排序规则一致;
  • 目标容器需要先开辟空间,要等于第一个容器空间。(最特殊的情况就是两个容器没有交集,差集的元素个数与第一个容器元素一样)

函数原型:

vector<int>::iterator taget_end= set_difference(iteraor begin1, iterator end1, iterator begin2, iterator end2, iterator taget_begin);

taget_end:返回目标容器的最后一个元素的迭代器地址

begin1: 容器1开始迭代器

end1: 容器1结束迭代器

begin2:容器2开始迭代器

end2:容器2结束迭代器

target_begin:目标容器开始迭代器

示例:

#include <iostream>
#include <string>
#include<vector>
#include<algorithm>
//#include<numeric>
using namespace std; //set_difference 求容器差集 void print(int val) {
cout << val << " ";
} void test() {
vector<int> v1;
vector<int>v2;
for (int i = 0; i < 10; i++) {
v1.push_back(i);
v2.push_back(i+5);
}
for_each(v1.begin(), v1.end(), print);
cout << endl;
for_each(v2.begin(), v2.end(), print);
cout << endl; // 求v1和v2的差集
vector<int> vt1;
// 求差集需要先给目标容器开辟空间
// 最特殊的情况,两个容器没有差集,空间可以设定为第一个容器的大小
vt1.resize(v1.size()); // 返回目标容器的最后一个元素的迭代器地址
vector<int>::iterator itEnd = set_difference(v1.begin(),v1.end(),
v2.begin(),v2.end(),vt1.begin());
cout << "v1和v2的差集:" << endl;
for_each(vt1.begin(), itEnd, print);
cout << endl; // 求v2和v1的差集:
vector<int> vt2;
// 求差集需要先给目标容器开辟空间
// 最特殊的情况,两个容器没有差集,空间可以设定为第一个容器的大小
vt2.resize(v2.size()); // 返回目标容器的最后一个元素的迭代器地址
vector<int>::iterator itEnd2 = set_difference(v2.begin(), v2.end(),
v1.begin(), v1.end(), vt2.begin());
cout << "v2和v1的差集:" << endl;
for_each(vt2.begin(), itEnd2, print);
cout << endl;
} int main() {
test(); system("pause");
return 0;
}

输出:

0 1 2 3 4 5 6 7 8 9

5 6 7 8 9 10 11 12 13 14

v1和v2的差集:

0 1 2 3 4

v2和v1的差集:

10 11 12 13 14

14 STL-常用算法的更多相关文章

  1. [C++ STL] 常用算法总结

    1 概述 STL算法部分主要由头文件<algorithm>,<numeric>,<functional>组成.要使用 STL中的算法函数必须包含头文件<alg ...

  2. 28.STL常用算法

    #include <algorithm> 算法 常用版本 描述 返回Type std::find() find(_InIt _Fisrt,_InIt _Last, _Ty& _Va ...

  3. C++ STL——常用算法

    目录 一 常用查找算法 二 常用遍历算法 注:原创不易,转载请务必注明原作者和出处,感谢支持! 注:内容来自某培训课程,不一定完全正确! 一 常用查找算法 /* find算法 查找元素 @param ...

  4. c++ STL常用算法使用方法

    #include <string> #include <vector> #include <functional> #include <iostream> ...

  5. 常用的STL查找算法

    常用的STL查找算法 <effective STL>中有句忠告,尽量用算法替代手写循环:查找少不了循环遍历,在这里总结下常用的STL查找算法: 查找有三种,即点线面: 点就是查找目标为单个 ...

  6. STL——配接器、常用算法使用

    学习STL,必然会用到它里面的适配器和一些常用的算法.它们都是STL中的重要组成部分. 适配器 在STL里可以用一些容器适配得到适配器.例如其中的stack和queue就是由双端队列deque容器适配 ...

  7. C++ STL 常用算术和生成算法

    C++ STL 常用算术和生成算法 accumulate() accumulate: 对指定范围内的元素求和,然后结果再加上一个由val指定的初始值. #include<numeric> ...

  8. C++ STL 常用排序算法

    C++ STL 常用排序算法 merge() 以下是排序和通用算法:提供元素排序策略 merge: 合并两个有序序列,存放到另一个序列. 例如: vecIntA,vecIntB,vecIntC是用ve ...

  9. C++ STL 常用查找算法

    C++ STL 常用查找算法 adjacent_find() 在iterator对标识元素范围内,查找一对相邻重复元素,找到则返回指向这对元素的第一个元素的迭代器.否则返回past-the-end. ...

  10. C++ STL 常用遍历算法

    C++ STL 常用遍历算法 STL的容器算法迭代器的设计理念 1) STL的容器通过类模板技术,实现数据类型和容器模型的分离 2) STL的迭代器技术实现了遍历容器的统一方法:也为STL的算法提供了 ...

随机推荐

  1. Kibana:运用索引模式的 formatter 来探索 IP 信息

    文章转载自:https://blog.csdn.net/UbuntuTouch/article/details/107484674

  2. Portainer 基本功能介紹之升級映像檔並更新 Container

    文档地址:https://www.asustor.com/zh-tw/online/College_topic?topic=145#dpt7

  3. linux系统下查看某个进程内存使用量

  4. 谣言检测(PLAN)——《Interpretable Rumor Detection in Microblogs by Attending to User Interactions》

    论文信息 论文标题:Interpretable Rumor Detection in Microblogs by Attending to User Interactions论文作者:Ling Min ...

  5. P7114 [NOIP2020] 字符串匹配 (字符串hash+树状数组)

    好多题解用的扩展KMP(没学过,所以不用这种方法). 我们按照题目要求记F(s)表示s串的权值,可以预处理出前缀权值(用于A)和后缀权值(用于C),枚举AB的长度i=2~n-1,不需要分开枚举,我们只 ...

  6. 基于docker安装phpmyadmin

    今天用到了phpadmin,要从头装的话,比较麻烦,所以就选择使用docker 安装 准备 任意Linux系统且已成功安装docker环境 安装phpmyAdmin 1. 拉取镜像 docker pu ...

  7. 第一个微信小程序的初始化过程、小程序微信开发平台的下载、如何注册一个微信小程序的账号

    文章目录 1.注册微信小程序账号 1.1 小程序的注册流程 1.2 登录小程序账号 2.下载微信小程序开发者平台 3.新建一个小程序 3.1 点击加号 3.2 填写项目目录和小程序ID 3.3 点击确 ...

  8. dubbo的一系列配置与搭建

    dubbo新的版本采用前后端分离技术,在github上下载的时候,不仅仅只是一个dubbo-admin 而是将admin包分离为dubbo-admin-ui前端包和dubbo-admin-server ...

  9. HTML元素大全(2)-表单

    01.<form>表单 <form> 表单是比较重要的HTML元素,块元素,主要作用是向服务端提交数据.结合表单元素input使用,通过内部的button按钮提交(type=& ...

  10. How to install the Package Controller

    How to install the Package Controller? https://packagecontrol.io/installation INSTALLATION Use one o ...