罗马数字包含以下七种字符: I, V, X, L,C,D 和 M。
字符 数值
I 1
V 5
X 10
L 50
C 100
D 500
M 1000
例如, 罗马数字 2 写做 II ,即为两个并列的 1。12 写做 XII ,即为 X + II 。 27 写做 XXVII, 即为 XX + V + II 。
通常情况下,罗马数字中小的数字在大的数字的右边。但也存在特例,例如 4 不写做 IIII,而是 IV。数字 1 在数字 5 的左边,所表示的数等于大数 5 减小数 1 得到的数值 4 。同样地,数字 9 表示为 IX。这个特殊的规则只适用于以下六种情况:
I 可以放在 V (5) 和 X (10) 的左边,来表示 4 和 9。
X 可以放在 L (50) 和 C (100) 的左边,来表示 40 和 90。
C 可以放在 D (500) 和 M (1000) 的左边,来表示 400 和 900。
给定一个罗马数字,将其转换成整数。输入确保在 1 到 3999 的范围内。
求解:
#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;
}
输出1994
at函数
// 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;
}
输出:
0
alpha: 10
beta: 20
gamma: 30
begin/end函数
// 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函数
从map容器中删除所有元素,使容器的大小为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函数
在容器中搜索等于键值的元素,并返回匹配的数量。
因为map容器中的所有元素都是唯一的,所以该函数只能返回1(如果找到该元素)或0(否则)。
// 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;
}
输出:
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函数
在容器中搜索键值等于k的元素,并返回一个迭代器,否则返回一个映射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;
}
输出:
elements in mymap:
a => 50
c => 150
d => 200
erase函数
// 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;
}
输出:
a => 10
d => 40
size函数
// 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;
}
输出:
mymap.size() is 3