IT数码 购物 网址 头条 软件 日历 阅读 图书馆
TxT小说阅读器
↓语音阅读,小说下载,古典文学↓
图片批量下载器
↓批量下载图片,美女图库↓
图片自动播放器
↓图片自动播放器↓
一键清除垃圾
↓轻轻一点,清除系统垃圾↓
开发: C++知识库 Java知识库 JavaScript Python PHP知识库 人工智能 区块链 大数据 移动开发 嵌入式 开发工具 数据结构与算法 开发测试 游戏开发 网络协议 系统运维
教程: HTML教程 CSS教程 JavaScript教程 Go语言教程 JQuery教程 VUE教程 VUE3教程 Bootstrap教程 SQL数据库教程 C语言教程 C++教程 Java教程 Python教程 Python3教程 C#教程
数码: 电脑 笔记本 显卡 显示器 固态硬盘 硬盘 耳机 手机 iphone vivo oppo 小米 华为 单反 装机 图拉丁
 
   -> 数据结构与算法 -> STL之算法 -> 正文阅读

[数据结构与算法]STL之算法

一、for_each()遍历算法

?
# include <iostream>
# include <vector>
# include <algorithm>

using namespace std;

void printf_01 (int a)
{
	cout<<a<<" ";
}

class printf_02
{
	public:
		void operator ()(int a)
		{
			cout<<a<<" ";
		}
};

int main (void)
{
	vector<int> v;
	for(int i=0;i<10;++i)
	{
		v.push_back(i);
	}
	for_each(v.begin(),v.end(),printf_01);//第一种,用普通函数实现
	cout<<endl<<"---------------"<<endl;
	for_each(v.begin(),v.end(),printf_02());//第二种,用仿函数实现
	return 0;
} 

?

二、transform()遍历算法

# include <iostream>
# include <vector>
# include <algorithm>

using namespace std;

class printf_01
{
	public:
		void operator ()(int a)
		{
			cout<<a<<" "; 
		}
};

class transform_01
{
	public:
		int operator ()(int a)
		{
			return a+100;
		}
};

int main (void)
{
	vector<int> v;
	for(int i=0;i<10;++i)
	{
		v.push_back(i); 
	}
	for_each(v.begin(),v.end(),printf_01());
	cout<<endl<<"-----------------"<<endl;
	vector<int> v1;
	v1.resize(v.size());
	transform(v.begin(),v.end(),v1.begin(),transform_01());//在使用transform前一定要先 v1.resize(v.size())开辟空间 
	for_each(v1.begin(),v1.end(),printf_01());
	return 0;
}

?三、find查找算法

//常规查找
# include <iostream>
# include <vector>
# include <algorithm>

using namespace std;

int main (void)
{
	vector<int> v;
	for(int i=0;i<10;++i)
	{
		v.push_back(i);
	}
	
	vector<int>::iterator it=find(v.begin(),v.end(),5);
	if(it==v.end())
	{
		cout<<"Not find!"<<endl;
	}
	else
	{
		cout<<"Find it!"<<" "<<*it<<endl;
	}
	return 0;
} 


//自定义数据类型查找
# include <iostream>
# include <vector>
# include <string>
# include <algorithm>
using namespace std;

class student
{
	public:
		student(string m_name,int m_age)
		{
			name=m_name;
			age=m_age;
		}
		bool operator==(const student& stu)//自定义数据类型必须指明查找标准,重载=运算符
		{
			if(age==stu.age&&name==stu.name)
				return true;
			else
				return false;
		}
		int age;
		string name;
};

int main (void)
{
	student stu1("aaa",10);
	student stu2("bbb",20);
	student stu3("ccc",30);
	student stu4("ddd",40);
	student stu5("eee",50);

	vector<student> v;
	v.push_back(stu1);
	v.push_back(stu2);
	v.push_back(stu3);
	v.push_back(stu4);
	v.push_back(stu5);
	
	student stu("ccc",30);
	
	vector<student>::iterator it=find(v.begin(),v.end(),stu);
	if(it==v.end())
	{
		cout<<"Not find!"<<endl;
	}
	else
	{
		cout<<"Find it!"<<endl<<"Name:"<<it->name<<" "<<"Age:"<<it->name<<endl;
	}
	return 0;
}

四、find_if查找算法

//自定义数据查找
# include <iostream>
# include <vector>
# include <string>
# include <algorithm>

using namespace std;

class student
{
	public:
		student(string m_name,int m_age)
		{
			name=m_name;
			age=m_age;
		}
		int age;
		string name;	
};

class compare
{
	public:
		bool operator()(student stu)
		{
			return stu.age>20;
		}	
};

int main (void)
{
	student stu1("aaa",10);
	student stu2("bbb",20);
	student stu3("ccc",30);
	vector<student> v;
	v.push_back(stu1);
	v.push_back(stu2);
	v.push_back(stu3);
	vector<student>::iterator it=find_if(v.begin(),v.end(),compare());
	if(it!=v.end())
	{
		cout<<"Find it!"<<endl<<"Age:"<<it->age<<" "<<"Name:"<<it->name<<endl;
	}
	else
	{
		cout<<"Not find it!"<<endl;
	}
	return 0;
}


//普通查找
# include <iostream>
# include <vector>
# include <algorithm>
using namespace std;

class greatertwo
{
	public:
		bool operator ()(int a)
		{
			return a>20;
		}
};

int main (void)
{
	vector<int> v;
	v.push_back(10);
	v.push_back(20);
	v.push_back(30);
	vector<int>::iterator it=find_if(v.begin(),v.end(),greatertwo());
	if(it!=v.end())
	{
		cout<<"Find it!"<<" "<<*it<<endl;
	}
	else
	{
		cout<<"Not find it!"<<endl;
	}
	
	return 0;
}

五、adjacent_find查找相邻且相同元素的算法

# include <iostream>
# include <vector>
# include <algorithm>

using namespace std;

int main (void)
{
	vector<int> v;
	v.push_back(1);
	v.push_back(2);
	v.push_back(3);
	v.push_back(3);
	v.push_back(4);
	vector<int>::iterator it=adjacent_find(v.begin(),v.end());//adjacent_find()
	if(it==v.end())
	{
		cout<<"Not find it!"<<endl;
	}
	else
	{
		cout<<"Find it!"<<endl<<*it<<" "<<*(it++)<<endl;
	}
	return 0;
}

六、binary_search()查找某个元素是否存在算法(元素递增的等差数列)? ? ?????

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

int main (void)
{
	vector<int> v;
	v.push_back(1);
	v.push_back(2);
	v.push_back(3);
	v.push_back(4);
	v.push_back(5);
	/*
	v.push_back(5);//如果是这样就错了
	v.push_back(4);
	v.push_back(3);
	v.push_back(2);
	v.push_back(1);
	*/
	bool a=binary_search(v.begin(),v.end(),3);
	if(a)
	{
		cout<<"Find it!"<<endl;
	}
	else
	{
		cout<<"Not find it!"<<endl;
	}
	return 0;
}

七、count()统计相同个数算法

//统计普通类型
# include <iostream>
# include <vector>
# include <algorithm>
using namespace std;

int main (void)
{
	vector<int> v;
	v.push_back(1);
	v.push_back(1);
	v.push_back(2);
	v.push_back(3);
	v.push_back(4);
	int a=count(v.begin(),v.end(),1);
	cout<<"The number is:"<<a<<endl;
	return 0;
}

//统计自定义数据类型
# include <iostream>
# include <string>
# include <vector>
# include <algorithm>

using namespace std;

class student 
{
	public:
		student(string m_name,int m_age)
		{
			name=m_name;
			age=m_age;
		}
		bool operator ==(student stu)//需要重载=运算符
		{
			if(stu.age==age)
				return true;
			else
				return false;
		}
		string name;
		int age;	
};

int main (void)
{
	vector<student> v;
	student stu1("aaa",29);
	student stu2("bbb",29);
	student stu3("ccc",49);
	v.push_back(stu1);
	v.push_back(stu2);
	v.push_back(stu3);

	student stu4("ddd",29);
	
	int num=count(v.begin(),v.end(),stu4);
	cout<<num<<endl;
	return 0;
}

八、count_if按照条件查找


//普通查找
# include <iostream>
# include <vector>
# include <algorithm>
using namespace std;

class compare
{
	public:
		bool operator ()(int a)
		{
			return a>2;
		}
};

int main (void)
{
	vector<int> v;
	v.push_back(1);
	v.push_back(3);
	v.push_back(4);
	v.push_back(4);
	int num=count_if(v.begin(),v.end(),compare());
	cout<<num<<endl;
	return 0;
}

//自定义数据类型的查找
# include <iostream>
# include <string>
# include <vector>
# include <algorithm>

using namespace std;

class student
{
	public:
		student(string m_name,int m_age)
		{
			name=m_name;
			age=m_age;
		}
		string name;
		int age;	
};

class compare
{
	public:
		bool operator ()(const student stu)
		{
			return stu.age>25;
		}	
};

int main (void)
{
	vector<student> v;
	student stu1("aaa",30);
	student stu2("bbb",20);
	student stu3("ccc",30);
	v.push_back(stu1);
	v.push_back(stu2);
	v.push_back(stu3);
	int num=count_if(v.begin(),v.end(),compare());
	cout<<num<<endl;
	return 0;
}

九、sort排序算法

# include <iostream>
# include <vector>
# include <algorithm>
# include <functional>

using namespace std;

void myprintf(int a)
{
	cout<<a<<" ";
}

int main (void)
{
	vector<int> v;
	v.push_back(10);
	v.push_back(30);
	v.push_back(20);
	sort(v.begin(),v.end());
	for_each(v.begin(),v.end(),myprintf);
	cout<<endl<<"------------"<<endl;
	sort(v.begin(),v.end(),greater<int>());
	for_each(v.begin(),v.end(),myprintf);
	cout<<endl;
	return 0;
}

十、random_shuffle洗牌算法

# include <iostream>
# include <vector>
# include <algorithm>
# include <ctime>		//time()函数所在库(或者"time.h")
# include <stdlib.h>		//rand()函数和srand()函数所在库
using namespace std;

void myprintf(int a)
{
	cout<<a<<" ";
}

int main (void)
{
	srand(time(NULL));//加入随机数种子,使每次洗牌算法执行结果不同
	vector<int> v;
	for(int i=0;i<10;++i)
	{
		v.push_back(i);
	}
	for_each(v.begin(),v.end(),myprintf);
	cout<<endl<<"----------------------"<<endl;
	random_shuffle(v.begin(),v.end());
	for_each(v.begin(),v.end(),myprintf);
	cout<<endl;
	return 0;
}

? ? ? ? ?

  数据结构与算法 最新文章
【力扣106】 从中序与后续遍历序列构造二叉
leetcode 322 零钱兑换
哈希的应用:海量数据处理
动态规划|最短Hamilton路径
华为机试_HJ41 称砝码【中等】【menset】【
【C与数据结构】——寒假提高每日练习Day1
基础算法——堆排序
2023王道数据结构线性表--单链表课后习题部
LeetCode 之 反转链表的一部分
【题解】lintcode必刷50题<有效的括号序列
上一篇文章      下一篇文章      查看所有文章
加:2021-08-09 10:28:44  更:2021-08-09 10:31:24 
 
开发: C++知识库 Java知识库 JavaScript Python PHP知识库 人工智能 区块链 大数据 移动开发 嵌入式 开发工具 数据结构与算法 开发测试 游戏开发 网络协议 系统运维
教程: HTML教程 CSS教程 JavaScript教程 Go语言教程 JQuery教程 VUE教程 VUE3教程 Bootstrap教程 SQL数据库教程 C语言教程 C++教程 Java教程 Python教程 Python3教程 C#教程
数码: 电脑 笔记本 显卡 显示器 固态硬盘 硬盘 耳机 手机 iphone vivo oppo 小米 华为 单反 装机 图拉丁

360图书馆 购物 三丰科技 阅读网 日历 万年历 2024年5日历 -2024/5/19 23:50:29-

图片自动播放器
↓图片自动播放器↓
TxT小说阅读器
↓语音阅读,小说下载,古典文学↓
一键清除垃圾
↓轻轻一点,清除系统垃圾↓
图片批量下载器
↓批量下载图片,美女图库↓
  网站联系: qq:121756557 email:121756557@qq.com  IT数码