栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 软件开发 > 后端开发 > C/C++/C#

C++STL

C/C++/C# 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

C++STL

C++STL容器部分 定长数组

Lambda表达式

  • array

  • #include 
    #include 
    #include 
    using namespace std;
    //size_t unsigned int 
    template 
    class MyArray 
    {
    public:
    	MyArray() 
    	{
    		memroy = new _Ty[size];
    		//new MM[size]
    		
    	}
    	_Ty& operator[](int index)
    	{
    		return memroy[index];
    	}
    	~MyArray() 
    	{
    		delete[] memroy;
    	}
    public:
    	_Ty* begin() 
    	{
    		return memroy + 0;
    	}
    	_Ty* end() 
    	{
    		return memroy + size;
    	}
    	//类的对象模仿指针的行为
    	class iterator 
    	{
    	public:
    		iterator(_Ty* pmove = nullptr) :pmove(pmove) {}
    		void operator=(_Ty* pmove) 
    		{
    			this->pmove = pmove;
    		}
    		bool operator!=(_Ty* pmove) 
    		{
    			return this->pmove != pmove;
    		}
    		iterator operator++(int) 
    		{
    			this->pmove++;
    			return *this;
    		}
    		_Ty operator*() 
    		{
    			return pmove[0];
    		}
    	protected:
    		_Ty* pmove;
    	};
    
    protected:
    	_Ty* memroy;	//MM
    };
    void testMyArray() 
    {
    	MyArray array1D;
    	for (int i = 0; i < 3; i++) 
    	{
    		array1D[i] = i;
    	}
    	MyArray::iterator iter;
    	for (iter = array1D.begin(); iter != array1D.end(); iter++) 
    	{
    		cout << *iter << "t";
    	}
    	cout << endl;
    }
    void testArray() 
    {
    	//存储数据的类型是:int  
    //数组长度:3
    //用模板的时候用的都是对象,而不是new一个对象
    	array array1D;
    	array* p = new array;
    	delete p;
    #define MAX 5
    	array dAarray1D;
    	//创建并初始化
    	array  num = { 1,2,3 };
    	for (int i = 0; i < array1D.size(); i++) 
    	{
    		array1D[i] = i;
    	}
    	//迭代器
    }
    void testExOperator() 
    {
    	//使用: 和数组一样的用法
    	//一些函数
    	array test = { 1,2,3 };
    	cout << test.empty() << endl;
    	cout << test.size() << endl;
    	test.fill(5);		//填充所有的元素 ,填充为5
    	for (int v : test)		
    	{
    		cout << v << "t";
    	}
    	cout << endl;
    	//交换  长度一定是要一样常
    	array test1 = { 0,0,0};
    	test.swap(test1);
    	int cData[3] = { 1,2,3 };   //映射:一种对应关系,数组下标对应元素
    	for (auto v : cData) 
    	{
    		cout << v << "t";
    	}
    	cout << endl;
    }
    //定长数组处理自定义类型的数据
    class MM
    {
    public:
    	MM() {}
    	MM(string name, int age) :name(name), age(age) {}
    	void print() 
    	{
    		cout << name << "t" << age << endl;
    	}
    protected:
    	string name;
    	int age;
    };
    void testUserData() 
    {
    	array mmData;	//	MM array[3];
    	for (int i = 0; i < mmData.size(); i++) 
    	{
    		string name = "name";
    		mmData[i] = MM(name + to_string(i), 16 + i);
    	}
    
    	for (auto v : mmData) 
    	{
    		//v:就是MM的对象
    		v.print();
    		//cout<::iterator iter;
    	//begin()
    	//end(): 最后一个位置,不是最后元素的位置
    	(*mmData.begin()).print();
    	//(*mmData.end()).print();  越界访问
    	(*(mmData.end() - 1)).print();
    	for (iter = mmData.begin(); iter != mmData.end(); iter++) 
    	{
    		//(*iter).print();
    		iter->print();
    		//cout<<*iter;		//重载<<
    	}
    }
    //array当做函数参数,返回值都可以
    array&  returnArray(array& temp) 
    {
    	for (int i = 0; i < temp.size(); i++)
    	{
    		temp[i] = i;
    	}
    	return temp;
    }
    
    int main() 
    {
    	testArray();
    	testExOperator();
    	testUserData();
    	cout << "自己写的模板" << endl;
    	testMyArray();
    
    	return 0;
    }
    动态数组
  • vector

    #include 
    #include 
    #include 
    using namespace std;
    //辅助函数:遍历容器
    template 
    void printVector(vector<_Ty>& temp) 
    {
    	for (auto v : temp) 
    	{
    		cout << v << "t ";
    	}
    	cout << endl;
    }
    //基本用法
    void testCreateVector() 
    {
    	//模板类型:存储数据类型
    	//1.不带长度的创建方式
    	vector vecData;
    	//只能用成员函数做插入
    	for (int i = 0; i < 3; i++) 
    	{
    		vecData.push_back(i);	//尾插法  0   0 1  0 1 2
    	}
    	printVector(vecData);
    	//2.带长度
    	vector strData(3);		//当前动态数组的长度是3
    	//确定长度,可以直接使用数组法插入
    	for (int i = 0; i < 3; i++) 
    	{
    		string name = "name";
    		//只有在确定长度范围以内的可以直接采用数组法插入
    		strData[i] = name + to_string(i);
    	}
    	printVector(strData);
    	//超过的必须用成员函数插入
    	//strData[3] = "name3";    vector subscript out of range  错误
    	strData.push_back("name3");	//在这个函数中做了自动扩增
    	printVector(strData);
    		
    
    	//3.带初始化
    	vector dData = { 1.1,1.23,1.44 };  //自动算出长度为3
    	printVector(dData);
    
    	//猜谜
    	vector intData(3);   
    	intData.push_back(1111);  //在原来内存的后面扩增
    	printVector(intData);	  //0 0 0 1111
    
    	//迭代器遍历
    	vector::iterator iter;
    	for (iter = strData.begin(); iter != strData.end(); iter++) 
    	{
    		cout << *iter << "t";
    	}
    	cout << endl;
    
    }
    //自定义类型数据
    class MM 
    {
    public:
    	MM(string name, int age) :name(name), age(age) {}
    	friend ostream& operator<<(ostream& out, const MM& temp) 
    	{
    		out << temp.name << "t" << temp.age;
    		return out;
    	}
    protected:
    	string name;
    	int age;
    };
    void testUserData() 
    {
    	vector mmData;
    	for (int i = 0; i < 3; i++) 
    	{
    		string name = "name";
    		mmData.push_back(MM(name + to_string(i), 18 + i));
    	}
    	//二进制“<<”: 没有找到接受“MM”类型的右操作数的运算符(或没有可接受的转换)
    	printVector(mmData);
    }
    
    void testExOperator() 
    {
    	vector iData = { 1,2,3,4 };
    	cout << iData.size() << endl;		//当前容器中的元素个数
    	cout << iData.empty() << endl;		//判断是否为空   return size==0; 有元素返回false
    	cout << iData.front() << endl;		//访问第一个元素
    	cout << iData.back() << endl;		//访问最后一个元素
    	cout << iData.at(2) << endl;		//下标访问
    	cout << iData[2] << endl;			//和at(2)一样的效果 表示
    	//修改
    	iData.emplace(iData.begin() + 2, 100); //修改下标是2位置的元素为100
    	printVector(iData);
    	iData.emplace_back(999);			//和push_back一样的功能
    	iData.emplace(iData.begin(), 1111);	//修改第一个元素
    										//删除函数
    	//iData.erase(iData.begin() + 2);   //数组只有伪删除,没有删除操作
    	printVector(iData);
    
    	//批量复制
    	int array[] = { 1,2,3 };
    	vector vecData;
    	vecData.assign(array, array + 3);   //不需要起始长度
    	printVector(vecData);
    }
    int main() 
    {
    	testCreateVector();
    	testUserData();
    	testExOperator();
    	return 0;
    }
    array与vector嵌套
  • #include 
    #include 
    #include 
    #include 
    #include 
    #include 
    using namespace std;
    void testArrayVsArray() 
    {
    	array, 4> arrData;  //int arrData[4][3]
    	for (int i = 0; i < 4; i++) 
    	{
    		for (int j = 0; j < 3; j++) 
    		{
    			arrData[i][j] = i * j;
    			cout << arrData[i][j] << "t";
    		}
    		cout << endl;
    	}
    }
    void testVectorVsVector() 
    {
    	srand((unsigned int)time(nullptr));
    	vector> vecData;
    	//一般vecotor 采用的是push_back插入
    	for (int i = 0; i < 4; i++) 
    	{
    		vector temp;
    		//rand()%3 [0,2]  [2,4]
    		for (int j = 0; j < rand()%3+2; j++) 
    		{
    			temp.push_back(i * j);
    		}
    		vecData.push_back(temp);
    	}
    	//不等列数的二位数组
    	for (int i = 0; i < vecData.size(); i++) 
    	{
    		for (int j = 0; j < vecData[i].size(); j++) 
    		{
    			cout << vecData[i][j] << "t";
    		}
    		cout << endl;
    	}
    }
    void testArrayVsVector()
    {
    	array, 3> vecArr;
    	vector vec1[3] = { { 1,2,3 } , {1,2,3,4}, {1,2}};
    	for (int i = 0; i < vecArr.size(); i++) 
    	{
    		vecArr[i] = vec1[i];
    	}
    	//不等列数的二位数组
    	for (int i = 0; i < vecArr.size(); i++)
    	{
    		for (int j = 0; j < vecArr[i].size(); j++)
    		{
    			cout << vecArr[i][j] << "t";
    		}
    		cout << endl;
    	}
    	vector, 3>, 3>> vec;
    	//慢慢剥洋葱即可
    	array, 3>, 3> test;
    	for (int i = 0; i < 3; i++) 
    	{
    		test[i] = vecArr;		//vecArr: array, 3>
    	}
    	vec.push_back(test);
    	
    	vector, 3>, 3>> test;
    	//上面一行 等效下面两行
    	using Data = array, 3>, 3>;
    	vector test2;
    
    	array, 3>, 3> test3;
    	//上面一行 等效下面两行
    	using Data2 = array, 3>;
    	array  test3;
    }
    
    void testVectorVsArray() 
    {
    	vector> arrVec;
    	array  arr[3] = { { 1,2,3 } , {1,2,3}, {1,2,3}};
    	for (int i = 0; i < 3; i++) 
    	{
    		arrVec.push_back(arr[i]);
    	}
    	for (int i = 0; i < arrVec.size(); i++)
    	{
    		for (int j = 0; j < arrVec[i].size(); j++)
    		{
    			cout << arrVec[i][j] << "t";
    		}
    		cout << endl;
    	}
    }
    
    int main() 
    {
    	testArrayVsArray();
    	testVectorVsVector();
    	testArrayVsVector();
    	testVectorVsArray();
    	return 0;
    }
    array与vector嵌套
    #include 
    #include 
    #include 
    #include 
    #include 
    #include 
    using namespace std;
    void testArrayVsArray() 
    {
    	array, 4> arrData;  //int arrData[4][3]
    	for (int i = 0; i < 4; i++) 
    	{
    		for (int j = 0; j < 3; j++) 
    		{
    			arrData[i][j] = i * j;
    			cout << arrData[i][j] << "t";
    		}
    		cout << endl;
    	}
    }
    void testVectorVsVector() 
    {
    	srand((unsigned int)time(nullptr));
    	vector> vecData;
    	//一般vecotor 采用的是push_back插入
    	for (int i = 0; i < 4; i++) 
    	{
    		vector temp;
    		//rand()%3 [0,2]  [2,4]
    		for (int j = 0; j < rand()%3+2; j++) 
    		{
    			temp.push_back(i * j);
    		}
    		vecData.push_back(temp);
    	}
    	//不等列数的二位数组
    	for (int i = 0; i < vecData.size(); i++) 
    	{
    		for (int j = 0; j < vecData[i].size(); j++) 
    		{
    			cout << vecData[i][j] << "t";
    		}
    		cout << endl;
    	}
    }
    void testArrayVsVector()
    {
    	array, 3> vecArr;
    	vector vec1[3] = { { 1,2,3 } , {1,2,3,4}, {1,2}};
    	for (int i = 0; i < vecArr.size(); i++) 
    	{
    		vecArr[i] = vec1[i];
    	}
    	//不等列数的二位数组
    	for (int i = 0; i < vecArr.size(); i++)
    	{
    		for (int j = 0; j < vecArr[i].size(); j++)
    		{
    			cout << vecArr[i][j] << "t";
    		}
    		cout << endl;
    	}
    	vector, 3>, 3>> vec;
    	//慢慢剥洋葱即可
    	array, 3>, 3> test;
    	for (int i = 0; i < 3; i++) 
    	{
    		test[i] = vecArr;		//vecArr: array, 3>
    	}
    	vec.push_back(test);
    	
    	vector, 3>, 3>> test;
    	//上面一行 等效下面两行
    	using Data = array, 3>, 3>;
    	vector test2;
    
    	array, 3>, 3> test3;
    	//上面一行 等效下面两行
    	using Data2 = array, 3>;
    	array  test3;
    }
    
    void testVectorVsArray() 
    {
    	vector> arrVec;
    	array  arr[3] = { { 1,2,3 } , {1,2,3}, {1,2,3}};
    	for (int i = 0; i < 3; i++) 
    	{
    		arrVec.push_back(arr[i]);
    	}
    	for (int i = 0; i < arrVec.size(); i++)
    	{
    		for (int j = 0; j < arrVec[i].size(); j++)
    		{
    			cout << arrVec[i][j] << "t";
    		}
    		cout << endl;
    	}
    }
    
    int main() 
    {
    	testArrayVsArray();
    	testVectorVsVector();
    	testArrayVsVector();
    	testVectorVsArray();
    	return 0;
    }
    双向链表
  • list

    #include 
    #include 
    #include 
    #include   //less和greator头文件
    using namespace std;
    list::iterator myFind(list& iNum,int data) 
    {
    	for (list::iterator iter = iNum.begin(); iter != iNum.end(); iter++) 
    	{
    		if (*iter == data) 
    		{
    			return iter;
    		}
    	}
    	return iNum.end();
    }
    //基本操作: 操作基本数据类型
    void testList() 
    {
    	list iNum;
    	list strNum;
    	//插入
    	strNum.push_back("string1");	//尾插发
    	strNum.push_back("string2");
    	strNum.push_front("string3");
    	//string3 string1  string2
    	//遍历
    	//不删除方式遍历
    	list::iterator iter;
    	for (iter = strNum.begin(); iter != strNum.end(); iter++) 
    	{
    		cout << *iter << " ";
    	}
    	cout << endl;
    	cout << "是否为空:" <());				//排序
    	for (auto v : iNum)
    	{
    		cout << v << "t";
    	}
    	cout << endl;
    }
    void testDelete() 
    {
    	int array[4] = { 1,2,2,3 };
    	list data;
    	data.assign(array, array + 4);
    	//相同元素的删除
    	for (list::iterator iter = data.begin(); iter != data.end(); )
    	{
    		if (*iter == 2)
    		{
    			iter = data.erase(iter);
    		}
    		else
    		{
    			iter++;
    		}
    	}
    	for (auto v : data)
    	{
    		cout << v << "t";
    	}
    	cout << endl;
    }
    //操作自定义类型数据
    class MM 
    {
    public:
    	MM(string name, int age, int num) :name(name), age(age), num(num) {}
    	void print() 
    	{
    		cout << name << "t" << age << "t" << num << endl;
    	}
    	bool operator==(const string& name) const
    	{
    		return this->name == name;
    	}
    	bool operator<(const MM& object) const 
    	{
    		return this->name < object.name;
    	}
    	string getName() const
    	{
    		return name;
    	}
    	int getAge() const
    	{
    		return age;
    	}
    protected:
    	string name;
    	int age;
    	int num;
    };
    bool compareByName(const MM& object1, const MM& object2) 
    {
    	return object1.getName() < object2.getName();
    }
    bool compareByAge(const MM& object1, const MM& object2)
    {
    	return object1.getAge() < object2.getAge();
    }
    void testUserData() 
    {
    	list mmData;
    	string name;
    	int age;
    	int num;
    	while (1) 
    	{
    		cout << "input MM:" << endl;
    		cin >> name >> age >> num;
    		mmData.push_back(MM(name, age, num));
    		cout << "是否继续输入?" << endl;
    		while (cin.get() != 'n');
    		if (cin.get() == 'n')
    			break;
    	}
    	cout << "姓名t年龄t编号" << endl;
    	for (MM v : mmData) 
    	{
    		v.print();
    	}
    	//二进制“==”: 没有找到接受“MM”类型的左操作数的运算符(或没有可接受的转换)
    	auto result = find(mmData.begin(), mmData.end(), string("name1"));
    	//重载方式
    	mmData.sort(less());		//重载<  
    	//mmData.sort(greater());	//重载>
    	//不采用重载方式,需要自己写比较准则
    	mmData.sort(compareByName);
    	mmData.sort(compareByAge);
    }
    int main() 
    {
    	//testList();
    	//testDelete();
    	testUserData();
    	return 0;
    }
  • stack

    #include 
    #include 
    #include 
    using namespace std;
    void testStack() 
    {
    	//穿脱原则
    	//FILO  
    	//1 2 3 
    	//3 2 1 
    	//push(data)
    	//pop()  删除
    	//top()  获取栈顶元素
    	//size() empty();
    	stack intStack;
    	for (int i = 0; i < 3; i++) 
    	{
    		intStack.push(i);
    	}
    	while (!intStack.empty()) 
    	{
    		cout << intStack.top() << "t";
    		intStack.pop();
    	}
    	cout << endl;
    }
    
    void NumTobinary(int data) 
    {
    	stack bin;
    	while (data) 
    	{
    		bin.push(data % 2);
    		data = data / 2;
    	}
    	if (bin.size() < 8) 
    	{
    		for (int i = bin.size(); i < 8; i++) 
    		{
    			bin.push(0);
    		}
    	}
    	while (!bin.empty()) 
    	{
    		cout << bin.top();
    		bin.pop();
    	}
    	cout << endl;
    	//中缀和后缀表达式资料看看
    	//a+b 中缀
    	//ab+
    	//a+b*c+d;
    }
    int main() 
    {
    	//testStack();
    	NumTobinary(512);
    	return 0;
    }
    队列
  • queue

    #include 
    #include 
    #include 
    using namespace std;
    void pop_queue(queue qData) 
    {
    	while (!qData.empty())
    	{
    		cout << qData.front() << " ";  //获取队头元素
    		qData.pop();	//出队
    	}
    	cout << endl;
    }
    void testQueue() 
    {
    	queue qData;
    	for (int i = 0; i < 3; i++) 
    	{
    		qData.push(i);	//入队
    	}
    	while (!qData.empty()) 
    	{
    		cout << qData.front() << " ";  //获取队头元素
    		qData.pop();	//出队
    	}
    	cout << endl;
    	cout << qData.size() << endl;
    }
    int main() 
    {
    	testQueue();
    
    
    	return 0;
    }

    deque

    #include 
    #include 
    #include 
    using namespace std;
    //从头出来
    void pop_front_dequeue(deque dData) 
    {
    	while (!dData.empty()) 
    	{
    		cout << dData.front()<<" ";
    		dData.pop_front();
    	}
    	cout << endl;
    }
    //从尾出来
    void pop_back_dequeue(deque dData)
    {
    	while (!dData.empty())
    	{
    		cout << dData.back() << " ";
    		dData.pop_back();
    	}
    	cout << endl;
    }
    void testDeque() 
    {
    	deque deData;
    	for (int i = 0; i < 3; i++) 
    	{
    		deData.push_back(i);		//尾插法入队
    		deData.push_front(i);		//头插法入队
    	}
    	deData.push_back(999);
    	//0 0
    	//1 0 0 1
    	//2 1 0 0 1 2
    	pop_front_dequeue(deData);
    	pop_back_dequeue(deData);
    }
    int main() 
    {
    	testDeque();
    	return 0;
    }

    priority_queue

    #include 
    #include 
    #include 
    using namespace std;
    template ,class _Pr = less<_Ty>>
    class my_priority_queue
    {
    public:
    
    protected:
    
    };
    void testCreatePriorityQueue() 
    {
    	//默认的方式,一级下面三种 都一样,大的先出队
    	priority_queue pqData;
    	priority_queue> pqData2;			//默认排序准则
    	priority_queue,less> pqData3;	//所有参数都完整
    	//优先队列,是按照数据的优先权出队,VIP服务
    	pqData.push(12);
    	pqData.push(0);
    	pqData.push(34);
    	while (!pqData.empty()) 
    	{
    		cout << pqData.top()<<" ";
    		pqData.pop();		//出队
    	}
    	cout << endl;
    	//贪心算法
    	priority_queue, greater> pqDataG;	
    	pqDataG.push(12);
    	pqDataG.push(0);
    	pqDataG.push(34);
    	while (!pqDataG.empty())
    	{
    		cout << pqDataG.top() << " ";
    		pqDataG.pop();		//出队
    	}
    	cout << endl;
    }
    int main() 
    {
    	testCreatePriorityQueue();
    	return 0;
    }
    集合
  • set/multiset

    #include 
    #include 
    #include 
    using namespace std;
    
    class MM 
    {
    public:
    	MM(string name, int age) :name(name), age(age) {}
    	bool operator<(const MM& object)const 
    	{
    		return this->name < object.name;
    	}
    	void print() 
    	{
    		cout << name << " " << age << endl;
    	}
    protected:
    	string name;
    	int age;
    };
    
    void testSet() 
    {
    	srand((unsigned int)time(nullptr));
    	set setData;					//默认方式 从小到大
    	set> setData2;		//和默认方式一样
    	set> setData3;	//从大到小
    	int array[10] = { 0 };
    	for (int i = 0; i < 10; i++)
    	{
    		int temp = rand() % 10;
    		array[i] = temp;
    		setData.insert(temp);
    	}
    	for (auto v : array) 
    	{
    		cout << v << " ";
    	}
    	cout << endl;
    	for (set::iterator iter = setData.begin(); iter != setData.end(); iter++) 
    	{
    		cout << *iter << " ";
    	}
    	cout << endl;
    }
    void testUserData() 
    {
    	set mmData;   //less <
    	mmData.insert(MM("name3", 19));
    	mmData.insert(MM("name2", 28));
    	mmData.insert(MM("name3", 188));
    	for (auto v : mmData) 
    	{
    		v.print();
    	}
    
    	set cData;
    	set strData;
    }
    //多重集合: 只具有排序功能,不具有去重功能
    void testmultiset()
    {
    	multiset mulData;
    	for (int i = 0; i < 10; i++) 
    	{
    		mulData.insert(rand() % 10);
    		//rand()%26+'A';
    		//rand()%26+'a';
    	}
    	for (auto v : mulData) 
    	{
    		cout << v << " ";
    	}
    	cout << endl;
    }
    int main() 
    {
    	testSet();
    	testUserData();
    	testmultiset();
    
    
    	return 0;
    }

    bitset

    #include 
    #include 
    using namespace std;
    template 
    class MyArray 
    {
    
    };
    int main() 
    {
    	//多个二进制位
    	bitset<8> bData("11110000");
    	cout << bData << endl;
    	bData.flip();
    	cout << bData << endl;
    	cout << bData.all() << endl;
    	cout << bData.any() << endl;
    	cout << bData.size() << endl;
    	cout < num(7);
    	cout << num << endl;
    	return 0;
    }
    映射
  • map/multiset

    #include 
    #include 
    #include 
    using namespace std;
    template 
    struct MyPair 
    {
    	_Ty1 first;
    	_Ty2 second;
    	MyPair(_Ty1 first, _Ty2 second) :first(first), second(second) {}
    };
    //map存储的数据是一个数对类型
    void testPair() 
    {
    	pair pData(1, "string");
    	MyPair myPData(1, "string");
    	cout << pData.first << " " << pData.second << endl;
    }
    //map
    //1.自带排序,默认是从小到大
    //2.数据唯一性
    void testMap() 
    {
    	map mapData;
    	map> mapData1; //和上面创建方式一样,从小到大
    	map> mapData2; //从大到小
    	//1.insert插入
    	mapData.insert(pair(1, "string"));
    	//2.make_pair构建数对插入
    	mapData.insert(make_pair(3, "string3"));
    	//3.单映射,可以直接采用数组下标方式进行插入
    	//数组在一定程序上来说或可以说数组
    	//map[first]=second;
    	//相比数组来说,这个下标是没有任何要求
    	mapData[-1] = string("string-1");  //等效插入一个数对类型
    	//上面代码等效:mapData.insert(pair(-1,"string-1"))
    	mapData[1] = "string1";		//相同键 采用的是覆盖方式
    	//遍历:
    	for (auto iter = mapData.begin(); iter != mapData.end(); iter++) 
    	{
    		/
    #include 
    using namespace std;
    int Max(int a, int b) 
    {
    	return a > b ? a : b;
    }
    void print(int(*pMax)(int, int), int a, int b) 
    {
    	cout << pMax(a, b) << endl;
    }
    class MM 
    {
    public:
    	void print() 
    	{
    		[this] {cout << name << "t" << age << endl; }();		//定义和调用一步到位
    	}
    protected:
    	string name="默认";
    	int age=100;
    };
    int main() 
    {
    	int(*pMax)(int, int) = nullptr;
    	//完整版:Lambad 表达式
    	//final override
    	pMax = [](int a, int b)mutable noexcept->int {return a > b ? a : b; };
    	cout << pMax(1, 3) << endl;
    	//省略版本的,写代码越简单越好
    	auto  pp = [](int a, int b) {return a > b ? a : b; };
    	cout << pp(1, 3) << endl;
    	//实际使用可以一步到位--->短暂性局部使用的的函数
    	cout << [](int a, int b)mutable noexcept->int {return a > b ? a : b; }(1, 3) << endl;
    	print([](int a, int b) {return a > b ? a : b; }, 1, 3);
    	print([](int a, int b) {return a > b ? b : a; }, 1, 3);
    	print([](int a, int b) {return a+b; }, 1, 3);
    	//捕获方式的区别
    	//用值的捕获: 在Lambad中不能把值当做左值使用,函数调用不会因为值的改变而改变
    	int data = 101010;
    	auto pFunc = [=] {  cout << data << endl; };  //无参() 可以省略
    	auto pFunc2 = [&] { cout << data << endl; };
    	pFunc();
    	pFunc2();
    	data = 808080;
    	pFunc();
    	pFunc2();
    	MM  mm;
    	mm.print();
    	//特殊的东西-->结合auto使用
    	auto pAuto = [](auto a, auto b) ->auto{return a > b ? a : b; };
    	cout << pAuto(1, 3) << endl;				//[](auto a, auto b) ->auto{return a > b ? a : b; }(1,3)
    	cout << pAuto("stringa", "stringb") << endl;
    	//[] (auto a, auto b) ->auto{return a > b ? a : b; }("stringa", "stringb");
    	return 0;
    }
    仿函数
  • 什么仿函数? 类模仿函数调用行为,实质是无名对象调用重载的()函数

    • 所以仿函数的关键点在于重载()

  • 一般情况仿函数是做排序准则,或者一些算法的计算准则

  • 标准库中的仿函数

    • 算术类

    • 关系类

    • 逻辑类

    • 选择,证同,投射

      #include 
      #include 
      #include 		//仿函数所在头文件
      #include 
      using namespace std;
      class Sum 
      {
      public:
      	int  operator()(int a, int b) const 
      	{
      		return a + b;
      	}
      };
      int main()
      {
      	//重载的()的调用方式
      	Sum s;
      	cout << "显式调用:" << s.operator()(1, 3) << endl;		//显式调用重载函数
      	cout << "隐式调用:" << s(1, 3) << endl;
      	//用{}和()帮助编译器去做解析
      	cout << "无名调用:" << Sum{}(1, 3) << endl;			//类模仿函数调用行为--->仿函数
      	cout << "无名调用:" << Sum()(1, 3) << endl;			//类模仿函数调用行为--->仿函数
      	//算术
      	cout << plus{}(1, 3) << endl;
      	//关系
      	cout << equal_to{}(1, 3) << endl;
      	map> map1;
      	map> map3;
      	//逻辑类
      	cout << logical_and{}(1,0) << endl;
      	//求大于3 小于10的数字
      	//没必要的做的事情
      	int a = 34;
      	if (logical_and{}(a > 3, a < 10)) 
      	{
      		cout << "大于3 小于10的数字" << endl;
      	}
      
      	return 0;
      }
      函数适配器
    • 什么是函数适配器: 用来绑定函数调用时候的参数,让函数适应其他调用的用法

      #include 
      #include 
      #include 
      #include 
      #include 
      using namespace std;
      //老版本bind1st bind2nd
      //新版本: bind函数
      int Max(int a, int b) 
      {
      	return a > b ? a : b;
      }
      void print(int(*pMax)(int,int), int a,int b) 
      {
      	cout << pMax(a,b) << endl;
      }
      class Test 
      {
      public:
      	void print(int a, int b, int c) 
      	{
      		cout << a << " " << b << " " << c << endl;
      	}
      protected:
      };
      void testClassFunc()
      {
      	Test test;
      	auto testFunc = bind(&Test::print, &test, std::placeholders::_1, std::placeholders::_2, 99);
      	testFunc(1, 3);		//调用,直接调用
      }
      
      void printData(int one, Test two, string str) 
      {
      	cout << "调用成功" << endl;
      }
      //可以通过占位符,所以调整参数位置,形成不同的调用形态
      void testExUser() 
      {
      	//占位符代表原函数的参数 在调用形态 的第二个位置
      	auto testFunc = bind(printData,std::placeholders::_3, std::placeholders::_1, std::placeholders::_2);
      	printData(1, Test(), "ILoveyou");
      	testFunc(Test(), "ILoveyou", 1);
      }
      
      int main() 
      {
      	cout << Max(1, 3) << endl;
      	//基本用法
      	//std::placeholders::_1占位符
      	auto pMax = bind(Max, std::placeholders::_1, 100);  //把第二个参数置为100
      	//只是增加调用行为,并没有真正改变了这个函数指针类型
      	cout << pMax(34) << endl;
      	cout << pMax(13, 44) << endl;  //绑定后的函数指针,不再支持传参的,第二个参数无效
      	//语法上没问题,但是尽量别这样做
      	using namespace std::placeholders;
      	auto pMax2 = bind(Max, _1, 100);  //把第二个参数置为100
      	cout << pMax2(34) << endl;
      	vector vecData = { 19,43,89,89,34,54,67,54 };
      	cout << count_if(vecData.begin(), vecData.end(),bind(greater(),std::placeholders::_1,60)) << endl;
      	cout << count_if(vecData.begin(), vecData.end(), [](int a) {return a > 60; }) << endl;
      	testClassFunc();
      	testExUser();
      
      	return 0;
      }
      函数包装器
    • 函数包装器是什么?就是把函数指针包装成一个对象。通过这个对象调用函数

      • 一旦函数指针被函数包装器包装了,那这个包装器对象可以直接替换函数指针的用法去调用函数

    • 函数包装器类的实力例化传参: function<函数返回值类型(参数类型)>

      #include 
      #include 
      #include 
      using namespace std;
      int Max(int a, int b) 
      {
      	cout << "包装普通函数:" << endl;
      	return a > b ? a : b;
      }
      class MM 
      {
      public:
      	void print(int a)    //void print(int a, MM* mm);
      	{
      		cout << "包装成员函数" << endl;
      	}
      	static void printStatic() 
      	{
      		cout << "包装静态的函数" << endl;
      	}
      protected:
      };
      void testMMFunc() 
      {
      	MM mm;
      	function func(bind(&MM::print,&mm,std::placeholders::_1));
      	func(12);
      }
      
      
      class Test 
      {
      public:
      	void operator()(string str) 
      	{
      		cout << str << endl;
      	}
      };
      
      
      
      
      void printData(int a, MM mm, string str) 
      {
      	cout << "bind和function" << endl;
      }
      void TestFuncBind() 
      {
      	function pf = bind(printData,
      		std::placeholders::_2, std::placeholders::_3, std::placeholders::_1 );
      	pf("string", 1, MM());
      	//3  1  2
      }
      
      int main() 
      {
      	function funcMax(Max);
      	cout << funcMax(1, 3) << endl;
      
      	function funcMax2=Max;
      	function funcS(MM::printStatic);
      	funcS();
      
      	//仿函数包装
      	Test test;
      	function func = test;
      	func("包装仿函数");
      	TestFuncBind();
      	//包装成员函数
      	testMMFunc();
      	return 0;
      }
      C++STL算法篇 STL查找算法
    • 基本查找

      • find:区间查找

      • find_if:条件查找

      • find_firt_of: 查找区间第一次出现值

      • adjacent_find: 查找第一次重复的数

      • search:子序列查找

      • search_n: 子序列查找出现次数

    • 统计查找

      • count: 区间统计

      • count_if: 条件统计个数

      • equal:比较

    • 有序查找

      • binary_search:二分查找

      • upper_bound: 查找最后一个大于查找的值

      • lower_bound: 大于等于查找的值

      • equal_range:区间比较---有序序列

    • STL排序通用算法
    • merge: 归并排序,存于新容器

    • inplace_merge: 归并排序,覆盖原区间

    • sort: 排序,更改原容器顺序

    • stable_sort: 排序,保存原容器数据顺序

    • nth_element: 关键字排序

    • partition:范围排序

    • partial_sort:范围排序

    • partial_sort_copy:范围排序外加复制操作

    • stable_partition: 范围排序,保存原容器顺序

    • random_shuffle: 随机排序

    • reverse:逆序原容器

    • reverse_copy: 逆序容器保存到新容器

    • STL删除替换算法
    • copy: 拷贝函数

    • copy_backward: 逆序拷贝

    • iter_swap: 交换

    • remove: 删除

    • remove_copy: 删除元素复制到新容器

    • remove_if:条件删除

    • remove_copy_if:条件删除拷贝到新容器

    • replace:替换

    • replace_copy: 替换,结果放到新容器

    • replace_if: 条件替换

    • replace_copy_if:条件替换,结果另存

    • swap: 交换

    • swap_range:区间交换

    • unique:去重

    • unique_copy:去重,结果另存

    • STL排列组合算法
    • next_permutation:下一个排序序列的组合

    • prev_permutation:上一个排序序列的组合

    • STL 算术算法
    • accumulate:区间求和

    • partial_sum:相邻元素的和

    • inner_product:序列内积运算

    • adjacent_difference:相邻元素的差

    • STL 生成异变算法
    • for_each:迭代访问

    • fill:填充方式初始容器

    • fill_n:指定长度填充容器

    • generate_n:填充前n个位置

    • transform:一元转换和二元转换

    • STL 关系算法
    • equal:两容器元素是否都相同

    • includes:是否是包含关系

    • lexicographical_compare:比较两个序列

    • max:求最大值

    • max_element:返回最大值的iterator

    • min:求最小值

    • min_element:求最小值的iterator

    • mismatch:找到第一个不同的位置

    • STL 集合算法
    • set_union:差集

    • set_intersection:并集

    • set_difference:保存第一个中有第二个没有的元素

    • set_symmetric_difference:对称差集

    • STL堆算法
    • make_heap:生成一个堆

    • pop_heap:出堆

    • push_heap:入堆

    • sort_heap:堆排序

    • rotate:移动元素到容器末尾

    • rotate_copy:移动元素到新容器

转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/873066.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号