1.list的front函数和back函数,在list空时,直接调用,在不同平台上返回结果不同,会返回未知错误,因此调用前务必要判断是否空。Calling this function on an empty container causes undefined behavior.
2.map的[]操作符,可以用来进行键值赋值,但不可以直接用来判断或赋值给其它变量,如:
std::map<int, int> mymap;
mymap[1] = 100;//ok,mymap.size() =1
int n = mymap[2];//此时,将会增加了一个key为2,value为NULL。mymap.size=2。
因此最后的方法,是使用find函数。
----
If k does not match the key of any element in the container, the function inserts a new element with that key and returns a reference to its mapped value. Notice that this always increases the container size by one, even if no mapped value is assigned to the element (the element is constructed using its default constructor).
但对于at,如果key不存在,会抛出异常。
A similar member function, map::at, has the same behavior when an element with the key exists, but throws an exception when it does not.