全球最实用的IT互联网信息网站!

AI人工智能P2P分享&下载搜索网页发布信息网站地图

当前位置:诺佳网 > 电子/半导体 > 嵌入式技术 >

C++之STL算法(二)

时间:2023-07-18 14:49

人气:

作者:admin

标签: C++  STL  算法 

导读:C++之STL算法(二)...

1.sort排序算法

sort(const _RanIt _First, const _RanIt _Last, _Pr _Pred)  --默认为升序排序
形参:_First、_Last --容器的起始和结束迭代器
	  _Pred --排序规则,默认为从小到大

示例:

#include < iostream >
#include < vector >
#include < algorithm >
using namespace std;
class Print
{
public:
	void operator()(int val)
	{
		cout < < val < < " ";
	}
};
void test()
{
	vector< int >vtr;
	vtr.push_back(rand()%50);
	vtr.push_back(rand() % 50);
	vtr.push_back(rand() % 50);
	vtr.push_back(rand() % 50);
	vtr.push_back(rand() % 50);
	vtr.push_back(rand() % 50);
	for_each(vtr.begin(), vtr.end(), Print());
	cout < < endl;
	cout < < "升序排序:" < < endl;
	sort(vtr.begin(), vtr.end());
	for_each(vtr.begin(), vtr.end(), Print());
	cout < < endl;
	cout < < "降序排序:" < < endl;
	sort(vtr.begin(), vtr.end(), greater< int >());
	for_each(vtr.begin(), vtr.end(), Print());
	cout < < endl;
}
int main()
{
	test();
	system("pause");
}
wKgaomS2MleAaM1cAARtD7TUTa0441.png

2.random_shuffle打乱顺序(洗牌)


打乱有序数列,重新洗牌:
void random_shuffle(_RanIt _First, _RanIt _Last);
形参:_First、_Last --起始和结束迭代器
#include < iostream >
using namespace std;
#include < vector >
#include < algorithm >
#include < map >
#include < ctime >
class Person
{
	friend class Print;
public:
	Person() {}
	Person(string name, int age) :name(name), age(age) {

	}
	bool operator< (const Person p)const
	{
		if (age == p.age)
		{
			return name < p.name;
		}
		return age < p.age;
	}
	string name;
	int age;

};
class Print
{
public:
	void operator()(int val)
	{
		cout < < val < < " ";
	}
	void operator()(Person& p)
	{
		cout < < "姓名:" < < p.name < < "t年龄:" < < p.age < < endl;
	}

};
void test()
{
	vector< int >vtr;
	vtr.resize(10);
	for (int i = 0; i < 10; i++)
	{
		vtr[i] = i;
	}
	for_each(vtr.begin(), vtr.end(), Print());
	cout < < endl;
	cout < < "洗牌后:" < < endl;
	random_shuffle(vtr.begin(), vtr.end());
	for_each(vtr.begin(), vtr.end(), Print());
	cout < < endl;
}

void test02()
{
	cout < < "t自定义数据:" < < endl;
	vector< Person >vtr;
	vtr.push_back(Person("小王", 1));
	vtr.push_back(Person("小王", 2));
	vtr.push_back(Person("小王", 3));
	vtr.push_back(Person("小李", 1));
	vtr.push_back(Person("小李", 2));

	for_each(vtr.begin(), vtr.end(), Print());
	cout < < "洗牌后:" < < endl;
	random_shuffle(vtr.begin(), vtr.end());
	for_each(vtr.begin(), vtr.end(), Print());
}
class mapPrint
{
public:
	void operator()(pair< Person, int >p)
	{
		cout < < "姓名:" < < (p.first).name < < "t年龄:" < < (p.first).age < < "t得分:" < < p.second < < endl;
	}
	
};
int main()
{
	srand(time(NULL));//random_shuffle底层需要随机数种子,否则每次生成结果一样
	test();
	test02();
	system("pause");
}
wKgZomS2MyOAParyAATTtbRgkRM915.png

3.merge合并容器


容器合并:
	merge()实现吧两个容器合并在一起,存放到第三个容器中。
	注意:merge()合并一定要保证容器元素有序,默认是从小到大的顺序。
	merge(_InIt1 _First1, _InIt1 _Last1, _InIt2 _First2, _InIt2 _Last2, _OutIt _Dest)  -->默认从小到大
	merge(_InIt1 _First1, _InIt1 _Last1, _InIt2 _First2, _InIt2 _Last2, _OutIt _Dest, _Pr _Pred) -->重载版本,支持自定义排序规则
	_First1、_Last1 --第一个容器的起始和结束迭代器
	_Last2、_Dest --第二个元素的起始和结束迭代器
	_Dest --要存储的新容器起始迭代器
	_Pred --谓词,设定排序规则
谓词:
	函数对象返回中为bool类;
	函数对象形参只有一个  --> 一元谓词
	函数对象形参有两个  -->  二元谓词

示例:

#include < iostream >
#include < algorithm >
#include < vector >
#include < map >
using namespace std;
class Print
{
public:
	void operator()(int val)
	{
		cout < < val < < " ";
	}
};
void test01()
{
	vector< int >v1;
	vector< int >v2;
	for (int i = 0; i < 5; i++)
	{
		v1.push_back(i);
	}

	v2.push_back(2);
	v2.push_back(16);
	v2.push_back(12);
	v2.push_back(14);
	sort(v2.begin(), v2.end(), less< int >());
	vector< int >v3;
	v3.resize(v1.size() + v2.size());
	cout < < "从小到大于有序合并:" < < endl;
	merge(v1.begin(), v1.end(), v2.begin(), v2.end(), v3.begin());
	for_each(v3.begin(), v3.end(), Print());
	cout< ());
	sort(v2.begin(), v2.end(), greater< int >());
	merge(v1.begin(), v1.end(), v2.begin(), v2.end(), v3.begin(),greater< int >());
	for_each(v3.begin(), v3.end(), Print());
	cout < < endl;
}

int main()
{
	test01();
	system("pause");
}
;>
wKgaomS2M5CAHW_ZAAUfDFpZcos785.png

4.reverse元素反转

函数功能: 元素反转,将容器中的元素前后颠倒
	reverse(const _BidIt _First, const _BidIt _Last)
	形参:_First、_Last --起始和结束迭代器
#include < iostream >
#include < vector >
#include < algorithm >
using namespace std;
class Person
{
public:
	Person() {}
	Person(string name, int age) :name(name), age(age) {

	}
	Person(const Person& p)
	{
		this->age = p.age;
		name = p.name;
	}
	bool operator< ( Person p)const 
	{
		if (age == p.age)return name < p.name;
		return age < p.age;
	}
	string name;
	int age;
};
class Print
{
public:
	void operator()(Person p)
	{
		cout < < "姓名:" < < p.name < < "t年龄:" < < p.age < < endl;
	}
};
void test()
{
	vector< Person >vtr;
	vtr.push_back(Person("小王",18));
	vtr.push_back(Person("小刘", 15));
	vtr.push_back(Person("小林", 20));
	for_each(vtr.begin(), vtr.end(), Print());
	cout < < "反转:" < < endl;
	reverse(vtr.begin(), vtr.end());
	for_each(vtr.begin(), vtr.end(), Print());
}

int main()
{
	test();
	system("pause");
}
wKgZomS2NDGAIdujAAP94cWoEYY680.png

5.copy元素拷贝

_OutIt copy(_InIt _First, _InIt _Last, _OutIt _Dest)
形参:_First、_Last --原容器的起始和结束位置
      _Dest --目标容器的起始位置
该函数功能类似于重载运算符=功能
#include < iostream >
#include < algorithm >
#include < vector >
void test()
{
	std::vector< std::string >vtr, vtr2;
	vtr.push_back("C++");
	vtr.push_back("copy算法");
	vtr.push_back("学习");
	vtr.push_back("案例");	
	std::cout < < "copy算法使用示例:" < < std::endl;
	vtr2.resize(vtr.size());
	copy(vtr.begin(), vtr.end(), vtr2.begin());//copy函数类似于赋值操作,vtr2=vtr1
	for (std::vector< std::string >::iterator p = vtr2.begin(); p != vtr2.end(); p++)
	{
		std::cout < < *p < < " ";
	}
	std::cout < < std::endl;
}
int main()
{
	test();
	system("pause");
}
wKgaomS2NMqASSfmAAOw2IjA5HA706.png

6.replace元素替换

元素替换
void replace(const _FwdIt _First, const _FwdIt _Last, const _Ty& _Oldval, const _Ty& _Newval)
形参:_First、_Last --要替换的数据区间
     _Oldval  --要替换的内容
	 _Newval  --替换后的内容
#include < vector >
#include < iostream >
using namespace std;
#include < algorithm >
using namespace std;
class Print
{
public:
	void operator()(int val)
	{
		cout < < val < < " ";
	}
};
void test()
{
	vector< int >vtr;
	vtr.push_back(10);
	vtr.push_back(10);
	vtr.push_back(30);
	vtr.push_back(10);
	vtr.push_back(35);
	vtr.push_back(10);
	cout < < "替换前:" < < endl;
	for_each(vtr.begin(), vtr.end(), Print());
	cout < < endl;
	cout < < "将10替换成666" < < endl;
	replace(vtr.begin(), vtr.end(), 10, 666);
	for_each(vtr.begin(), vtr.end(), Print());
	cout < < endl;
}
int main()
{
	test();
	system("pause");
}
wKgaomS2NR6AcbESAAPM0PgQAfQ713.png

7.条件替换replace_if

条件替换
 replace_if(const _FwdIt _First, const _FwdIt _Last, _Pr _Pred, const _Ty& _Val)
 形参:_First、_Last  --要替换的区间
       _Pred  --谓词,替换条件
	   _Val  --替换后的值

示例:

#include < iostream >
#include < vector >
#include < functional >
#include < algorithm >
using namespace std;
class Myreplace
{
public:
	bool operator()(int val)
	{
		return val >= 30;
	}

};
void test()
{
	vector< int >vtr;
	vtr.push_back(10);
	vtr.push_back(10);
	vtr.push_back(30);
	vtr.push_back(10);
	vtr.push_back(50);
	vtr.push_back(10);
	cout < < "将所有的 >=30的值替换为666" < < endl;
	replace_if(vtr.begin(), vtr.end(), Myreplace(), 666);
	for (vector< int >::iterator ptr = vtr.begin();ptr != vtr.end();ptr++)
	{
		cout < < *ptr < < " ";
	}
	cout < < endl;

}
int main()
{
	test();
	system("pause");
}
wKgaomS2NYWAPTU2AAMPqWUslQk200.png

8.容器互换swap

容器元素互换:
	swap(container v1,container v2);
	将v1和v2的容器元素进行互换,类似于成员函数swap();
#include < iostream >
using namespace std;
#include < algorithm >
#include < vector >
class Print
{
public:
	void operator()(int val)
	{
		cout < < val < < " ";
	}
};
void test()
{
	vector< int >vtr1 = { 10,20,30,50 };
	vector< int >vtr2 = { 10,29,88 };
	cout < < "替换前:" < < endl;
	for_each(vtr1.begin(), vtr1.end(), Print());
	cout < < endl;
	for_each(vtr2.begin(), vtr2.end(), Print());
	cout < < endl;
	swap(vtr1, vtr2);
	cout < < "替换结果:" < < endl;
	for_each(vtr1.begin(), vtr1.end(), Print());
	cout < < endl;
	for_each(vtr2.begin(), vtr2.end(), Print());
	cout < < endl;
}
int main()
{
	test();
	system("pause");
}
wKgaomS2NeuAN7ZbAAQnGVB6dtc195.png

温馨提示:以上内容整理于网络,仅供参考,如果对您有帮助,留下您的阅读感言吧!
相关阅读
本类排行
相关标签
本类推荐

CPU | 内存 | 硬盘 | 显卡 | 显示器 | 主板 | 电源 | 键鼠 | 网站地图

Copyright © 2025-2035 诺佳网 版权所有 备案号:赣ICP备2025066733号
本站资料均来源互联网收集整理,作品版权归作者所有,如果侵犯了您的版权,请跟我们联系。

关注微信