C++中STL的常用算法總結(jié)
算法主要由頭文件<algorithm><functional><numeric>組成。
<algorithm>是所有STL頭文件中最大的一個(gè),范圍包括比較、交換、查找、遍歷、復(fù)制、修改等。
<functional>定義了一些模版類,常用的仿函數(shù)等。
<numeric>體積很小,只包括幾個(gè)在序列上面進(jìn)行簡(jiǎn)單數(shù)學(xué)運(yùn)算的模版函數(shù)。
1.常用遍歷算法
1.1 for_each
能力:
遍歷容器的算法
函數(shù)原型:
for_each(iterator begin, iterator end, _func);
- begin: 開(kāi)始迭代器
- end: 結(jié)束迭代器
- _func: 函數(shù)或函數(shù)對(duì)象(即仿函數(shù))
上面_func參數(shù)寫(xiě)法:
如果是普通函數(shù):就寫(xiě) 函數(shù)名
如果是仿函數(shù):需要寫(xiě) 類名()
示例:
#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);
}
// 利用普通和函數(shù) print1
for_each(v.begin(), v.end(), print1);
cout << endl;
// 利用仿函數(shù) print2()
for_each(v.begin(), v.end(), print2());
}
int main() {
test();
system("pause");
return 0;
}1.2 transform
能力:
搬運(yùn)容器到另一個(gè)容器中
函數(shù)原型:
transform(iterator begin1, iterator end1, iterator begin2, _func);
- begin1:源容器開(kāi)始迭代器
- end1: 源容器結(jié)束迭代器
- begin2: 目標(biāo)容器開(kāi)始迭代器
- _func: 普通函數(shù)或仿函數(shù)
注意:
在使用transform搬運(yùn)容器之前,必須要給目標(biāo)容器提前開(kāi)辟空間,不然會(huì)報(bào)錯(cuò)。通常用 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;//目標(biāo)容器
v2.resize(v.size());// 必須要給目標(biāo)容器提前開(kāi)辟空間,開(kāi)辟搬運(yùn)容器空間的大小即可。
transform(v.begin(), v.end(), v2.begin(), Transform());
for_each(v2.begin(), v2.end(), MyPrint());
cout << endl;
}
int main() {
test();
system("pause");
return 0;
}2.常用查找算法
主要包含:
- find 查找元素
- find_if 按條件查找元素
- adjacent_find 查找相鄰重復(fù)元素
- binary_search 二分查找法
- count 統(tǒng)計(jì)元素個(gè)數(shù)
- count_if 按條件統(tǒng)計(jì)元素個(gè)數(shù)
2.1 find
能力:
查找指定元素,若找到,返回指定元素迭代器;若未找到,返回結(jié)束迭代器end().
函數(shù)原型:
find(iterator begin, iterator end, value);
- begin: 開(kāi)始迭代器
- end: 結(jié)束迭代器
- value: 要查找的元素
示例:
#include <iostream>
#include <string>
#include<vector>
#include<algorithm>
using namespace std;
// 常用算法 find
// find查找內(nèi)置數(shù)據(jù)類型
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;
}
}
// 查找自定義數(shù)據(jù)類型
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;
}2.2 find_if
能力:
按條件查找元素。
按值查找元素,若找到,返回指定元素迭代器,若未找到,返回結(jié)束迭代器。
函數(shù)原型:
find_if(iterator begin, iterator end, _pred);
- begin: 開(kāi)始迭代器
- end: 結(jié)束迭代器
- _pred: 普通函數(shù)或仿函數(shù)(需要是返回bool類型的函數(shù))
示例:
#include <iostream>
#include <string>
#include<vector>
#include<algorithm>
// find_if
using namespace std;
// 查找內(nèi)置數(shù)據(jù)類型
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;
}
}
//自定義數(shù)據(jù)類型
class Person {
public:
Person(string name, int age) {
this->m_name = name;
this->m_age = age;
}
string m_name;
int m_age;
};
// 仿函數(shù)
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;
}2.3 adjacent_find
能力:
查找相鄰重復(fù)元素,若存在,返回指定位置迭代器,若不存在,返回迭代器end()。
函數(shù)原型:
adjacent_find(iterator begin, iterator end);
- begin: 開(kāi)始迭代器
- end: 結(jié)束迭代器
示例:
#include <iostream>
#include <string>
#include<vector>
#include<algorithm>
using namespace std;
// 查找相鄰重復(fù)元素 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 << "查找到相鄰元素重復(fù):" << *pos << endl;
}
else {
cout << "未找到相鄰重復(fù)元素" << endl;
}
}
int main() {
test();
system("pause");
return 0;
}2.4 binary_search
能力:
查找指定元素是否存在。若存在,返回true。若不存在,返回false。
注意:
只能在有序序列才可用。
函數(shù)原型:
bool binary_search(iterator begin, iterator end, value);
- begin: 開(kāi)始迭代器
- end: 結(jié)束迭代器
- 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);
// 只有在有序序列中才是準(zhǔn)確的,如果沒(méi)有sort排序測(cè)試出 未找到
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;
}2.5 count
能力:
統(tǒng)計(jì)元素個(gè)數(shù)
函數(shù)原型:
count(iterator begin, iterator end, value);
- begin: 開(kāi)始迭代器
- end: 結(jié)束迭代器
- value: 要統(tǒng)計(jì)的元素
示例:
#include <iostream>
#include <string>
#include<vector>
#include<algorithm>
using namespace std;
// count 統(tǒng)計(jì)
// 內(nèi)置數(shù)據(jù)類型
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的個(gè)數(shù)為:" << n << endl;
}
// 自定義數(shù)據(jù)類型
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 的元素個(gè)數(shù)為:" << n << endl;
}
int main() {
//test();
test2();
system("pause");
return 0;
}2.6 count_if
能力:
按條件統(tǒng)計(jì)元素個(gè)數(shù)。
函數(shù)原型:
count_if(iterator begin, iterator end, _pred);
- begin: 開(kāi)始迭代器
- end: 結(jié)束迭代器
- _pred: 謂詞
示例:
#include <iostream>
#include <string>
#include<vector>
#include<algorithm>
using namespace std;
// count 統(tǒng)計(jì)
// 內(nèi)置數(shù)據(jù)類型
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個(gè)數(shù)為:" << n << endl;
}
// 自定義數(shù)據(jù)類型
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的元素個(gè)數(shù)為:" << n << endl;
}
int main() {
//test();
test2();
system("pause");
return 0;
}3.常用排序算法
3.1 sort
能力:
對(duì)容器內(nèi)元素排序
函數(shù)原型:
sort(iterator begin, iterator end, _pred);
- begin: 開(kāi)始迭代器
- end: 結(jié)束迭代器
- _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 默認(rèn)升序
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;
}3.2 random_shuffe
能力:
也叫洗牌。將指定范圍內(nèi)的元素隨機(jī)打亂次序。
函數(shù)原型:
random_shuffle(iterator begin, iterator end);//也叫洗牌。將指定范圍內(nèi)的元素隨機(jī)打亂次序。
begin: 開(kāi)始迭代器
end: 結(jié)束迭代器
注意:
使用時(shí),需要加隨機(jī)數(shù)種子,才能真實(shí)的隨機(jī)起來(lái),每次不一樣。
#include<ctime> ???????srand((unsigned int)time(NULL));
#include <iostream>
#include <string>
#include<vector>
#include<algorithm>
#include<ctime>
using namespace std;
// random_shuffle 洗牌 將制定范圍內(nèi)元素打亂次序
void print(int val) {
cout << val << " ";
}
void test() {
// 隨機(jī)數(shù)種子,讓random_shuffle真實(shí)的隨機(jī)起來(lái)
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
3.3 merge
能力:
兩個(gè)有序容器元素合并,并存儲(chǔ)到目標(biāo)容器中。
注意:
兩個(gè)容器必須是有序的,才能合并。
兩個(gè)容器的排序必須是一致的,都是升序或降序。
需要先給目標(biāo)容器開(kāi)辟空間。
函數(shù)原型:
merge(iterator begin1, iterator end1, iterator begin2, iterator end2, iterator target_begin);
- begin1: 容器1開(kāi)始迭代器
- end1: 容器1結(jié)束迭代器
- begin2:容器2開(kāi)始迭代器
- end2:容器2結(jié)束迭代器
- target_begin:目標(biāo)容器開(kāi)始迭代器
示例:
#include <iostream>
#include <string>
#include<vector>
#include<algorithm>
using namespace std;
// merge 將兩個(gè)有序容器元素合并到一個(gè)目標(biāo)容器中
void print(int val) {
cout << val << " ";
}
void test() {
vector<int> v1;
vector<int> v2;
// 兩個(gè)容器需要是有序,且排序一致,都是升序或降序
for (int i = 0; i < 10; i++) {
v1.push_back(i);
v2.push_back(100 + i);
// v2.push_back(100 - i); // 程序會(huì)崩潰
}
vector<int> v3;
// 需要提前給目標(biāo)容器分配空間
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;
}3.4 reverse
能力:
將容器元素進(jìn)行反轉(zhuǎn)。
函數(shù)原型:
reverse(iterator begin, iterator end);
- begin: 容器1開(kāi)始迭代器
- end: 容器1結(jié)束迭代器
示例:
#include <iostream>
#include <string>
#include<vector>
#include<algorithm>
using namespace std;
// reverse 將容器元素反轉(zhuǎn)
// 將string內(nèi)容反轉(zhuǎn)
void test() {
string a = "abcdefg";
reverse(a.begin(), a.end());
cout << a << endl;
}
// vector容器元素反轉(zhuǎn)
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;
}4.常用拷貝和替換算法
4.1 copy
能力:
將容器內(nèi)指定范圍內(nèi)的元素拷貝到目標(biāo)容器。
注意:
使用copy前需要提前給目標(biāo)容器開(kāi)辟空間。
函數(shù)原型:
copy(iterator begin, iterator end, iterator target_begin);
- begin:容器開(kāi)始迭代器
- end:容器結(jié)束迭代器
- target_begin:目標(biāo)容器開(kāi)始迭代器
示例:
#include <iostream>
#include <string>
#include<vector>
#include<algorithm>
using namespace std;
// copy 將容器內(nèi)指定范圍內(nèi)元素拷貝到目標(biāo)容器
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前需要提前給目標(biāo)容器開(kāi)辟空間
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;
}4.2 replace
能力:
將容器指定范圍的舊元素替換為新元素
函數(shù)原型:
replace(iterator begin, iterator end, oldvalue, newvalue);
- begin: 開(kāi)始迭代器
- end: 結(jié)束迭代器
- oldvalue: 舊元素
- newvalue: 新元素
示例:
#include <iostream>
#include <string>
#include<vector>
#include<algorithm>
using namespace std;
// replace 將容器內(nèi)舊元素替換為新元素
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;
}4.3 replace_if
能力:
將容器區(qū)間內(nèi)滿足條件的元素,替換成新元素
函數(shù)原型:
replace_if(iterator begin, iterator end, _pred, newvalue);
- begin: 開(kāi)始迭代器
- end: 結(jié)束迭代器
- _pred: 謂詞
- newvalue: 新元素
示例:
#include <iostream>
#include <string>
#include<vector>
#include<algorithm>
using namespace std;
// replace_if 將容器區(qū)間內(nèi)滿足條件的元素,替換成新元素
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;
}4.4 swap
能力:
互換兩個(gè)容器的元素
注意:
交互的兩個(gè)容器是同種類型
函數(shù)原型:
swap(container c1, container c2);
- c1: 容器1
- c2: 容器2
示例:
#include <iostream>
#include <string>
#include<vector>
#include<algorithm>
using namespace std;
// swap 互換兩個(gè)容器的元素
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;
}5.常用算術(shù)生成算法
5.1 accumulate
能力:
計(jì)算容器區(qū)間內(nèi)元素之和
函數(shù)原型:
accumlate(iterator begin, iterator end, value);
- begin: 開(kāi)始迭代器
- end: 結(jié)束迭代器
- value: 起始值
示例:
#include <iostream>
#include <string>
#include<vector>
#include<numeric>
using namespace std;
//accumulate 計(jì)算容器區(qū)間內(nèi)元素之和
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;
// 計(jì)算前4個(gè)元素之和
int ret=accumulate(beg,end, 0);
cout << ret << endl;
}
int main() {
test();
system("pause");
return 0;
}5.2 fill
能力:
向容器指定范圍區(qū)間,填充指定的元素
注意:
填充的元素?cái)?shù)據(jù)類型,與容器中元素的數(shù)據(jù)類型要保持一致。
函數(shù)原型:
fill(iterator begin, iterator end, value);
- begin: 開(kāi)始迭代器
- end: 結(jié)束迭代
- value: 填充的值
示例:
#include <iostream>
#include <string>
#include<vector>
#include<numeric>
#include<algorithm>
using namespace std;
// fill 向容器中指定范圍區(qū)間,填充指定元素
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;
}6.常用集合算法
集合的概念:
交集:包含兩個(gè)集合相同的元素
并集:包含兩個(gè)集合全部元素
差集:一個(gè)集合去掉也存在另一個(gè)集合中的元素,剩下來(lái)的元素。

注意,本章節(jié)三個(gè)求集合算法使用時(shí):
求集合的兩個(gè)容器,都必須是有序序列,且排序規(guī)則一致。
目標(biāo)容器需要提前開(kāi)辟空間。
6.1 set_intersection
能力:
求兩個(gè)容器的交集
注意:
兩個(gè)容器必須是有序序列,且排序規(guī)則一致;
要先開(kāi)辟目標(biāo)容器空間,可以取目標(biāo)容器中中較小的空間。(最特殊情況,大容器包含小容器元素,目標(biāo)容器空間元素個(gè)數(shù)等于小容器元素個(gè)數(shù))
函數(shù)原型:
vector<int>::iterator taget_end= set_intersection(iteraor begin1, iterator end1, iterator begin2, iterator end2, iterator taget_begin);
- taget_end:返回目標(biāo)容器的最后一個(gè)元素的迭代器地址
- begin1: 容器1開(kāi)始迭代器
- end1: 容器1結(jié)束迭代器
- begin2:容器2開(kāi)始迭代器
- end2:容器2結(jié)束迭代器
- target_begin:目標(biāo)容器開(kāi)始迭代器
示例:
#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;
// 求交集 要先開(kāi)辟目標(biāo)容器空間
// 可以取目標(biāo)容器中中較小的空間
vt.resize(min(v1.size(),v2.size()));
// 返回目標(biāo)容器的最后一個(gè)元素的迭代器地址
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
6.2 set_union
能力:
求兩個(gè)容器的并集
注意:
兩個(gè)容器必須是有序序列,且排序規(guī)則一致;
目標(biāo)容器需要先開(kāi)辟空間,要等于兩個(gè)容器空間之和。(最特殊情況,兩個(gè)容器沒(méi)有交集,并集元素個(gè)數(shù)等于兩個(gè)容器元素個(gè)數(shù)之和)
函數(shù)原型:
vector<int>::iterator taget_end= set_union(iteraor begin1, iterator end1, iterator begin2, iterator end2, iterator taget_begin);
- taget_end:返回目標(biāo)容器的最后一個(gè)元素的迭代器地址
- begin1: 容器1開(kāi)始迭代器
- end1: 容器1結(jié)束迭代器
- begin2:容器2開(kāi)始迭代器
- end2:容器2結(jié)束迭代器
- target_begin:目標(biāo)容器開(kāi)始迭代器
示例:
#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;
// 求并集 需要先開(kāi)辟目標(biāo)容器空間
// 可以取兩個(gè)容器空間之和
vt.resize(v1.size() + v2.size());
// 返回目標(biāo)容器的最后一個(gè)元素的迭代器地址
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
6.3 set_difference
能力:
求兩個(gè)容器的差集
注意:
兩個(gè)容器必須是有序序列,且排序規(guī)則一致;
目標(biāo)容器需要先開(kāi)辟空間,要等于第一個(gè)容器空間。(最特殊的情況就是兩個(gè)容器沒(méi)有交集,差集的元素個(gè)數(shù)與第一個(gè)容器元素一樣)
函數(shù)原型:
vector<int>::iterator taget_end= set_difference(iteraor begin1, iterator end1, iterator begin2, iterator end2, iterator taget_begin);
- taget_end:返回目標(biāo)容器的最后一個(gè)元素的迭代器地址
- begin1: 容器1開(kāi)始迭代器
- end1: 容器1結(jié)束迭代器
- begin2:容器2開(kāi)始迭代器
- end2:容器2結(jié)束迭代器
- target_begin:目標(biāo)容器開(kāi)始迭代器
示例:
#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;
// 求差集需要先給目標(biāo)容器開(kāi)辟空間
// 最特殊的情況,兩個(gè)容器沒(méi)有差集,空間可以設(shè)定為第一個(gè)容器的大小
vt1.resize(v1.size());
// 返回目標(biāo)容器的最后一個(gè)元素的迭代器地址
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;
// 求差集需要先給目標(biāo)容器開(kāi)辟空間
// 最特殊的情況,兩個(gè)容器沒(méi)有差集,空間可以設(shè)定為第一個(gè)容器的大小
vt2.resize(v2.size());
// 返回目標(biāo)容器的最后一個(gè)元素的迭代器地址
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
以上就是C++中STL的常用算法總結(jié)的詳細(xì)內(nèi)容,更多關(guān)于C++ STL常用算法的資料請(qǐng)關(guān)注腳本之家其它相關(guān)文章!
相關(guān)文章
C語(yǔ)言基礎(chǔ)函數(shù)用法示例詳細(xì)解析
最接地氣的C函數(shù)基礎(chǔ)介紹,此處對(duì)于函數(shù)的相關(guān)知識(shí)點(diǎn)做一些簡(jiǎn)要的介紹,作者實(shí)屬初學(xué),寫(xiě)博客也是作者學(xué)習(xí)的一個(gè)過(guò)程,難免文章中有內(nèi)容理解不到位或者有不當(dāng)之處,還請(qǐng)朋友們不吝指正2021-11-11
C語(yǔ)言fprintf()函數(shù)和fscanf()函數(shù)的具體使用
本文主要介紹了C語(yǔ)言fprintf()函數(shù)和fscanf()函數(shù)的具體使用,文中通過(guò)示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2021-11-11
C語(yǔ)言實(shí)現(xiàn)掃雷游戲詳解(附源碼)
大家好,本篇文章主要講的是C語(yǔ)言實(shí)現(xiàn)掃雷游戲詳解(附源碼),感興趣的同學(xué)趕快來(lái)看一看吧,對(duì)你有幫助的話記得收藏一下2022-01-01
QT網(wǎng)絡(luò)編程Tcp下C/S架構(gòu)的即時(shí)通信實(shí)例
下面小編就為大家?guī)?lái)一篇QT網(wǎng)絡(luò)編程Tcp下C/S架構(gòu)的即時(shí)通信實(shí)例。小編覺(jué)得挺不錯(cuò)的,現(xiàn)在就分享給大家,也給大家做個(gè)參考。一起跟隨小編過(guò)來(lái)看看吧2017-08-08
C++實(shí)現(xiàn)一鍵關(guān)閉桌面的示例代碼
這篇文章主要為大家詳細(xì)介紹了如何利用C++實(shí)現(xiàn)一鍵關(guān)閉桌面的功能,文中的示例代碼講解詳細(xì),具有一定的學(xué)習(xí)價(jià)值,感興趣的小伙伴可以了解一下2023-07-07
C++貪心算法處理多機(jī)調(diào)度問(wèn)題詳解
貪心算法(又稱貪婪算法)是指,在對(duì)問(wèn)題求解時(shí),總是做出在當(dāng)前看來(lái)是最好的選擇。也就是說(shuō),不從整體最優(yōu)上加以考慮,他所做出的僅是在某種意義上的局部最優(yōu)解2022-06-06
OpenCV圖像算法實(shí)現(xiàn)圖像切分圖像合并示例
這篇文章主要為大家介紹了python圖像算法OpenCV實(shí)現(xiàn)圖像切分圖像合并操作示例,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪2022-06-06

