⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 maptest.cpp

📁 c++中关于stl的详细介绍 有多本书籍在里面
💻 CPP
字号:
#include <algorithm>
#include <map>
#include <iostream>

#include <stdio.h>

using namespace std;

typedef struct {
	int index;
	char data[256];
} MYNODE, *PMYNODE;


//map构造比较函数
struct compare{
	bool operator()(int node1index, int node2index) const
	{
		return (node1index < node2index);
	}
};

int main()
{
	
	map<int, MYNODE, compare> testdata;

	int i = 0;
	while (true)
	{
		MYNODE tmpnode;
		
		if (scanf("%d, %s", &(tmpnode.index), &(tmpnode.data)) != 2)
			break;

		testdata[tmpnode.index] = tmpnode;
		
		i++;
	}

	cout<<"\n"<<endl;

	//遍历map
	map<int, MYNODE, compare>::iterator it;
	for (it=testdata.begin(); it!=testdata.end(); it++)
	{
		cout<<(*it).second.index<<"--"<<(*it).second.data<<endl;
	}

	cout<<"\n"<<endl;

	//改变map中NODE值
	MYNODE tmpnode;
	tmpnode.index = 9999;
	strcpy(tmpnode.data, "HelloWorld!");

	testdata[100] = tmpnode;

	//遍历map
	for (it=testdata.begin(); it!=testdata.end(); it++)
	{
		cout<<(*it).second.index<<"--"<<(*it).second.data<<endl;
	}
	

	//打印map大小
	cout<<"Size:"<<testdata.size()<<endl;
}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -