C++进阶学习:STL常用算法

【C++】STL简介 STL简介。开始学习STL之前,是不是得先知道STL是个什么东西,以及如何学习STL呢?话不多说,开始我们今天的内容!是C++标准库的重要组成部分,不仅是一个可复用的组件库,而且是一个包罗数据结构与算法的软件框架。本篇博客主要介绍了有关STL的历史,主流版本,六大组件,以及其优缺点和学习方法等内容。希望能帮助到大家。♥C++ 阅读详情

概述:

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

        <algorithm> 是所有STL文件中最大的一个  范围涉及到 比较 交换 查找 遍历 复制 修改 等

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

        <functional> 定义了一些模板类  用于声明函数对象

1. 常用遍历算法

1.1 for_each

功能描述:

        实现遍历容器

常用

代码示例

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

//常用遍历算法 for_each

//普通函数
void print01(int val)
{
	cout << val << " ";
}

//仿函数
class print02
{
public:
	void operator()(int val)
	{
		cout << val << " ";
	}
};

void test01()
{
	vector<int> v;

	for (int i = 0;i < 10;i++)
	{
		v.push_back(i);
	}

	for_each(v.begin(), v.end(), print01);  //0 1 2 3 4 5 6 7 8 9
	cout << endl;

	for_each(v.begin(), v.end(), print02());  //0 1 2 3 4 5 6 7 8 9
	cout << endl;
}

int main()
{
	test01();
	system("pause");
	return 0;
}

1.2 transform

功能描述 :

        搬运一个容器到另一个容器

搬运时目标容器一定要提前开辟空间

代码示例

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

//常用遍历算法 transfrom

class Transfrom
{
public:
	int operator()(int val)
	{
		return val+100;
	}
};

class MyPrint
{
public:
	void operator()(int val)
	{
		cout << val << " ";
	}
};

void test01()
{
	vector<int> v;

	for (int i = 0;i < 10;i++)
	{
		v.push_back(i);
	}

	vector<int> vTarget;  //目标容器
	vTarget.resize(10);  //目标容器需要提前开辟空间

	transform(v.begin(), v.end(), vTarget.begin(), Transfrom());
	for_each(vTarget.begin(), vTarget.end(), MyPrint());
	cout << endl;

}

int main()
{
	test01();
	system("pause");
	return 0;
}

2. 常用查找算法

2.1 find

功能描述:

        查找指定容器  找到返回指定元素的迭代器  找不到返回end()

find 查找自定义数据类型时  需要重载==

代码示例

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

//常用查找算法 find

//查找内置数据类型
void test01()
{
	vector<int> v;

	for (int i = 0;i < 10;i++)
	{
		v.push_back(i);
	}

	//查找容器中是否有 5 这个元素
	vector<int>::iterator it = find(v.begin(), v.end(), 5);
	if(it == v.end())
	{
		cout << "没有找到" << endl;
	}
	else
	{
		cout << "找到了" << endl;
	}


}

//查找自定义数据类型
class Person
{
public:
	Person(string name, int age)
	{
		this->m_age = age;
		this->m_name = name;
	}
	bool operator==(const Person & p)
	{
		if (this->m_name == p.m_name && this->m_age == p.m_age)
		{
			return true;
		}
		return false;
	}

	string m_name;
	int m_age;
};

void test02()
{
	vector<Person> v;
	Person p1("aa", 10);
	Person p2("bb", 20);
	Person p3("cc", 30);
	Person p4("dd", 40);
	Person pp("bb", 20);

	//放到容器中
	v.push_back(p1);
	v.push_back(p2);
	v.push_back(p3);
	v.push_back(p4);

	vector<Person>::iterator it = find(v.begin(), v.end(), pp);
	if (it == v.end())
	{
		cout << "没有找到" << endl;
	}
	else
	{
		cout << "找到了" << endl;
		cout << "姓名:" << it->m_name << " 年龄:" << it->m_age << endl;
	}

}
int main()
{
	test01();
	test02();
	system("pause");
	return 0;
}

2.2 find_if

功能描述:

        按条件查找元素

按元素查找  找到返回指定位置的迭代器  找不到返回结束迭代器

代码示例

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

//常用查找算法 find_if

//查找内置数据类型

class GreateFive
{
public:
	bool operator()(int val)
	{
		return val > 5;
	}
};
void test01()
{
	vector<int> v;

	for (int i = 0;i < 10;i++)
	{
		v.push_back(i);
	}

	//查找容器中是否有 5 这个元素
	vector<int>::iterator it = find_if(v.begin(), v.end(), GreateFive());
	if(it == v.end())
	{
		cout << "没有找到" << endl;
	}
	else
	{
		cout << "找到大于5的数字为:"<<*it << endl;  //6
	}
}

//查找自定义数据类型
class Person
{
public:
	Person(string name, int age)
	{
		this->m_age = age;
		this->m_name = name;
	}
	bool operator==(const Person & p)
	{
		if (this->m_name == p.m_name && this->m_age == p.m_age)
		{
			return true;
		}
		return false;
	}

	string m_name;
	int m_age;
};

class Greater20
{
public:
	bool operator()(Person &p)
	{
		return p.m_age > 20;
	}
};

void test02()
{
	vector<Person> v;
	Person p1("aa", 10);
	Person p2("bb", 20);
	Person p3("cc", 30);
	Person p4("dd", 40);
	Person pp("bb", 20);

	//放到容器中
	v.push_back(p1);
	v.push_back(p2);
	v.push_back(p3);
	v.push_back(p4);

	//找年龄大于20的人
	vector<Person>::iterator it = find_if(v.begin(), v.end(), Greater20());
	if (it == v.end())
	{
		cout << "没有找到" << endl;
	}
	else
	{
		cout << "找到了" << endl;
		cout << "姓名:" << it->m_name << " 年龄:" << it->m_age << endl;
	}

}
int main()
{
	test01();
	test02();
	system("pause");
	return 0;
}

3. adjacent_find

功能描述:

        查找相邻重复元素

返回相邻元素的第一个迭代器位置

代码示例

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

//常用查找算法 adjacent_if

//查找相邻重复元素

void test01()
{
	vector<int> v;

	v.push_back(0);
	v.push_back(2);
	v.push_back(0);
	v.push_back(3);
	v.push_back(1);
	v.push_back(4);
	v.push_back(3);
	v.push_back(3);

	vector<int>::iterator it = adjacent_find(v.begin(), v.end());
	if(it == v.end())
	{
		cout << "没有找到相邻重复元素" << endl;
	}
	else
	{
		cout << "找到了相邻重复元素:"<<*it << endl;  //3
	}
}

int main()
{
	test01();
	system("pause");
	return 0;
}

4. binary_search

功能描述:

        查找指定元素是否存在

该算法查找效率很高  但是必须在有序的序列中使用

代码示例

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

//常用查找算法 binary_search

void test01()
{
	vector<int> v;

	for (int i = 0;i < 10;i++)
	{
		v.push_back(i);
	}

	//查找容器中是否有9元素
	bool ret = binary_search(v.begin(), v.end(), 9);

	if(ret)
	{
		cout << "找到了" << endl;
	}
	else
	{
		cout << "没有找到"<< endl;
	}
}

int main()
{
	test01();
	system("pause");
	return 0;
}

5. count

功能描述:

        统计元素个数

统计自定义数据类型  需要重载 == 

代码示例

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

//常用查找算法 count

//1、统计内置数据类型
void test01()
{
	vector<int> v;

	v.push_back(10);
	v.push_back(40);
	v.push_back(40);
	v.push_back(20);
	v.push_back(40);
	v.push_back(30);

	int num = count(v.begin(), v.end(), 40);
	cout << "40的元素个数是:"<<num << endl;  //3
}

//2、统计自定义数据类型

class Person
{
public:
	Person(string name, int age)
	{
		this->m_age = age;
		this->m_name = name;
	}
	bool operator ==(const Person &p)
	{
		if (this->m_age == p.m_age)
		{
			return true;
		}
		return false;
	}
	string m_name;
	int m_age;
};
void test02()
{
	vector<Person> v;

	Person p1("刘备", 35);
	Person p2("关羽", 35);
	Person p3("张飞", 35);
	Person p4("曹操", 40);
	Person p5("赵云", 20);

	Person p6("诸葛亮", 35);

	v.push_back(p1);
	v.push_back(p2);
	v.push_back(p3);
	v.push_back(p4);
	v.push_back(p5);

	int num = count(v.begin(), v.end(), p6);
	cout << "和诸葛亮同岁人个数是:" << num << endl;   //3
}
int main()
{
	test01();
	test02();
	system("pause");
	return 0;
}

6. count_if

功能描述:

        按条件统计元素个数

代码示例

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

//常用查找算法 count_if

//1、统计内置数据类型
class Greater20
{
public:
	bool operator()(int val)
	{
		return val > 20;
	}
};

void test01()
{
	vector<int> v;

	v.push_back(10);
	v.push_back(40);
	v.push_back(40);
	v.push_back(20);
	v.push_back(40);
	v.push_back(30);

	int num = count_if(v.begin(), v.end(), Greater20());
	cout << "容器中大于20的元素个数是:"<<num << endl;  //4
}

//2、统计自定义数据类型

class Person
{
public:
	Person(string name, int age)
	{
		this->m_age = age;
		this->m_name = name;
	}
	bool operator ==(const Person &p)
	{
		if (this->m_age == p.m_age)
		{
			return true;
		}
		return false;
	}
	string m_name;
	int m_age;
};
class AgeGreater20
{
public:
	bool operator()(const Person &p)
	{
		return p.m_age > 20;
	}
};
void test02()
{
	vector<Person> v;

	Person p1("刘备", 35);
	Person p2("关羽", 35);
	Person p3("张飞", 35);
	Person p4("曹操", 40);
	Person p5("赵云", 20);

	v.push_back(p1);
	v.push_back(p2);
	v.push_back(p3);
	v.push_back(p4);
	v.push_back(p5);

	int num = count_if(v.begin(), v.end(), AgeGreater20());
	cout << "大于20岁的人的个数是:" << num << endl;   //4
}
int main()
{
	test01();
	test02();
	system("pause");
	return 0;
}

3. 常用排序算法

3.1 sort

功能描述:

        对容器中的数据进行排序

很常用

代码示例

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

//常用排序算法 sort

void myPrint(int val)
{
	cout << val << " ";
}

void test01()
{
	vector<int> v;

	v.push_back(10);
	v.push_back(40);
	v.push_back(40);
	v.push_back(20);
	v.push_back(40);
	v.push_back(30);

	//利用sort进行升序
	sort(v.begin(), v.end());
	for_each(v.begin(), v.end(), myPrint);
	cout << endl;

	//改变为降序
	sort(v.begin(), v.end(), greater<int>());
	for_each(v.begin(), v.end(), myPrint);
	cout << endl;
}

int main()
{
	test01();
	system("pause");
	return 0;
}

3.2 random_shuffle

功能描述:

        指定范围内的数据随机打乱

增加随机数种子  确保真实随机

代码示例

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

//常用排序算法 random_shuffle

void myPrint(int val)
{
	cout << val << " ";
}

void test01()
{
	srand((unsigned int)time(NULL));  //随机种子
	vector<int> v;

	for (int i = 0;i < 10;i++)
	{
		v.push_back(i);
	}

	//利用排序算法  打乱顺序
	random_shuffle(v.begin(), v.end());
	for_each(v.begin(), v.end(), myPrint);
	cout << endl;
}

int main()
{
	test01();
	system("pause");
	return 0;
}

3.3 merge

功能描述:

        将两个容器元素合并到一个容器

必须是两个有序容器  合成的结果也是有序的

使用前切记给目标容器开辟空间

代码示例

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

//常用排序算法 random_shuffle

void MyPrint(int val)
{
	cout << val << " ";
}

void test01()
{
	vector<int> v1;
	vector<int> v2;

	for (int i = 0;i < 10;i++)
	{
		v1.push_back(i);
		v2.push_back(i+1);
	}

	//目标容器
	vector<int> v;
	v.resize(20);
	merge(v1.begin(), v1.end(), v2.begin(), v2.end(), v.begin());
	for_each(v.begin(), v.end(),MyPrint);  //0 1 1 2 2 3 3 4 4 5 5 6 6 7 7 8 8 9 9 10
	cout << endl;
}

int main()
{
	test01();
	system("pause");
	return 0;
}

3.4 reverse

功能描述:

        将容器内元素反转

代码示例

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

//常用排序算法 reverse

void MyPrint(int val)
{
	cout << val << " ";
}

void test01()
{
	vector<int> v1;
	vector<int> v2;

	v1.push_back(50);
	v1.push_back(30);
	v1.push_back(20);
	v1.push_back(40);
	v1.push_back(10);

	cout << "反转前" << endl;
	for_each(v1.begin(), v1.end(),MyPrint);  //0 1 1 2 2 3 3 4 4 5 5 6 6 7 7 8 8 9 9 10
	cout << endl;
	reverse(v1.begin(), v1.end());
	cout << "反转后" << endl;
	for_each(v1.begin(), v1.end(), MyPrint);  //0 1 1 2 2 3 3 4 4 5 5 6 6 7 7 8 8 9 9 10
	cout << endl;
}

int main()
{
	test01();
	system("pause");
	return 0;
}

4. 常用拷贝算法

4.1 copy

功能描述:

        容器内指定范围的元素拷贝到另一容器中

目标容器记得提前开辟空间

代码示例

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

//常用拷贝和替换算法 copy

void MyPrint(int val)
{
	cout << val << " ";
}

void test01()
{
	vector<int> v1;

	for (int i = 0;i < 10;i++)
	{
		v1.push_back(i);
	}
	vector<int> v2;
	v2.resize(10);
	copy(v1.begin(), v1.end(), v2.begin());
	for_each(v1.begin(), v1.end(),MyPrint);  //0 1 2 3 4 5 6 7 8 9
	cout << endl;
	for_each(v2.begin(), v2.end(), MyPrint);  //0 1 2 3 4 5 6 7 8 9
	cout << endl;
}

int main()
{
	test01();
	system("pause");
	return 0;
}

4.2 replace

功能描述:

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

会替换区间内所有满足条件的元素

代码示例

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

//常用拷贝和替换算法 copy

void MyPrint(int val)
{
	cout << val << " ";
}

void test01()
{
	vector<int> v1;

	for (int i = 0;i < 9;i++)
	{
		v1.push_back(i);
	}
	v1.push_back(1);
	cout << "替换前:" << endl;
	for_each(v1.begin(), v1.end(),MyPrint);  //0 1 2 3 4 5 6 7 8 1
	cout << endl;	

	//替换
	replace(v1.begin(), v1.end(), 1, 100);
	cout << "替换后:" << endl;
	for_each(v1.begin(), v1.end(), MyPrint);  //0 100 2 3 4 5 6 7 8 100
	cout << endl;
}

int main()
{
	test01();
	system("pause");
	return 0;
}

4.3 replace_if

功能描述:

        将区间内满足条件的元素  替换为新元素

利用仿函数可以灵活调整替换条件

代码示例

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

//常用拷贝和替换算法 copy

void MyPrint(int val)
{
	cout << val << " ";
}

class Greater30
{
public:
	bool operator ()(int val)
	{
		return val >= 3;
	}
};

void test01()
{
	vector<int> v1;

	v1.push_back(1);
	v1.push_back(4);
	v1.push_back(5);
	v1.push_back(3);
	v1.push_back(6);
	v1.push_back(2);
	v1.push_back(7);
	v1.push_back(4);

	//大于等于3的替换为13
	cout << "替换前:" << endl;
	for_each(v1.begin(), v1.end(),MyPrint);  //1 4 5 3 6 2 7 4
	cout << endl;	

	//替换
	replace_if(v1.begin(), v1.end(), Greater30(), 13);
	cout << "替换后:" << endl;
	for_each(v1.begin(), v1.end(), MyPrint);  //1 13 13 13 13 2 13 13
	cout << endl;
}

int main()
{
	test01();
	system("pause");
	return 0;
}

4.4 swap

功能描述:

        互换两个容器的元素

必须是两个同种容器

代码示例

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

//常用拷贝和替换算法 copy

void MyPrint(int val)
{
	cout << val << " ";
}

class Greater30
{
public:
	bool operator ()(int val)
	{
		return val >= 3;
	}
};

void test01()
{
	vector<int> v1;

	vector<int> v2;

	v1.push_back(1);
	v1.push_back(4);
	v1.push_back(5);
	v1.push_back(3);
	v1.push_back(6);

	v2.push_back(10);
	v2.push_back(40);
	v2.push_back(50);
	v2.push_back(30);
	v2.push_back(60);

	cout << "交换前:" << endl;
	for_each(v1.begin(), v1.end(),MyPrint);  //1 4 5 3 6
	cout << endl;
	for_each(v2.begin(), v2.end(),MyPrint);  //10 40 50 30 60
	cout << endl;	

	swap(v1,v2);
	//替换
	cout << "交换后:" << endl;
	for_each(v1.begin(), v1.end(), MyPrint);  //10 40 50 30 60
	cout << endl;
	for_each(v2.begin(), v2.end(), MyPrint);  //1 4 5 3 6
	cout << endl;
	cout << endl;
}

int main()
{
	test01();
	system("pause");
	return 0;
}

5. 常用算术生成算法

算术生成算法属于小型算法  需要包含头文件 <numeric>

5.1 accumulate

功能描述:

        计算区间内容器元素累计求和

函数的第三个参数是一个起始的累加值

代码示例

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

//常用拷贝和替换算法 copy

void test01()
{
	vector<int> v1;

	for (int i = 0;i <= 100;i++)
	{
		v1.push_back(i);
	}
	//参数3是一个起始的累加值
	int total = accumulate(v1.begin(), v1.end(), 0);
	
	cout << total << endl;  //5050
}

int main()
{
	test01();
	system("pause");
	return 0;
}

5.2 fill

功能描述:

        向容器中填充指定元素

代码示例

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

//常用拷贝和替换算法 copy

void MyPrint(int val)
{
	cout << val << " ";
}

void test01()
{
	vector<int> v1;

	v1.resize(10);

	//后期重新填充
	fill(v1.begin(), v1.end(), 100);

	for_each(v1.begin(), v1.end(), MyPrint);  //100 100 100 100 100 100 100 100 100 100
	cout << endl;
}

int main()
{
	test01();
	system("pause");
	return 0;
}

6. 常用集合算法

6.1 set_intersection

 功能描述:

        获取两个容器的交集

求交集的两个容器必须是有序序列

目标容器的开辟空间需要从两个容器中取最小值

set_intersection 算法的返回值是交集中最后一个元素的位置

代码示例

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

//常用集合算法 set_intersection

void MyPrint(int val)
{
	cout << val << " ";
}

class Greater30
{
public:
	bool operator ()(int val)
	{
		return val >= 3;
	}
};

void test01()
{
	vector<int> v1;
	vector<int> v2;

	for (int i = 0;i < 10;i++)
	{
		v1.push_back(i);
		v2.push_back(i+5);
	}
	
	//目标容器需要提前开辟空间
	vector<int> v;
	v.resize(min(size(v1), size(v2)));

	//获取交集
	vector<int>::iterator itend = set_intersection(v1.begin(), v1.end(), v2.begin(), v2.end(), v.begin());

	for_each(v.begin(), itend, MyPrint);  //5 6 7 8 9
	cout << endl;
}

int main()
{
	test01();
	system("pause");
	return 0;
}

6.2 set_union

 功能描述:

        获取两个容器的并集

求并集的两个容器必须是有序序列

目标容器的开辟空间应该是两个容器的 size 相加

set_union 算法的返回值是并集中最后一个元素的位置

代码示例

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

//常用集合算法 set_union

void MyPrint(int val)
{
	cout << val << " ";
}

class Greater30
{
public:
	bool operator ()(int val)
	{
		return val >= 3;
	}
};

void test01()
{
	vector<int> v1;
	vector<int> v2;

	for (int i = 0;i < 10;i++)
	{
		v1.push_back(i);
		v2.push_back(i+5);
	}
	
	//目标容器需要提前开辟空间
	vector<int> v;
	v.resize(size(v1)+size(v2));
	vector<int>::iterator itend = set_union(v1.begin(), v1.end(), v2.begin(), v2.end(), v.begin());

	for_each(v.begin(), itend, MyPrint);  //0 1 2 3 4 5 6 7 8 9 10 11 12 13 14
	cout << endl;
}

int main()
{
	test01();
	system("pause");
	return 0;
}

6.3 set_difference

 功能描述:

        获取两个容器的并集

两个容器的顺序会影响结果

求并集的两个容器必须是有序序列

目标容器的开辟空间应该是两个容器的 size 中较大的值

set_difference 算法的返回值是并集中最后一个元素的位置

代码示例

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

//常用集合算法 set_difference

void MyPrint(int val)
{
	cout << val << " ";
}

class Greater30
{
public:
	bool operator ()(int val)
	{
		return val >= 3;
	}
};

void test01()
{
	vector<int> v1;
	vector<int> v2;

	for (int i = 0;i < 10;i++)
	{
		v1.push_back(i);
		v2.push_back(i+5);
	}
	
	//目标容器需要提前开辟空间
	vector<int> v;
	v.resize(max(size(v1),size(v2)));

	cout << "v1 和 v2 的差集:" << endl;
	vector<int>::iterator itend = set_difference(v1.begin(), v1.end(), v2.begin(), v2.end(), v.begin());

	for_each(v.begin(), itend, MyPrint);  //0 1 2 3 4
	cout << endl;

	cout << "v2 和 v1 的差集:" << endl;
	itend = set_difference(v2.begin(), v2.end(), v1.begin(), v1.end(), v.begin());

	for_each(v.begin(), itend, MyPrint);  //10 11 12 13 14
	cout << endl;
}

int main()
{
	test01();
	system("pause");
	return 0;
}

C++ STL常用算法 学习总结 end1 容器1结束迭代器 beg2 容器2开始迭代器 end2 容器2结束迭代器 dest 目标容器开始迭代器。beg1 容器1开始迭代器,end1 容器1结束迭代器,beg2 容器2开始迭代器,end2 容器2结束迭代器。beg 开始迭代器,end 结束迭代器,oldvalue 旧元素,newvalue 新元素。beg 开始迭代器,end 结束迭代器,_Pred 谓词,newvalue 替换的新元素。查找指定元素,找到返回指定元素的迭代器,找不到返回结束迭代器end() 阅读详情

相关推荐

C++进阶学习--STL常用算法总结

目录 一、引言 二、迭代器 ------> 2.1、访问顺序/操作算法 ------> 2.2、读写元素 三、STL常用容器 ------> 3.1、序列容器 ------------> 3.1.1、vector 一、引言 前一章大致介绍了在STL中常用的几种容器类,本章来介绍下STL中常用的算法 https://www.cnblogs.com/hustsss/p/11177040.html https://blog.youkuaiyun.com/wdl20170204/article/deta

文艺小少年的博客 754

C++ 算法进阶系列之剖析树型动态规划算法思想

什么是树型动态规划?概念中有2一个是动态规划概念。动态规划可以简单理解为通过对已经计算出来的子问题的状态值进行修改(基于子问题的状态值找到当前子问题的最优值)而得到当前子问题的状态值。本文侧重于动态规划在树型结构中的应用,更多关于动态规划的基础理论和常规知识,可以翻阅相关资料。树指树数据结构。树型动态规划算法指在树逻辑结构之上提供的动态规划思想。动态规划最重要环节查找到子问题间发生变化的状态量以及状态转移表达式。树型动态规划的状态转移过程一般都是由子树向根结点转移,这也符合动态规划的由底向上的逻辑思想。

y6123236的专栏 487

C++进阶算法合集--ACM必备

C++进阶算法合集--ACM必备 主要讲解一些算法和用法,具体是代码实现,后期还会完善,是学习的好资料

探索C++算法奥秘:四种进阶算法精讲与实战

快速排序是一种高效排序算法,采用分治策略。通过选择一个元素作为“基准”,将数据分为小于基准和大于基准的两部分,再递归地排序两部分。

aoligei167167的博客 689

数据中心GPU芯片性能参数速查表(Nvidia+国产算力卡)

本文提供了一份数据中心GPU芯片性能参数速查表,涵盖Nvidia与国产算力卡两大阵营。文章详细解读了算力、显存、互连技术及功耗等关键性能指标,并对比了A100、H100、昇腾910B等主流型号,旨在帮助用户根据AI训练、推理等实际场景,做出高效的硬件选型与成本评估决策。

a0b1c2d3的专栏 835

jmeter5.4.1 使用案例

jmeter主要用于接口压力测试,模拟海量请求并发访问接口 1.下载jmeter https://jmeter.apache.org/download_jmeter.cgi 2.启动jemter 双击即可 3.配置jmeter 1.先更改语言 2.创建线程组 配置线程执行规划 3.创建http请求默认值 4.创建http请求 5.创建http信息头管理器 指定 Content-Type:application/json 6.创建响应断言 7.创建察看结果数 直接点击 执行 按.

SpringHASh的博客 1221

算法之路——C++的初步到进阶

提供智能指针(如 std::unique_ptr、std::shared_ptr、std::weak_ptr)和内存管理相关的工具。插入、删除和查找操作的平均时间复杂度为O(1) ,但在最坏情况下可能达到O(n)。动态数组,可随机访问,尾部插入和删除元素效率高,时间复杂度O(1);std::cout 用于向控制台输出内容,<< 是流插入运算符,std::endl 表示换行并刷新缓冲区。:std::cin 进行输入、std::cout 进行输出、std::cerr 输出错误信息。用于单遍输出算法,如。

2403_88090280的博客 747

Ansys Zemax | 多模光纤耦合效率优化与IMAE操作数实战解析

本文详细解析了在Ansys Zemax(OpticStudio)中优化多模光纤耦合效率的实战流程。通过几何图像分析诊断初始效率,并重点介绍了如何利用IMAE操作数将分析工具转化为自动化优化引擎,实现从2%到54%的几何效率飞跃。文章还探讨了引入菲涅尔损耗与偏振计算以逼近真实效率,并分享了关键的避坑指南与高阶技巧。

xk7n92h3p5m的博客 748

C++算法进阶系列之倍增算法 ST 表

区间查询有很多方式,ST是不错的选择。

y6123236的专栏 1533

MS1030超声波流量计电路实战:如何用15ps精度优化水表设计(附配置代码)

本文深入探讨了如何利用MS1030超声波流量计芯片实现15ps高精度测量,以优化水表设计。文章从系统误差分配出发,详细解析了硬件电路设计要点、SPI通信配置及核心优化技巧,如利用可编程偏置电压和8回波累加功能来提升抗干扰能力与测量稳定性,并附有实用的配置代码示例。

svm4gardener的博客 332

STM32 移植FreeModbus详细过程

modbus是一个非常好的串口协议(当然也能用在网口上),它简洁、规范、强大。可以满足大部分的工业、嵌入式需求。 这里详细说下如何将freemodbus移植到stm32平台。我之前下载的版本是1.6,我会在我的下载链接里面加上1.6的freemodbus压缩包以及我已经移植好的stm32 for freemodbus版本。我上传的移植好的绝对没问题,经过多个板子测试。这里编译器使用的是keil5.28,单片机用的是STM32F103RCT6。 移植到VET6以及ZET6的话只需要改一下选择的芯片就好,改法:

qq_40305944的博客 8万+

c++学习--c++进阶

c++学习---时间复杂度,时间复杂度计算,二分法,前缀和,差分

m0_63599362的博客 1375

如何用 Claude Code 阅读和理解陌生代码

摘要: 在接手陌生代码时,Claude Code能显著提升理解效率。核心策略是结构化拆解而非逐行阅读: 从整体入手:先扫描目录结构、定位核心模块,建立项目地图; 聚焦关键路径:通过数据流追踪、异常逻辑分析,识别技术债与风险点; 规避常见错误:避免抽象提问或一次性提交全部代码,应分模块精准提问; 高阶应用:可生成逻辑分层图、标记高复杂度区域,甚至定制阅读顺序。 最终目标是通过结构优先的提问策略,将理解周期从数天压缩至数小时。

sg_knight的专栏 1019

STL模板库:

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38...

weixin_52995261的博客 549

医学图像处理实战:利用OpenSlide解析病理SVS切片的完整解决方案

本文介绍了使用openslide处理病理SVS切片的完整流程,从基础安装到科研应用。主要内容包括:1)SVS切片特点及openslide工具简介;2)环境配置与软件安装;3)基础操作:读取切片、查看信息、可视化不同层级图像;4)进阶操作:坐标裁剪与批量处理;5)科研方向拓展:结合AI模型进行病理分割分类、量化分析及可视化报告。教程提供了完整的代码示例,帮助毕业设计学生快速掌握病理图像分析的核心技能。

m0_71589036的博客 309

Python 实战:爬取高德地图交通数据(路况拥堵指数分析)

高德地图是由高德软件有限公司开发的一款地图导航产品,提供详细的地图浏览、路线规划、实时路况、公共交通查询等功能。高德地图拥有庞大的用户群体和海量的交通数据,这些数据对于交通管理、城市规划、出行服务等领域具有重要的价值。通过对高德地图交通数据的分析,我们可以了解城市交通的运行状况,识别拥堵路段,预测交通流量,为交通管理部门和出行者提供决策支持。

yansideyucsdn的博客 2684

STL教程:C++ STL快速入门

1.STL引言 STL 是“Standard Template Library”的缩写,中文译为“标准模板库”。STL 是 C++ 标准库的一部分,不用单独安装。C++ 对模板(Template)支持得很好,STL 就是借助模板把常用的数据结构及其算法都实现了一遍,并且做到了数据结构和算法的分离。例如,vector 的底层为顺序表(数组),list 的底层为双向链表,deque 的底层为循环队列...

梦心之魂的博客 1万+

蓝牙模块HM-10手册

蓝牙模块HM-10手册,该手册对蓝牙模块HM-10进行了详细的介绍和使用方法以及一些AT指令。

【C++进阶】深入STL之vector:构建高效C++程序的基石

vector容器允许我们存储任意数量的同类型元素,并且能够根据需要进行动态扩展。这种灵活性使得vector在处理大量数据时变得尤为高效,无论是在科学计算、图形处理、网络编程还是游戏开发等领域,我们都能看到vector的身影。 现在让我们一起踏上学习STL中vector的旅程吧!

EterNity_TiMe_的计算机学习之路 2655

最新的sdio 4.0 spec 协议标准 带书签 高清完整PDF版

Part-E1-SDIO-Specification-Ver4.00-Final-120220.pdf:最新的sdio 4.0 spec 协议标准 带书签 高清完整PDF版,包括SDIO 接口的外设,SDIO 接口官方协议等。

精雕软件(JDpaint) 5.5 绿色版

精雕软件(JDpaint) 5.5 绿色版精雕软件(JDpaint) 5.5 绿色版精雕软件(JDpaint) 5.5 绿色版

C++学习笔记之stl库及常用函数

C++是一种功能强大、灵活的编程语言,广泛应用于系统软件、应用软件、设备驱动、嵌入式软件、高性能服务器和客户端应用程序、游戏引擎等。

sincerelover的博客 3360

常用 C++ STL 用法

STL

wfs66的博客 1793

【c++】STL简介

STL(Standard Template Library,标准模板库),是c++标准库的一个重要组成部分,它提供了一系列通用的类模板和函数,是一个包罗数据结构和算法的软件框架。STL的出现弥补了c++开发效率的不足,大大提高了程序的可复用性。

2301_80955819的博客 2442

C++模板与STL【常用算法】

推荐关注博主,随时获取关于C++的最新技术分享。

m0_51574586的博客 1481

深入探索C++ STL:从基础到进阶

STL(standard template library - 标准模板库)是C++ 标准库的重要组成部分。它就像是一个工具箱,里面装满了各种数据结构(如vector、list、map等)和算法(如sort、find等)。以下是一个简单使用 vector 容器的示例代码:cpp// 创建一个vector容器,用于存储int类型的数据// 向容器中添加元素// 遍历容器并输出元素++i) {return 0;难点剖析这里涉及到模板的概念,模板使得STL能够适应不同的数据类型。

nplplus的博客 1473

【C++】STL学习小总结

【C++】STL学习小总结 STL概述 长久以来,软件界一直希望建立一种可重复利用的东西,以及一种得以制造出”可重复运用的东西”的方法,从函数(functions),类别(classes),函数库(function libraries),类别库(class libraries)、各种组件,从模块化设计,到面向对象(object oriented ),为的就是复用性的提升。 复用性必须建立在某种标准之上。但是在许多环境下,就连软件开发最基本的数据结构(data structures) 和算法(algorith

ZYP_997_的博客 4661

c++ STL学习笔记

学习目标:vector中存放自定义数据类型,并打印输出//自定义数据类型public:mAge = age;//存放对象//创建数据it!= v.end();it++) {//*it解引用出来的数据类型为vector里的数据类型//放对象指针//创建数据it!= v.end();it++) {

jhxjxjs的博客 1619

STL常用算法笔记

STL常用算法笔记、方便查阅、b站黑马程序员的c++

知道的越多,不知道的越多 1276
上一篇: C++进阶学习:STL函数对象
下一篇: C++实现基于STL的演讲比赛流程管理系统
Miki Makimura
博客等级 码龄4年 124粉丝 56原创
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

个

红包个数最小为10个

元

红包金额最低5元

当前余额3.43元 前往充值 >
需支付:10.00元
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付元
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值