-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmap1.cpp
More file actions
46 lines (36 loc) · 1.05 KB
/
map1.cpp
File metadata and controls
46 lines (36 loc) · 1.05 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
#include<iostream>
#include<map>
#include<string>
using namespace std;
int main(){
/*Create a map/associative array
- keys are strings
- values are floats
*/
typedef map<string,float> StringFloatMap;
StringFloatMap stocks;
//inserts some elements
stocks["BASF"] = 369.50;
stocks["VW"] = 413.50;
stocks["Daimler"] = 819.00;
stocks["BMW"] = 834.00;
stocks["Siemens"] = 842.20;
//print all elements
StringFloatMap::iterator pos;
//boom (all prices doubled)
for(pos = stocks.begin(); pos!= stocks.end(); pos++){
cout<<"Stock: "<<pos->first<<"\t"
<<"Price: "<<pos->second<<endl;
}
cout<<endl;
/*rename the key from "VW" to "Volkswagen"
- only provided by exchanging element
*/
stocks["Volkswagen"] = stocks["VW"];
stocks.erase("VW");
for(pos = stocks.begin(); pos!= stocks.end(); pos++){
cout<<"Stock: "<<pos->first<<"\t"
<<"Price: "<<pos->second<<endl;
}
cout<<endl;
}