![](http://www.cppblog.com/Images/OutliningIndicators/ContractedBlock.gif)
![](http://www.cppblog.com/Images/OutliningIndicators/ExpandedBlockStart.gif)
1
要求: 将mymap中itemstruct 的a大于100的项删除
2
struct itemstruct
3![](http://www.cppblog.com/Images/OutliningIndicators/ExpandedBlockStart.gif)
![](http://www.cppblog.com/Images/OutliningIndicators/ContractedBlock.gif)
{
4
int a;
5
char b[20];
6
};
7
map<string, itemstruct > mymap.
8![](http://www.cppblog.com/Images/OutliningIndicators/None.gif)
9
解答1:
10
#include <iostream>
11
#include <ctime>
12
#include <map>
13
using namespace std;
14
typedef struct itemstruct
15![](http://www.cppblog.com/Images/OutliningIndicators/ExpandedBlockStart.gif)
![](http://www.cppblog.com/Images/OutliningIndicators/ContractedBlock.gif)
{
16
int a;
17
char b[20];
18
}itemS;
19![](http://www.cppblog.com/Images/OutliningIndicators/ExpandedBlockStart.gif)
itemS s[4] =
{
{102,"what"},
20![](http://www.cppblog.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
![](http://www.cppblog.com/Images/OutliningIndicators/ContractedSubBlock.gif)
{33, "hello"},
21![](http://www.cppblog.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
![](http://www.cppblog.com/Images/OutliningIndicators/ContractedSubBlock.gif)
{198,"world"},
22![](http://www.cppblog.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
![](http://www.cppblog.com/Images/OutliningIndicators/ContractedSubBlock.gif)
{45, "c++"}
23
};;
24![](http://www.cppblog.com/Images/OutliningIndicators/None.gif)
25![](http://www.cppblog.com/Images/OutliningIndicators/None.gif)
26
int main()
27![](http://www.cppblog.com/Images/OutliningIndicators/ExpandedBlockStart.gif)
![](http://www.cppblog.com/Images/OutliningIndicators/ContractedBlock.gif)
{
28
map<string, itemS> mymap;
29![](http://www.cppblog.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
string str[4] =
{"1st","2nd","3rd","4th"};
30
for(int i = 0; i<4; i++)
31![](http://www.cppblog.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
![](http://www.cppblog.com/Images/OutliningIndicators/ContractedSubBlock.gif)
{
32
mymap.insert(make_pair(str[i], s[i]));
33
}
34![](http://www.cppblog.com/Images/OutliningIndicators/InBlock.gif)
35
map<string,itemS>::iterator it;
36
for(it=mymap.begin(); it!=mymap.end(); it++)
37![](http://www.cppblog.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
![](http://www.cppblog.com/Images/OutliningIndicators/ContractedSubBlock.gif)
{
38![](http://www.cppblog.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
if(it->second.a >100)
{
39
i=mymap.erase(it); ----->正确
40
mymap.erase(it); ----->it失效..
41
}
42
}
43
//first是Key, second是value;
44
for(it = mymap.begin(); it!=mymap.end(); it++)
45![](http://www.cppblog.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
![](http://www.cppblog.com/Images/OutliningIndicators/ContractedSubBlock.gif)
{
46
cout<<it->first<<" "<<it->second.a<<" "<<it->second.b<<endl;
47
}
48
system("pause");
49
return 0;
50
}
51![](http://www.cppblog.com/Images/OutliningIndicators/None.gif)
52
解答2:
53
#include<map>
54
#include<iterator>
55
#include<string>
56
#include<iostream>
57
#include<cstring>
58
using namespace std;
59
struct itemstruct
60![](http://www.cppblog.com/Images/OutliningIndicators/ExpandedBlockStart.gif)
![](http://www.cppblog.com/Images/OutliningIndicators/ContractedBlock.gif)
{
61
int a;
62
char b[20];
63
itemstruct(int t,char*str)
64![](http://www.cppblog.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
![](http://www.cppblog.com/Images/OutliningIndicators/ContractedSubBlock.gif)
{
65
a=t;
66
strcpy(b,str);
67
}
68
};
69
int main()
70![](http://www.cppblog.com/Images/OutliningIndicators/ExpandedBlockStart.gif)
![](http://www.cppblog.com/Images/OutliningIndicators/ContractedBlock.gif)
{
71
map<string,itemstruct>mymap;
72
mymap.insert(make_pair("a",itemstruct(10,"hanzhou")));
73
mymap.insert(make_pair("ab",itemstruct(20,"fuzhou")));
74
mymap.insert(make_pair("abc",itemstruct(30,"zhengzhou")));
75
mymap.insert(make_pair("abcd",itemstruct(200,"wuhan")));
76
mymap.insert(make_pair("abcde",itemstruct(150,"kunming")));
77
mymap.insert(make_pair("abcdef",itemstruct(50,"xiamen")));
78
map<string,itemstruct>::iterator it=mymap.begin();
79
while(it!=mymap.end())
80![](http://www.cppblog.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
![](http://www.cppblog.com/Images/OutliningIndicators/ContractedSubBlock.gif)
{
81
if((it->second).a>100)mymap.erase(it++);
82
else it++;
83
}
84
it=mymap.begin();
85
while(it!=mymap.end())
86![](http://www.cppblog.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
![](http://www.cppblog.com/Images/OutliningIndicators/ContractedSubBlock.gif)
{
87
cout<<it->first<<" "<<(it->second).a<<" "<<(it->second).b<<endl;
88
it++;
89
}
90
system("PAUSE");
91
return 0;
92
}
93![](http://www.cppblog.com/Images/OutliningIndicators/None.gif)
94
解答3:
95
for(map<string, itemstruct>::iterator i = mymap.begin(); i != mymap.end();)
96![](http://www.cppblog.com/Images/OutliningIndicators/ExpandedBlockStart.gif)
![](http://www.cppblog.com/Images/OutliningIndicators/ContractedBlock.gif)
{
97
if(i->second.a > 100)
98
i = mymap.erase(i);
99
else
100
++i;
101
}
102![](http://www.cppblog.com/Images/OutliningIndicators/None.gif)
103
解答4: VC6中编译map编译出错的解决方法
104
Warnings similar to the following are generated even if you use the warning pragma to disable the warning: warning C4786: 'std::rb_tree<CAiSpanningTree<State,std::less<State>>::TransClosureNode, CAiSpanningTree<State,std::less<State>>::TransClosureNode,std::ident<Cai SpanningTree<State,std::less<State>>::TransClosureNode,CAiSpanningTree<S tate,std::less<State>>::TransClosureNode>,std::less<CAiSpanningTree<Stat e,std::less<State>>::TransClosureNode>>' : identifier was truncated to '255' characters in the debug information
105![](http://www.cppblog.com/Images/OutliningIndicators/None.gif)
106![](http://www.cppblog.com/Images/OutliningIndicators/None.gif)
107
解决code 加在stdafx.h的头文件处:
108![](http://www.cppblog.com/Images/OutliningIndicators/None.gif)
109
#pragma warning(disable:4786)
110![](http://www.cppblog.com/Images/OutliningIndicators/None.gif)
C++中map容器提供一个键值对容器,map与multimap差别仅仅在于multiple允许一个键对应多个值。
1
#include <iostream>
2
#include <iomanip>
3
#include <string>
4
#include <map>
5
using namespace std;
6
int main()
7![](http://www.cppblog.com/Images/OutliningIndicators/ExpandedBlockStart.gif)
![](http://www.cppblog.com/Images/OutliningIndicators/ContractedBlock.gif)
{
8
map<string,int> m;
9
int n; cin>>n;
10
string s;
11
int x;
12
for (int i=1;i<=n;i++)
13![](http://www.cppblog.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
![](http://www.cppblog.com/Images/OutliningIndicators/ContractedSubBlock.gif)
{
14
cin>>s>>x;
15
m[s]=x;
16
}
17
int k;
18
cin>>k;
19
for (int i=1;i<=k;i++)
20![](http://www.cppblog.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
![](http://www.cppblog.com/Images/OutliningIndicators/ContractedSubBlock.gif)
{
21
cin>>s;
22
cout<<m[s]<<endl;
23
}
24
return 0;
25
}
26![](http://www.cppblog.com/Images/OutliningIndicators/None.gif)
27![](http://www.cppblog.com/Images/OutliningIndicators/None.gif)
28
一、map的说明
29
1 头文件
30
#include <map>
31![](http://www.cppblog.com/Images/OutliningIndicators/None.gif)
32
2 定义
33
map<string, int> my_Map;
34
或者是typedef map<string, int> MY_MAP;
35
MY_MAP my_Map;
36![](http://www.cppblog.com/Images/OutliningIndicators/None.gif)
37
3 插入数据
38
(1) my_Map["a"] = 1;
39
(2) my_Map.insert(map<string, int>::value_type("b",2));
40
(3) my_Map.insert(pair<string,int>("c",3));
41
(4) my_Map.insert(make_pair<string,int>("d",4));
42![](http://www.cppblog.com/Images/OutliningIndicators/None.gif)
43
4 查找数据和修改数据
44
(1) int i = my_Map["a"];
45
my_Map["a"] = i;
46
(2) MY_MAP::iterator my_Itr;
47
my_Itr.find("b");
48
int j = my_Itr->second;
49
my_Itr->second = j;
50
不过注意,键本身是不能被修改的,除非删除。
51![](http://www.cppblog.com/Images/OutliningIndicators/None.gif)
52
5 删除数据
53
(1) my_Map.erase(my_Itr);
54
(2) my_Map.erase("c");
55
还是注意,第一种情况在迭代期间是不能被删除的,道理和foreach时不能删除元素一样。
56![](http://www.cppblog.com/Images/OutliningIndicators/None.gif)
57
6 迭代数据
58![](http://www.cppblog.com/Images/OutliningIndicators/ExpandedBlockStart.gif)
for (my_Itr=my_Map.begin(); my_Itr!=my_Map.end(); ++my_Itr)
{}
59![](http://www.cppblog.com/Images/OutliningIndicators/None.gif)
60
7 其它方法
61
my_Map.size() 返回元素数目
62
my_Map.empty() 判断是否为空
63
my_Map.clear() 清空所有元素
64
可以直接进行赋值和比较:=, >, >=, <, <=, != 等等
65![](http://www.cppblog.com/Images/OutliningIndicators/None.gif)
66
更高级的应用查帮助去吧,^_^;
posted on 2009-11-23 00:33
纳兰伴月 阅读(632)
评论(0) 编辑 收藏 引用