maptest.cpp
来自「c++中关于stl的详细介绍 有多本书籍在里面」· C++ 代码 · 共 69 行
CPP
69 行
#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 + =
减小字号Ctrl + -
显示快捷键?