C++ STL学习(10)--基础算法

本文档展示了如何在C++中使用模板和泛型算法处理整型向量,涉及查找、搜索、排序和基本操作,如查找特定数值、复制、变换和移除元素。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

#include <algorithm>         //使用泛型算法必须包含的头文件
#include <numeric>           //泛化的算数算法
#include<functional>         //
#include <iostream>
#include<vector>
#include<string>

using namespace std;


template <class T>
struct display
{
	void operator()(const T& x) const
	{
		cout << x << " ";
	}
};

void showVector(vector<int> iv , string str )
{
	cout << str ;
	for (auto x : iv)
		cout << x << " ";
	cout << endl;
}

int transformfun(int  i )
{
	return	i += 100;
}

int main()
{
	int ia[9] = { 0,1,2,3,4,5,6,7,8 };
	vector<int> iv1(ia, ia + 5);
	vector<int> iv2(ia, ia + 9);
	showVector(iv1, "This is iv1: ");
	showVector(iv2, "This is iv2: ");
	vector<int>::iterator it;

/***********  只读算法  ***********/
//查找算法
	//find(beg,end,v)在迭代区间[beg,end,v)内查找等于v的元素,
	//找到返回对应的迭代器,否则返回end()迭代器
	it = find(iv1.begin(), iv1.end(), 6);          
	if (it == iv1.end())
		cout << " I can't find the number \n";
	else 
		cout << " I find the number "<< *it << endl;

	it = find(iv2.begin(), iv2.end(), 6);
	if (it == iv2.end())
		cout << " I can't find the number \n";
	else
		cout << " I find the number " << *it << endl;

	//find_first_of(beg1,end1,beg2,end2) 在迭代区[beg1,end1)内查找[beg2,end2)内任意元素匹配的元素
	//然后返回一个迭代器,指向第一个匹配的元素。
	//如果找不到元素,则返回第一个范围的end迭代器
	it = find_first_of(iv1.begin(), iv1.end(), iv2.begin(), iv2.end()); 
	if (it == iv1.end())                                                
		cout << " I can't find the number \n";                          
	else
		cout << " I find the number " << *it << endl;

	//find_end(beg1,end1,beg2,end2) 与上find_first_of类似,
	//区别为查找最后一个匹配元素
	it = find_end(iv1.begin(), iv1.end(), iv2.begin(), iv2.end());      
	if (it == iv1.end())                                                
		cout << " I can't find the number \n";                         
	else
		cout << " I find the number " << *it << endl;

//搜索与统计算法
	//search(beg1,end1,beg2,end2) 在迭代区[beg1,end1)内查找子序列[beg2,end2)
	//找到:返回[beg1,end1)匹配的首位迭代器,没找到:返回[beg1,end1)的end
	it = search(iv2.begin(), iv2.end(), iv1.begin(), iv1.end());  
	if (it == iv2.end())                                          
		cout << " I can't find the number \n";
	else
		cout << " I find the string " << *it << endl;

	//count(beg,end,v) 统计等于v的元素的个数
	int n = count(iv1.begin(), iv1.end(), 3);    
	cout << "the number 3 has " << n << " in the vector" << endl;



/***********  可变序列算法  ***********/
//复制
	//copy(beg, end, beg2) 将迭代区[beg, end)元素复制到以beg2开始的区间,
	//直接覆盖原始内容,若空间不够需要resize否则会报错
	vector<int> iv3 = {99,88,77,66,55,44,33,22 };
	showVector(iv3, "before copy iv3 : \t");
	copy(iv1.begin(), iv1.end(), iv3.begin());    
	showVector(iv3, "after copy iv3 : \t");

//变换
	//transform(beg,end,beg2,func)将迭代区[beg, end)元素经过func处理后复制到以beg2开始的区间,同上
	transform(iv1.begin(), iv1.end(), iv3.begin(), transformfun);//transformfun函数让值加100
	showVector(iv3, "after transform iv3 : \t");

//替换
	//replace(beg,end,v1,v2)将迭代区[beg, end)中元素v1都替代为v2
	iv3.push_back(100);
	replace(iv3.begin(), iv3.end(), 100, 111);
	showVector(iv3, "after replace iv3 : \t");

//填充
	//fill(beg,end,v)将迭代区[beg, end)中元素都替代为v
	fill(iv3.begin(), iv3.end(), 22);
	showVector(iv3, "after fill iv3 : \t");
	//fill_n(beg,n,v)从beg开始插入n个v
	fill_n(iv3.begin(), 2, 11);
	showVector(iv3, "after fill_n iv3 : \t");

//随机生成
	//generate(beg,n,rand)   向beg开始的n个位置随机写数据


//移除
	//remove(beg,end)  移除区间[beg, end)内的元素
	//并不真正删除元素,只是将要删除的元素移动到容器的末尾
	//unique(beg,end)  剔除相邻重复的元素
	//并不真正删除元素,只是将要剔除的元素移动到容器的末尾


/***********  排序算法  ***********/
	//sort(beg,end) 区间[beg, end)按字典次序排列
	iv3.push_back(1);
	iv3.push_back(2);
	sort(iv3.begin(),iv3.end());
	showVector(iv3, "after sort iv3 : \t");

	//stable_sort(beg,end)  同上,不过保存相等元素之间的顺序关系
	iv3.push_back(1);
	iv3.push_back(2);
	stable_sort(iv3.begin(), iv3.end());
	showVector(iv3, "after stable_sort iv3 : \t");

	//partial_sort(beg,mid,end) 将最小值顺序放在[beg,end)内  
	//对区间[beg,end]内的mid - beg个元素进行排序,
	//将最小的mid - beg个元素有序放在序列的前mid - beg的位置上。
	iv3.push_back(8);
	iv3.push_back(9);
	partial_sort(iv3.begin() + 2, iv3.begin() + 6, iv3.end());
	showVector(iv3, "after partial_sort iv3 : \t");

	//random_shuffle(beg,end)  区间内元素随机排序
	random_shuffle(iv3.begin(), iv3.end());
	showVector(iv3, "after random_shuffle iv3 : \t");

	//reverse(beg,end)将区间内元素反转
	reverse(iv3.begin(), iv3.end());
	showVector(iv3, "after reverse iv3 : \t");

	//rotate(beg,mid,end)  将区间[beg,mid)和[mid,end)旋转,使mid为新起点
	rotate(iv3.begin(), iv3.begin()+5, iv3.end());
	showVector(iv3, "after rotate iv3 : \t");

	//merge(beg1,end1,beg2,end2,nbeg)  将有序区间[beg1,end1)和[beg2,end2)合并到一个新的序列nbeg中,
	//并对其进行排序
	merge(iv1.begin(), iv1.begin(), iv2.begin(), iv2.end(),iv3.begin());
	showVector(iv3, "after rotate iv3 : \t");

/***********  关系算法  ***********/
	//equal(beg1,end1,beg2,end2)  判断两个区间元素是否相等
	cout << equal(iv1.begin(), iv1.end(), iv2.begin(), iv2.end())<<endl;

	//includes(beg1,end1,beg2,end2)  判断第二个序列[beg2,end2)是否被第一个序列[beg1,end1)包含
	cout<<includes(iv2.begin(), iv2.end(), iv1.begin(), iv1.end()) << endl;

	//max_element()  返回序列最大元素的位置 (迭代器)
	//min_element()  返回序列最小元素的位置 (迭代器)
	cout <<* max_element(iv2.begin(), iv2.end()) << endl;
	cout <<* min_element(iv2.begin(), iv2.end()) << endl;


/***********  堆算法  ***********/
	//make_heap(beg,end)   以区间[beg,end)内元素建立堆
	random_shuffle(iv2.begin(), iv2.end());
	showVector(iv2, "after random_shuffle iv2 : \t");
	make_heap(iv2.begin(), iv2.end());
	showVector(iv2, "after make_heap iv2 : \t");

	//pop_heap(beg,end)    重新排序堆,使第一个与最后一个交换,并不是弹出最大值
	pop_heap(iv2.begin(), iv2.end());
	showVector(iv2, "after pop_heap iv2 : \t");

	//push_heap(beg,end)   重新排序堆,把新元素放在最后位置
	push_heap(iv2.begin(), iv2.end());
	showVector(iv2, "after push_heap iv2 : \t");
	iv2.push_back(9);
	showVector(iv2, "after iv2.push_back(9) iv2 : \t");
	push_heap(iv2.begin(), iv2.end());
	showVector(iv2, "after push_heap2 iv2 : \t");

	//sort_heap(beg,end)   对序列重新排序
	sort_heap(iv2.begin(), iv2.end());
	showVector(iv2, "after sort_heap iv2 : \t");


	pair<vector<int>::iterator, vector<int>::iterator> p = mismatch(iv1.begin(), iv1.end(), iv2.begin());
	//mismatch(iv1.begin(), iv1.end(), iv2.begin())    返回第一个找到的两个空间中不相同的元素。

	if (p.first == iv1.end())
		cout << "iv1.end" << endl;
	else
		cout << *(p.first) << endl;          //*(p.first)直接打印会报错,iv1中没有与iv2中不同的元素
	if (p.second == iv2.end())
		cout << "iv2.end" << endl;
	else
		cout << *(p.second) << endl;

	cout << equal(iv1.begin(), iv1.end(), iv2.begin()) << endl;
	


}

在这里插入图片描述

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值