Roman numerals contain the following seven characters: I, V, X, L, C, D, and M.
Character value
I 1
V 5
X 10
L 50
C 100
D 500
M 1000
For example, the Roman numeral 2 is written as II, that is, two parallel ones. 12 write XII, that is, X + II. 27 write XXVII, that is XX + V + II.
Usually, the small numbers in roman numbers are to the right of the big ones. But there are also special cases. For example, 4 does not write IIII, but IV. The number 1 is to the left of the number 5. The number represented is equal to the number 4 obtained by subtracting the decimal 1 from the large number 5. Similarly, the number 9 is expressed as IX. This special rule only applies to the following six cases:
I can be placed to the left of V (5) and X (10) to represent 4 and 9. X can be placed to the left of L (50) and C (100) to represent 40 and 90. C can be placed to the left of D (500) and M (1000) to represent 400 and 900.
Given a Roman number, convert it to an integer. Enter to make sure it is in the range of 1 to 3999.
Solution:
#include <iostream> #include<map> #include<unordered_map> using namespace std; int romanToInt(string s) { int result=0; map<char,int> mymap={ {'I',1}, {'V',5}, {'X',10}, {'L',50}, {'C',100}, {'D',500}, {'M',1000} }; for(int i=0;i<s.length()-1;i++) { if(mymap[s[i]]<mymap[s[i+1]]) { result-=mymap[s[i]]; } else { result+=mymap[s[i]]; } } result+=mymap[s.back()]; return result; } int main() { string s="MCMXCIV"; cout << romanToInt(s) << endl; return 0; }
Output 1994
at function
// map::at #include <iostream> #include <string> #include <map> using namespace std; int main () { std::map<std::string,int> mymap = { { "alpha", 0 }, { "beta", 0 }, { "gamma", 0 } }; cout<<mymap["alpha"]<<endl; mymap.at("alpha") = 10; mymap.at("beta") = 20; mymap.at("gamma") = 30; for (auto& x: mymap) { std::cout << x.first << ": " << x.second << '\n'; } return 0; }
Output:
0
alpha: 10
beta: 20
gamma: 30
begin/end function
// map::begin/end #include <iostream> #include <map> int main () { std::map<char,int> mymap; mymap['b'] = 100; mymap['a'] = 200; mymap['c'] = 300; // show content: for (std::map<char,int>::iterator it=mymap.begin(); it!=mymap.end(); ++it) std::cout << it->first << " => " << it->second << '\n'; return 0; }
clear function
Remove all elements from the map container so that the size of the container is 0.
// map::clear #include <iostream> #include <map> int main () { std::map<char,int> mymap; mymap['x']=100; mymap['y']=200; mymap['z']=300; std::cout << "mymap contains:\n"; for (std::map<char,int>::iterator it=mymap.begin(); it!=mymap.end(); ++it) std::cout << it->first << " => " << it->second << '\n'; mymap.clear(); mymap['a']=1101; mymap['b']=2202; std::cout << "mymap contains:\n"; for (std::map<char,int>::iterator it=mymap.begin(); it!=mymap.end(); ++it) std::cout << it->first << " => " << it->second << '\n'; return 0; }
count function
Searches the container for elements equal to the key value and returns the number of matches.
Because all elements in the map container are unique, the function can only return 1 (if the element is found) or 0 (otherwise).
// map::count #include <iostream> #include <map> using namespace std; int main () { std::map<char,int> mymap; char c; mymap ['a']=-1; mymap ['c']=202; mymap ['f']=303; mymap ['a']=-2; for (c='a'; c<'h'; c++) { std::cout << c; if (mymap.count(c)>0) std::cout << " is an element of mymap.\n"; else std::cout << " is not an element of mymap.\n"; } return 0; }
Output:
a is an element of mymap.
b is not an element of mymap.
c is an element of mymap.
d is not an element of mymap.
e is not an element of mymap.
f is an element of mymap.
g is not an element of mymap.
find function
Search the container for an element with a key value equal to k and return an iterator, otherwise, an iterator that maps map::end.
// map::find #include <iostream> #include <map> int main () { std::map<char,int> mymap; std::map<char,int>::iterator it; mymap['a']=50; mymap['b']=100; mymap['c']=150; mymap['d']=200; it = mymap.find('b'); if (it != mymap.end()) mymap.erase (it); // print content: std::cout << "elements in mymap:" << '\n'; std::cout << "a => " << mymap.find('a')->second << '\n'; std::cout << "c => " << mymap.find('c')->second << '\n'; std::cout << "d => " << mymap.find('d')->second << '\n'; return 0; }
Output:
elements in mymap:
a => 50
c => 150
d => 200
erase function
// erasing from map #include <iostream> #include <map> int main () { std::map<char,int> mymap; std::map<char,int>::iterator it; // insert some values: mymap['a']=10; mymap['b']=20; mymap['c']=30; mymap['d']=40; mymap['e']=50; mymap['f']=60; it=mymap.find('b'); mymap.erase (it); // erasing by iterator mymap.erase ('c'); // erasing by key it=mymap.find ('e'); mymap.erase ( it, mymap.end() ); // erasing by range // show content: for (it=mymap.begin(); it!=mymap.end(); ++it) std::cout << it->first << " => " << it->second << '\n'; return 0; }
Output:
a => 10
d => 40
size function
// map::size #include <iostream> #include <map> int main () { std::map<char,int> mymap; mymap['a']=101; mymap['b']=202; mymap['c']=302; std::cout << "mymap.size() is " << mymap.size() << '\n'; return 0; }
Output:
mymap.size() is 3