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