最新国产好看的视频,伊人天堂AV在线,国产Aaaaaa视频,蜜臀视频在线观看一区,人妻av色图,密臀久久久精品影片,青青视频免费观看毛片,久草在线观看视,国产三级精品色情在线

C++中STL的常用算法總結(jié)

 更新時間:2022年12月01日 08:20:32   作者:愛學(xué)習(xí)的小靈子  
這篇文章主要介紹了C++?STL中一些常見算法的使用,本文給大家介紹的非常詳細(xì),對大家的學(xué)習(xí)或工作具有一定的參考借鑒價值,需要的朋友可以參考下

算法主要由頭文件<algorithm><functional><numeric>組成。

<algorithm>是所有STL頭文件中最大的一個,范圍包括比較、交換、查找、遍歷、復(fù)制、修改等。

<functional>定義了一些模版類,常用的仿函數(shù)等。

<numeric>體積很小,只包括幾個在序列上面進(jìn)行簡單數(shù)學(xué)運算的模版函數(shù)。

1.常用遍歷算法

1.1 for_each

能力:

遍歷容器的算法

函數(shù)原型:

for_each(iterator begin, iterator end, _func);

  • begin: 開始迭代器
  • end: 結(jié)束迭代器
  • _func: 函數(shù)或函數(shù)對象(即仿函數(shù))

上面_func參數(shù)寫法:

如果是普通函數(shù):就寫  函數(shù)名

如果是仿函數(shù):需要寫  類名()

示例:

#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

能力:

搬運容器到另一個容器中

函數(shù)原型:

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

  • begin1:源容器開始迭代器
  • end1: 源容器結(jié)束迭代器
  • begin2: 目標(biāo)容器開始迭代器
  • _func: 普通函數(shù)或仿函數(shù)

注意:

在使用transform搬運容器之前,必須要給目標(biāo)容器提前開辟空間,不然會報錯。通常用 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)容器提前開辟空間,開辟搬運容器空間的大小即可。
	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)計元素個數(shù)
  • count_if                   按條件統(tǒng)計元素個數(shù)

2.1 find

能力:

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

函數(shù)原型:

find(iterator begin, iterator end, value);

  • begin: 開始迭代器
  • 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: 開始迭代器
  • 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: 開始迭代器
  • 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: 開始迭代器
  • 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)確的,如果沒有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;
}

2.5 count

能力:

統(tǒng)計元素個數(shù)

函數(shù)原型:

count(iterator begin, iterator end, value);

  • begin: 開始迭代器
  • end: 結(jié)束迭代器
  • value: 要統(tǒng)計的元素

示例:

#include <iostream>
#include <string>
#include<vector>
#include<algorithm>
using namespace std;
// count 統(tǒng)計

// 內(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的個數(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 的元素個數(shù)為:" << n << endl;
}

int main() {
	//test();
	test2();

	system("pause");
	return 0;
}

2.6 count_if 

能力:

按條件統(tǒng)計元素個數(shù)。

函數(shù)原型:

count_if(iterator begin, iterator end, _pred);

  • begin: 開始迭代器
  • end: 結(jié)束迭代器
  • _pred: 謂詞

示例:

#include <iostream>
#include <string>
#include<vector>
#include<algorithm>
using namespace std;
// count 統(tǒng)計

// 內(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個數(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的元素個數(shù)為:" << n << endl;
}

int main() {
	//test();
	test2();

	system("pause");
	return 0;
}

3.常用排序算法

3.1 sort

能力:

對容器內(nèi)元素排序

函數(shù)原型:

sort(iterator begin, iterator end, _pred);

  • begin: 開始迭代器
  • 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: 開始迭代器

end: 結(jié)束迭代器

注意:

使用時,需要加隨機(jī)數(shù)種子,才能真實的隨機(jī)起來,每次不一樣。

#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真實的隨機(jī)起來
	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

能力:

兩個有序容器元素合并,并存儲到目標(biāo)容器中。

注意:

兩個容器必須是有序的,才能合并。

兩個容器的排序必須是一致的,都是升序或降序。

需要先給目標(biāo)容器開辟空間。

函數(shù)原型:

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

  • begin1: 容器1開始迭代器
  • end1: 容器1結(jié)束迭代器
  • begin2:容器2開始迭代器
  • end2:容器2結(jié)束迭代器
  • target_begin:目標(biāo)容器開始迭代器

示例:

#include <iostream>
#include <string>
#include<vector>
#include<algorithm>
using namespace std;

// merge 將兩個有序容器元素合并到一個目標(biāo)容器中
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;
	// 需要提前給目標(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開始迭代器
  • 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)容器開辟空間。

函數(shù)原型:

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

  • begin:容器開始迭代器
  • end:容器結(jié)束迭代器
  • target_begin:目標(biāo)容器開始迭代器

示例:

#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)容器開辟空間
	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: 開始迭代器
  • 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: 開始迭代器
  • 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

能力:

互換兩個容器的元素

注意:

交互的兩個容器是同種類型

函數(shù)原型:

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;
}

5.常用算術(shù)生成算法

5.1 accumulate

能力:

計算容器區(qū)間內(nèi)元素之和

函數(shù)原型:

accumlate(iterator begin, iterator end, value);

  • begin: 開始迭代器
  • end: 結(jié)束迭代器
  • value: 起始值

示例:

#include <iostream>
#include <string>
#include<vector>
#include<numeric>
using namespace std;
//accumulate 計算容器區(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;
	// 計算前4個元素之和
	int ret=accumulate(beg,end, 0);

	cout << ret << endl;
}

int main() {
	test();

	system("pause");
	return 0;
}

5.2 fill

能力:

向容器指定范圍區(qū)間,填充指定的元素

注意:

填充的元素數(shù)據(jù)類型,與容器中元素的數(shù)據(jù)類型要保持一致。

函數(shù)原型:

fill(iterator begin, iterator end, value);

  • begin: 開始迭代器
  • 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.常用集合算法

集合的概念:

交集:包含兩個集合相同的元素

并集:包含兩個集合全部元素

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

注意,本章節(jié)三個求集合算法使用時:

求集合的兩個容器,都必須是有序序列,且排序規(guī)則一致。

目標(biāo)容器需要提前開辟空間。

6.1 set_intersection

能力:

求兩個容器的交集

注意:

兩個容器必須是有序序列,且排序規(guī)則一致;

要先開辟目標(biāo)容器空間,可以取目標(biāo)容器中中較小的空間。(最特殊情況,大容器包含小容器元素,目標(biāo)容器空間元素個數(shù)等于小容器元素個數(shù))

函數(shù)原型:

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

  • taget_end:返回目標(biāo)容器的最后一個元素的迭代器地址
  • begin1: 容器1開始迭代器
  • end1: 容器1結(jié)束迭代器
  • begin2:容器2開始迭代器
  • end2:容器2結(jié)束迭代器
  • target_begin:目標(biāo)容器開始迭代器

示例:

#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;
	// 求交集 要先開辟目標(biāo)容器空間
	// 可以取目標(biāo)容器中中較小的空間
	vt.resize(min(v1.size(),v2.size()));

	// 返回目標(biāo)容器的最后一個元素的迭代器地址
	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

能力:

求兩個容器的并集

注意:

兩個容器必須是有序序列,且排序規(guī)則一致;

目標(biāo)容器需要先開辟空間,要等于兩個容器空間之和。(最特殊情況,兩個容器沒有交集,并集元素個數(shù)等于兩個容器元素個數(shù)之和)

函數(shù)原型:

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

  • taget_end:返回目標(biāo)容器的最后一個元素的迭代器地址
  • begin1: 容器1開始迭代器
  • end1: 容器1結(jié)束迭代器
  • begin2:容器2開始迭代器
  • end2:容器2結(jié)束迭代器
  •  target_begin:目標(biāo)容器開始迭代器

示例:

#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;
	// 求并集 需要先開辟目標(biāo)容器空間
	// 可以取兩個容器空間之和
	vt.resize(v1.size() + v2.size());

	// 返回目標(biāo)容器的最后一個元素的迭代器地址
	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

能力:

求兩個容器的差集

注意:

兩個容器必須是有序序列,且排序規(guī)則一致;

目標(biāo)容器需要先開辟空間,要等于第一個容器空間。(最特殊的情況就是兩個容器沒有交集,差集的元素個數(shù)與第一個容器元素一樣)

函數(shù)原型:

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

  • taget_end:返回目標(biāo)容器的最后一個元素的迭代器地址
  • begin1: 容器1開始迭代器
  • end1: 容器1結(jié)束迭代器
  • begin2:容器2開始迭代器
  • end2:容器2結(jié)束迭代器
  • target_begin:目標(biāo)容器開始迭代器

示例:

#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)容器開辟空間
	// 最特殊的情況,兩個容器沒有差集,空間可以設(shè)定為第一個容器的大小
	vt1.resize(v1.size());

	// 返回目標(biāo)容器的最后一個元素的迭代器地址
	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)容器開辟空間
	// 最特殊的情況,兩個容器沒有差集,空間可以設(shè)定為第一個容器的大小
	vt2.resize(v2.size());

	// 返回目標(biāo)容器的最后一個元素的迭代器地址
	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常用算法的資料請關(guān)注腳本之家其它相關(guān)文章!

相關(guān)文章

  • C語言堆結(jié)構(gòu)處理TopK問題詳解

    C語言堆結(jié)構(gòu)處理TopK問題詳解

    TopK問題即在N個數(shù)中找出最大的前K個,這篇文章將詳細(xì)講解如何利用小根堆的方法解決TopK問題,文中代碼具有一定參考價值,快跟隨小編一起學(xué)習(xí)一下吧
    2022-06-06
  • 淺談Qt中使用CEF的幾個要點(Windows下)

    淺談Qt中使用CEF的幾個要點(Windows下)

    下面小編就為大家?guī)硪黄獪\談Qt中使用CEF的幾個要點(Windows下)。小編覺得挺不錯的,現(xiàn)在就分享給大家,也給大家做個參考。一起跟隨小編過來看看吧
    2017-07-07
  • Ubuntu16.04下配置VScode的C/C++開發(fā)環(huán)境

    Ubuntu16.04下配置VScode的C/C++開發(fā)環(huán)境

    這篇文章主要介紹了Ubuntu16.04下配置VScode的C/C++開發(fā)環(huán)境的教程,本文通過圖文并茂的形式給大家介紹的非常詳細(xì),對大家的學(xué)習(xí)或工作具有一定的參考借鑒價值,需要的朋友可以參考下
    2020-03-03
  • C語言實現(xiàn)通訊錄的詳細(xì)代碼

    C語言實現(xiàn)通訊錄的詳細(xì)代碼

    本文詳細(xì)講解了C語言實現(xiàn)通訊錄的方法,文中通過示例代碼介紹的非常詳細(xì)。對大家的學(xué)習(xí)或工作具有一定的參考借鑒價值,需要的朋友可以參考下
    2021-12-12
  • C++歸并排序算法實例

    C++歸并排序算法實例

    這篇文章主要介紹了C++歸并排序算法實例,本文先是介紹了什么是歸并排序,然后給出了實現(xiàn)代碼,需要的朋友可以參考下
    2014-10-10
  • C++中mutable與volatile的深入理解

    C++中mutable與volatile的深入理解

    這篇文章主要給的阿加介紹了關(guān)于C++中mutable與volatile的相關(guān)資料,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧
    2018-11-11
  • 實例講解C++ 命名空間

    實例講解C++ 命名空間

    這篇文章主要介紹了C++ 命名空間的的相關(guān)資料,文中示例代碼非常詳細(xì),供大家參考和學(xué)習(xí),感興趣的朋友可以了解下
    2020-06-06
  • C語言實現(xiàn)學(xué)生信息管理系統(tǒng)(文件操作)

    C語言實現(xiàn)學(xué)生信息管理系統(tǒng)(文件操作)

    這篇文章主要介紹了C語言實現(xiàn)學(xué)生信息管理系統(tǒng),增加了文件操作,文中示例代碼介紹的非常詳細(xì),具有一定的參考價值,感興趣的小伙伴們可以參考一下
    2022-06-06
  • C語言實現(xiàn)學(xué)生信息管理系統(tǒng)(多文件)

    C語言實現(xiàn)學(xué)生信息管理系統(tǒng)(多文件)

    這篇文章主要為大家詳細(xì)介紹了C語言實現(xiàn)學(xué)生信息管理系統(tǒng),文中示例代碼介紹的非常詳細(xì),具有一定的參考價值,感興趣的小伙伴們可以參考一下
    2020-12-12
  • Qt數(shù)據(jù)庫應(yīng)用之實現(xiàn)文件編碼格式識別

    Qt數(shù)據(jù)庫應(yīng)用之實現(xiàn)文件編碼格式識別

    在做數(shù)據(jù)導(dǎo)入導(dǎo)出的過程中,如果應(yīng)用場景多了,相信各位都會遇到一個問題就是文件編碼的問題。本文將用Qt實現(xiàn)文件編碼格式識別,感興趣的可以了解一下
    2022-06-06

最新評論

秦皇岛市| 阳江市| 永福县| 陇南市| 历史| 景洪市| 余干县| 泉州市| 察哈| 漳平市| 布尔津县| 错那县| 阿拉善左旗| 安西县| 承德县| 商城县| 滨海县| 黄石市| 阿坝县| 德令哈市| 德保县| 谢通门县| 保山市| 清远市| 梁平县| 阳朔县| 合水县| 兴国县| 河源市| 孝感市| 邢台市| 比如县| 武乡县| 时尚| 衡阳县| 湾仔区| 太仆寺旗| 皋兰县| 荣成市| 金山区| 云霄县|