C++基本使用(编程语言学习笔记二)

前言:

作为本人接触的第二个面向对象编程的语言,C++相较于 java还是有很多不方便之处和坑,也可能是因为ide的原因,idea比vs界面友好太多。不过无论是保研考研复试都要求使用C/C++完成机试(至少北理是,然而比较坑的是它本科根本没有C++的课程但默认你会且精通,但用C也过于抽象),以及C++算是笔试的首选语言,我感觉这个笔记可能会是我复习最多的。C++最大的坑感觉就是默认值传递,虽然也有指针和引用,但写函数的时候总会忘记,下面代码主要包括基本类型转换、输入处理(笔试重点关注)、常用的数据结构、基本类对象的使用以及其他一些小坑。

源代码链接:

wlf728050719/CPlusPlus: C++学习笔记icon-default.png?t=O83Ahttps://github.com/wlf728050719/CPlusPlus

                                       

一、头文件部分

#include<iostream>
#include<sstream>
#include<string>
#include"People.h" //.h文件用引号导入
#include<vector>
#include<queue>
#include<stack>
#include<set>
#include<map>
#include<algorithm>
#define inputIndex -1
using namespace std;

二、定义函数

//默认实参
int add(int a, int b = 0)
{
	return a + b;
}
//不允许再定义int add(int a)否则add(1)时程序无法确定调用哪个函数

//C++模板 类似C# java泛型
template<class T,class U>
void judgeType(T t, U u)
{
	cout << typeid(t).name() << endl;
	cout << typeid(u).name() << endl;
}
bool sortByAge(People p1, People p2)
{
	return p1.age > p2.age;//true表示目前状态是true,不用交换
}

三、主函数

该部分所有内容均在int main(){}中

3.1缺省函数以及泛型方法调用

	cout << add(1) << endl;
	judgeType(1, '1');

对应输出

3.2类型转化以及字符串使用

//类型转换
{
	string str = "12345";
	cout << str.substr(0, 2) << endl;//从0开始,长度为2

	int a = 1;
	float b = 2.0;
	string str_a = to_string(a);//数字转字符串
	string str_b = to_string(b);

	string str_c = "3";
	string str_d = "4.0";
	int c = stoi(str_c);//字符串转数字
	float d = stof(str_d);

	char e = 'e';
	string str_e(1, e);//字符转字符串

	string str_f= "f";
	char f = str_f[0];//字符串转字符

	char array[] = { 'a','b','c' ,'\0'};//字符数组转string 需要加结束符号
	string str_array = array;

	for (int i = 0; i < str_array.length(); i++)//字符串长度不包括结束符
	{
		cout << str_array[i] << " ";
	}
	cout << endl;

	cout << a << " " << b << " " << c << " " << d <<" "<<e<<" "<<f<< endl;
	cout << str_a << " " << str_b << " " << str_c << " " << str_d <<" "<<str_e<<" "<<str_f<<" "<<str_array<<endl;

}

对应输出

3.3各种输入处理

通过修改头文件部分的inputIndex选择使用输入

3.3.1输入两个数据,一个数字,一个字符串换行或空格分割
#if inputIndex==1
//一次换行对应一次cin
{//输入两个数据,一个数字,一个字符串
	cout << "输入两个数据,一个数字,一个字符串换行或空格分割" << endl;
	int a;
	string b;
	cin >> a >> b;
	cout << a << " " << b << endl;
}
#endif 

对应输出

3.3.2输入一行空格分割的数据(数字类型)
	#if inputIndex==2
	{//处理一行空格分割的输入
		cout << "输入一行空格分割的数据(数字类型)" << endl;
		string input;
		getline(cin, input);
		stringstream ss(input);
		int num;
		int sum = 0;
		while (ss >> num)
		{
			sum += num;
		}
		cout << sum <<endl;
	}
	#endif 

对应输出

3.3.3输入一行逗号分割的数据(数字类型)
#if inputIndex==3
{
	//处理一行逗号分割的输入
	cout << "输入一行逗号分割的数据(数字类型)" << endl;
	string input;
	getline(cin, input);
	stringstream ss(input);
	string num;
	int sum = 0;
	while (getline(ss, num, ','))
	{
		sum += stoi(num);
	}
	cout << sum << endl;
}
#endif 

对应输出

3.3.4输入若干行数据(数字类型)以end结尾
	#if inputIndex==4
	{
		//处理多行输入以end结尾数据
		cout << "输入若干行数据(数字类型)以end结尾" << endl;
		string input;
		int sum = 0;
		while (cin >> input)
		{
			if (input == "end")
				break;
			sum += stoi(input);
		}
		cout << sum << endl;
	}

对应输出

3.3.5输入若干行数据数字加字符串
#if inputIndex==5
{
	cout << "输入若干行数据数字加字符串" << endl;
	string input;
	while (getline(cin,input))
	{
		if (input.length() > 0)
		{
			int a;
			string b;
			stringstream ss(input);
			ss >> a >> b;
			cout << a << " " << b << endl;
		}
		else
		{
			break;
		}
	}
	cout << "结束" << endl;
}
#endif

对应输出

4、类对象使用

.h文件

#pragma once
class Animal//C++使用抽象类实现java,C#接口效果,没有abstract关键字,有virtual关键字,用=0代替不写方法体表示函数纯虚函数
{
public:
	virtual void walk() = 0;//纯虚函数
	bool isHunman() {//可以被派生类重写
		return false;
	}
};

#pragma once
#include<iostream>
#include"Animal.h"
using namespace std;
//定义枚举类型
enum Fruits
{
	Apple = 0,
	Banana = 1,
	Watermelon = 2
};
class People:Animal
{
	private:
		int money;
		Fruits favotiteFruit=Apple;//枚举作为成员,使用不用Fruits.Apple区分java
	public:
		string name;
		string country;
		int age;
		People(string name,int age);//定义时不写函数体需要在cpp文件中填写
		People(string name, int age, int money,string country)//构造函数重载
		{
			this->name = name;
			this->age = age;
			this->country = country;
			this->money = money;
		}
		void showInfo();
		void falseBorrowMoneyTo(People other,int amount);
		void TrueBorrowMoneyTo1(People &other,int amount);
		void TrueBorrowMoneyTo2(People* other, int amount);
		void operator+(int money);//类运算符重载
		void walk()override;//必须重新基类虚方法,可不加关键字override
		bool isHunman() { return true; }//可以重新基类一般方法
		~People();


};


#pragma once
#include<iostream>
#include<string>
#include"People.h"
using namespace std;
class Employee:People
{
	public :
		string workName;
		Employee(string name,int age,string workName) :People(name,age)
		{
			this->workName = workName;
			cout << "员工姓名:" << this->name << "年龄:" << this->age << "工作:" << this->workName << endl;
			//cout << this->money; 不能访问基类的私有成员
		}

};

.cpp文件

#include "Animal.h"
#include "People.h"

People::People(string name,int age):country("China"),money(1000)
{
	this->name = name;	//C++ this表示当前对象的指针,区分其他语言,访问C++类成员:类实例.成员名  或 类实例执政->成员名
	(*this).age = age;
}

void People::showInfo()
{
	cout <<"年龄: "<< this->age <<"姓名: "<< this->name <<"存款"<<+this->money<< endl;
}

void People::falseBorrowMoneyTo(People other,int amount)
{
	cout << this->name << "错误借了" << other.name << amount<<endl;
	this->money -= amount;
	other.money += amount;//other为copy的对象,在函数结束时调用析构函数自动销毁
}

void People::TrueBorrowMoneyTo1(People &other,int amount)
{
	cout << this->name << "正确借了" << other.name << amount<<endl;
	this->money -= amount;
	other.money += amount;
}

void People::TrueBorrowMoneyTo2(People* other, int amount)
{
	cout << this->name << "正确借了" << other->name << amount << endl;
	this->money -= amount;
	other->money += amount;
}

void People::operator+(int money)
{
	cout << this->name << "获得" << money << "元" << endl;
	this->money += money;
}

void People::walk()
{
	cout << "用两条腿走" << endl;
}

People::~People()
{
	cout << "Goodbye" << this->name << endl;
}
#include "Employee.h"

主函数中:

//类对象使用
People xyw("xyw", 21);	//获取对象 存储栈上
People lfc("lfc", 24);
People* mmc = new People("mmc", 23);//通过new获取对象指针 存储堆上
cout << "初始状态" << endl;
xyw.showInfo();
lfc.showInfo();
mmc->showInfo();
xyw.falseBorrowMoneyTo(lfc, 100);//传递值导致错误
xyw.showInfo();
lfc.showInfo();
xyw.TrueBorrowMoneyTo1(lfc, 100);//引用传递正确
xyw.showInfo();
lfc.showInfo();
xyw.TrueBorrowMoneyTo2(mmc, 100);//指针传递正确
xyw.showInfo();
mmc->showInfo();
xyw + 100;
xyw.showInfo();
xyw.walk();
if (xyw.isHunman())
{
	cout << xyw.name<<"是人" << endl;
}
else
{
	cout << xyw.name << "诗人握持" << endl;
}

对应输出

由于函数形参为值,导致this存款变化而other不受影响,同时由于函数结束,函数体中的临时变量other销毁。

使用引用类型,两者钱正确变化(在上面xyw900,lfc1000的基础上)

使用指针,两者钱正确变化(在上面xyw800,mmc1000的基础上)

类运算符重载调用

调用重写方法

5、常用数据结构

5.1vector
	//Vector
	{
		//数组向量
		vector<int> numbers = { 1,2,3,4,5,6,7};
		numbers.pop_back();//没有返回值,区分java stack.pop
		numbers.push_back(8);
		//迭代器访问删除满足条件元素固定写法
		for (vector<int>::iterator i = numbers.begin(); i != numbers.end();)
		{
			cout <<"迭代器访问"<< *i <<endl;
			if (*i > 5)//删除大于5的元素
			{
				i = numbers.erase(i);//更新迭代器 erase返回删除元素后面一个的迭代器
			}
			else
				i++;
		}
		cout << "末尾数字" << numbers.back() <<" "<< numbers[numbers.size() - 1] << endl; //通过下标直接随机访问
		cout << "开头数字" << numbers.front() << " "<<numbers[0] << endl;
		for (int i : numbers)
			cout << i << " ";
		cout << endl;
		//同理迭代器插入元素
		for (vector<int>::iterator i = numbers.begin(); i != numbers.end();i++)
		{
			cout << "迭代器访问" << *i << endl;
			if (*i > 0)//在大于0的元素前插99
			{
				i=numbers.insert(i,99)+1;   //insert返回指向插入新元素的迭代器
			}

		}
		for (int i : numbers)
			cout << i << " ";
		cout << endl;
		int findnumber = 10;
		vector<int>::iterator it = find(numbers.begin(),numbers.end(), findnumber);//find返回指向找到元素的迭代器
		if (it != numbers.end())
			cout << "找到元素" << findnumber<<endl;
		else
			cout << "找不到元素" << findnumber << endl;
		
		//对象向量
		vector<People> peopleList = {xyw,lfc,*mmc };//值传递,而非引用区分java C#的List,用指针数组实现List效果
		for (int i = 0; i < peopleList.size(); i++)
		{
			peopleList[i].name = "xxx";//影响list中元素
		}
		xyw.showInfo();//不会影响原对象
		lfc.showInfo();
		(*mmc).showInfo();

		for (People p : peopleList)//类似java,C# foreach,但同样是值传递
		{
			p.name = "yyy";//不加引用相当于p=peopleList[i],修改p不会影响list中元素
		}
		for (People p : peopleList)
		{
			p.showInfo();
		}
		cout << "按年龄排序" << endl;
		sort(peopleList.begin(), peopleList.end(), sortByAge);//开始结束迭代器以及函数名
		for (People p : peopleList)
		{
			p.showInfo();
		}
		delete(mmc);//手动销毁对象
	}

对应输出

1234567弹出7插入8变成1234568

删除所有大于5的数字变成12345,头1尾5

所有大于0的数字前插入99

find调用是否存在元素10

此处调用了三次析构函数,如果再创建一个vector<People> peoplelist1={xyw,lfc,mmc},就会看到6个goodbye,关于“C++ vector 中,开辟空间时,为何会调用析构函数”这个问题我查询了一下似乎解答不多,让我能够接受的回答是,似乎是编译器的自动行为。

下面的输出表明由于传入不是对象,导致对vector内部元素的操作不会影响原来对象。但此时vector中姓名均已经改成了xxx

同样的通过foreach遍历方式操作内部元素也不会影响原来对象,同时由于每次都需要创建一个新的局部变量People p=数组中对象,每次循环都销毁,所以有三次析构调用,但由于并非使用People&,导致实际上并没有修改数组中元素,数组中姓名仍然为xxx,在遍历数组时同样道理又调用了三次析构函数。

前面14次析构函数的调用是sort引起的,具体为啥是14次就当作课后习题了(手动狗头),中间三次析构是遍历引起,和上面相同道理,goodbyemmc是手动delete释放空间引起,后面三次是因为大括号结束,创建的vector释放其内部成员。

由于lfc以及xyw是定义在主函数中,没有用{}代码块,所以他们在最后主函数结束才销毁。

5.2List

List 常用函数和vector一致,list底层双向链表,随机访问慢o(n),插删快o(1),vector底层动态数组,随机访问快o(1),插删慢o(n),(主要感觉vector还是用的多些,就偷懒没看了)

//List 常用函数和vector一致,list底层双向链表,随机访问慢o(n),插删快o(1),vector底层动态数组,随机访问快o(1),插删慢o(n)
5.3Queue

广度优先遍历

//Queue 不能使用迭代器访问
{
	queue<int> q;
	q.push(1);
	q.push(2);
	q.push(3);
	q.pop();
	cout << "开头" << q.front() << " 结尾:" << q.back() << endl;
	if (!q.empty())
		cout << "队列非空" << endl;
}

对应输出:

5.4Stack

括号匹配

//Stack 不能使用迭代器访问
{
	stack<int> s;
	s.push(1);
	s.push(2);
	s.push(3);
	s.pop();
	cout << "栈顶" << s.top() << endl;
	if (!s.empty())
		cout << "栈非空" << endl;
}

对应输出:

5.5Set
//Set
{
	set<int> mySet = { 1,2,3,4,5,5 };//即使初始化时有重复元素实际也会保证元素唯一
	mySet.insert(6);//增
	mySet.insert(6);//增相同不会报错
	mySet.erase(3);//删
	mySet.erase(-1);//删不存在不会报错
	for (set<int>::iterator it = mySet.begin(); it != mySet.end(); ++it) { //虽然能通过迭代器遍历,但不能下标随机访问
		cout << *it << " ";
	}
	cout << endl;
	int findnumber = 5;
	set<int>::iterator it = mySet.find(findnumber);//与vector区分
	if (it != mySet.end())
		cout << "存在元素" << findnumber << endl;
	else
		cout << "不存在元素" << findnumber << endl;
	cout << mySet.count(findnumber) << endl;//只会输出0或1判断元素是否在其中最简单方法
}

对应输出

12345加6删3

5.6Map
//Map
{
	map<string, int> m;
	m["1"] = 1;	//增
	m["2"] = 2;
	m["3"] = 3;
	m["temp"] = -1;
	m["temp"] = 0;//改
	m.erase("2");//删
	if (m.count("1")==1)
		cout << "存在键为1的元素" << endl;
	for (auto& pair : m)
	{
		cout << "key:" << pair.first << endl;
		pair.second++;
	}
	for (map<string, int>::iterator it = m.begin(); it != m.end(); it++)//显示遍历
	{
		cout << "key:" << it->first << " value:" << it->second << endl;
	}

	cout << "按值降序排序" << endl;
	// 将 map 的内容复制到 vector 中
	vector<pair<string, int>> vec(m.begin(), m.end());

	// 按值排序
	sort(vec.begin(), vec.end(), [](const auto& a, const auto& b) {
		return a.second > b.second; // 按值排序
		});

	// 输出排序后的结果
	for (pair<string, int > pair : vec) {
		std::cout << pair.first << ": " << pair.second << std::endl;
	}

}

对应输出

四、Main部分代码

完整代码=此部分代码+4、类对象使用中.h和.cpp文件=github链接

#include<iostream>
#include<sstream>
#include<string>
#include"People.h" //.h文件用引号导入
#include<vector>
#include<queue>
#include<stack>
#include<set>
#include<map>
#include<algorithm>
#define inputIndex -1
using namespace std;
//默认实参
int add(int a, int b = 0)
{
	return a + b;
}
//不允许再定义int add(int a)否则add(1)时程序无法确定调用哪个函数

//C++模板 类似C# java泛型
template<class T,class U>
void judgeType(T t, U u)
{
	cout << typeid(t).name() << endl;
	cout << typeid(u).name() << endl;
}
bool sortByAge(People p1, People p2)
{
	return p1.age > p2.age;//true表示目前状态是true,不用交换
}

int main()
{	
	cout << add(1) << endl;
	judgeType(1, '1');

	//类型转换
	{
		string str = "12345";
		cout << str.substr(0, 2) << endl;//从0开始,长度为2

		int a = 1;
		float b = 2.0;
		string str_a = to_string(a);//数字转字符串
		string str_b = to_string(b);

		string str_c = "3";
		string str_d = "4.0";
		int c = stoi(str_c);//字符串转数字
		float d = stof(str_d);

		char e = 'e';
		string str_e(1, e);//字符转字符串

		string str_f= "f";
		char f = str_f[0];//字符串转字符

		char array[] = { 'a','b','c' ,'\0'};//字符数组转string 需要加结束符号
		string str_array = array;

		for (int i = 0; i < str_array.length(); i++)//字符串长度不包括结束符
		{
			cout << str_array[i] << " ";
		}
		cout << endl;

		cout << a << " " << b << " " << c << " " << d <<" "<<e<<" "<<f<< endl;
		cout << str_a << " " << str_b << " " << str_c << " " << str_d <<" "<<str_e<<" "<<str_f<<" "<<str_array<<endl;

	}
	#if inputIndex==1
	//一次换行对应一次cin
	{//输入两个数据,一个数字,一个字符串
		cout << "输入两个数据,一个数字,一个字符串换行或空格分割" << endl;
		int a;
		string b;
		cin >> a >> b;
		cout << a << " " << b << endl;
	}
	#endif 

	#if inputIndex==2
	{//处理一行空格分割的输入
		cout << "输入一行空格分割的数据(数字类型)" << endl;
		string input;
		getline(cin, input);
		stringstream ss(input);
		int num;
		int sum = 0;
		while (ss >> num)
		{
			sum += num;
		}
		cout << sum <<endl;
	}
	#endif 

	#if inputIndex==3
	{
		//处理一行逗号分割的输入
		cout << "输入一行逗号分割的数据(数字类型)" << endl;
		string input;
		getline(cin, input);
		stringstream ss(input);
		string num;
		int sum = 0;
		while (getline(ss, num, ','))
		{
			sum += stoi(num);
		}
		cout << sum << endl;
	}
	#endif 

	#if inputIndex==4
	{
		//处理多行输入以end结尾数据
		cout << "输入若干行数据(数字类型)以end结尾" << endl;
		string input;
		int sum = 0;
		while (cin >> input)
		{
			if (input == "end")
				break;
			sum += stoi(input);
		}
		cout << sum << endl;
	}
	#endif 
	#if inputIndex==5
	{
		cout << "输入若干行数据数字加字符串" << endl;
		string input;
		while (getline(cin,input))
		{
			if (input.length() > 0)
			{
				int a;
				string b;
				stringstream ss(input);
				ss >> a >> b;
				cout << a << " " << b << endl;
			}
			else
			{
				break;
			}
		}
		cout << "结束" << endl;
	}
	#endif
	//类对象使用
	People xyw("xyw", 21);	//获取对象 存储栈上
	People lfc("lfc", 24);
	People* mmc = new People("mmc", 23);//通过new获取对象指针 存储堆上
	cout << "初始状态" << endl;
	xyw.showInfo();
	lfc.showInfo();
	mmc->showInfo();
	xyw.falseBorrowMoneyTo(lfc, 100);//传递值导致错误
	xyw.showInfo();
	lfc.showInfo();
	xyw.TrueBorrowMoneyTo1(lfc, 100);//引用传递正确
	xyw.showInfo();
	lfc.showInfo();
	xyw.TrueBorrowMoneyTo2(mmc, 100);//指针传递正确
	xyw.showInfo();
	mmc->showInfo();
	xyw + 100;
	xyw.showInfo();
	xyw.walk();
	if (xyw.isHunman())
	{
		cout << xyw.name<<"是人" << endl;
	}
	else
	{
		cout << xyw.name << "诗人握持" << endl;
	}

	//Vector
	{
		//数组向量
		vector<int> numbers = { 1,2,3,4,5,6,7};
		numbers.pop_back();//没有返回值,区分java stack.pop
		numbers.push_back(8);
		//迭代器访问删除满足条件元素固定写法
		for (vector<int>::iterator i = numbers.begin(); i != numbers.end();)
		{
			cout <<"迭代器访问"<< *i <<endl;
			if (*i > 5)//删除大于5的元素
			{
				i = numbers.erase(i);//更新迭代器 erase返回删除元素后面一个的迭代器
			}
			else
				i++;
		}
		cout << "末尾数字" << numbers.back() <<" "<< numbers[numbers.size() - 1] << endl; //通过下标直接随机访问
		cout << "开头数字" << numbers.front() << " "<<numbers[0] << endl;
		for (int i : numbers)
			cout << i << " ";
		cout << endl;
		//同理迭代器插入元素
		for (vector<int>::iterator i = numbers.begin(); i != numbers.end();i++)
		{
			cout << "迭代器访问" << *i << endl;
			if (*i > 0)//在大于0的元素前插99
			{
				i=numbers.insert(i,99)+1;   //insert返回指向插入新元素的迭代器
			}

		}
		for (int i : numbers)
			cout << i << " ";
		cout << endl;
		int findnumber = 10;
		vector<int>::iterator it = find(numbers.begin(),numbers.end(), findnumber);//find返回指向找到元素的迭代器
		if (it != numbers.end())
			cout << "找到元素" << findnumber<<endl;
		else
			cout << "找不到元素" << findnumber << endl;
		
		//对象向量
		vector<People> peopleList = {xyw,lfc,*mmc };//值传递,而非引用区分java C#的List,用指针数组实现List效果
		for (int i = 0; i < peopleList.size(); i++)
		{
			peopleList[i].name = "xxx";//影响list中元素
		}
		xyw.showInfo();//不会影响原对象
		lfc.showInfo();
		(*mmc).showInfo();

		for (People p : peopleList)//类似java,C# foreach,但同样是值传递
		{
			p.name = "yyy";//不加引用相当于p=peopleList[i],修改p不会影响list中元素
		}
		for (People p : peopleList)
		{
			p.showInfo();
		}
		cout << "按年龄排序" << endl;
		sort(peopleList.begin(), peopleList.end(), sortByAge);//开始结束迭代器以及函数名
		for (People p : peopleList)
		{
			p.showInfo();
		}
		delete(mmc);//手动销毁对象
	}
	//List 常用函数和vector一致,list底层双向链表,随机访问慢o(n),插删快o(1),vector底层动态数组,随机访问快o(1),插删慢o(n)
	//Queue 不能使用迭代器访问
	{
		queue<int> q;
		q.push(1);
		q.push(2);
		q.push(3);
		q.pop();
		cout << "开头" << q.front() << " 结尾:" << q.back() << endl;
		if (!q.empty())
			cout << "队列非空" << endl;
	}
	//Stack 不能使用迭代器访问
	{
		stack<int> s;
		s.push(1);
		s.push(2);
		s.push(3);
		s.pop();
		cout << "栈顶" << s.top() << endl;
		if (!s.empty())
			cout << "栈非空" << endl;
	}
	//Set
	{
		set<int> mySet = { 1,2,3,4,5,5 };//即使初始化时有重复元素实际也会保证元素唯一
		mySet.insert(6);//增
		mySet.insert(6);//增相同不会报错
		mySet.erase(3);//删
		mySet.erase(-1);//删不存在不会报错
		for (set<int>::iterator it = mySet.begin(); it != mySet.end(); ++it) { //虽然能通过迭代器遍历,但不能下标随机访问
			cout << *it << " ";
		}
		cout << endl;
		int findnumber = 5;
		set<int>::iterator it = mySet.find(findnumber);//与vector区分
		if (it != mySet.end())
			cout << "存在元素" << findnumber << endl;
		else
			cout << "不存在元素" << findnumber << endl;
		cout << mySet.count(findnumber) << endl;//只会输出0或1判断元素是否在其中最简单方法
	}
	//Map
	{
		map<string, int> m;
		m["1"] = 1;	//增
		m["2"] = 2;
		m["3"] = 3;
		m["temp"] = -1;
		m["temp"] = 0;//改
		m.erase("2");//删
		if (m.count("1")==1)
			cout << "存在键为1的元素" << endl;
		for (auto& pair : m)
		{
			cout << "key:" << pair.first << endl;
			pair.second++;
		}
		for (map<string, int>::iterator it = m.begin(); it != m.end(); it++)//显示遍历
		{
			cout << "key:" << it->first << " value:" << it->second << endl;
		}

		cout << "按值降序排序" << endl;
		// 将 map 的内容复制到 vector 中
		vector<pair<string, int>> vec(m.begin(), m.end());

		// 按值排序
		sort(vec.begin(), vec.end(), [](const auto& a, const auto& b) {
			return a.second > b.second; // 按值排序
			});

		// 输出排序后的结果
		for (pair<string, int > pair : vec) {
			std::cout << pair.first << ": " << pair.second << std::endl;
		}

	}
}
     

完整输出:

1
int
char
12
a b c
1 2 3 4 e f
1 2.000000 3 4.0 e f abc
初始状态
年龄: 21姓名: xyw存款1000
年龄: 24姓名: lfc存款1000
年龄: 23姓名: mmc存款1000
xyw错误借了lfc100
Goodbyelfc
年龄: 21姓名: xyw存款900
年龄: 24姓名: lfc存款1000
xyw正确借了lfc100
年龄: 21姓名: xyw存款800
年龄: 24姓名: lfc存款1100
xyw正确借了mmc100
年龄: 21姓名: xyw存款700
年龄: 23姓名: mmc存款1100
xyw获得100元
年龄: 21姓名: xyw存款800
用两条腿走
xyw是人
迭代器访问1
迭代器访问2
迭代器访问3
迭代器访问4
迭代器访问5
迭代器访问6
迭代器访问8
末尾数字5 5
开头数字1 1
1 2 3 4 5
迭代器访问1
迭代器访问2
迭代器访问3
迭代器访问4
迭代器访问5
99 1 99 2 99 3 99 4 99 5
找不到元素10
Goodbyemmc
Goodbyelfc
Goodbyexyw
年龄: 21姓名: xyw存款800
年龄: 24姓名: lfc存款1100
年龄: 23姓名: mmc存款1100
Goodbyeyyy
Goodbyeyyy
Goodbyeyyy
年龄: 21姓名: xxx存款800
Goodbyexxx
年龄: 24姓名: xxx存款1100
Goodbyexxx
年龄: 23姓名: xxx存款1100
Goodbyexxx
按年龄排序
Goodbyexxx
Goodbyexxx
Goodbyexxx
Goodbyexxx
Goodbyexxx
Goodbyexxx
Goodbyexxx
Goodbyexxx
Goodbyexxx
Goodbyexxx
Goodbyexxx
Goodbyexxx
Goodbyexxx
Goodbyexxx
年龄: 24姓名: xxx存款1100
Goodbyexxx
年龄: 23姓名: xxx存款1100
Goodbyexxx
年龄: 21姓名: xxx存款800
Goodbyexxx
Goodbyemmc
Goodbyexxx
Goodbyexxx
Goodbyexxx
开头2 结尾:3
队列非空
栈顶2
栈非空
1 2 4 5 6
存在元素5
1
存在键为1的元素
key:1
key:3
key:temp
key:1 value:2
key:3 value:4
key:temp value:1
按值降序排序
3: 4
1: 2
temp: 1
Goodbyelfc
Goodbyexyw

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值