Link: https: //www.cnblogs.com/jianfeifeng/p/11089799.html
For map object, count values can only return members is 0 or 1, map container allows only one key corresponds to one example. So count can effectively indicate the existence of a key. count returns the number of occurrences.
find returns an iterator pointing element, if the element is not present, end iterator returns.
Directly subscript presence of a dangerous side effect: If the key is not in the container map, then the subscript operator will insert a new element of the bond. In most cases, however, the user does not want to insert a key does not exist in the present container.
c provides two does not modify the map object query ++ in:
A, m.count (k) ==========> Returns the number of occurrences of k m
for example:
int occurs = 0;
if(word_count.count("foobar")){
occurs = word_count["foobar"];
}
// execution count before using the subscript operator symbol, in fact, made two Find the elements . If you want to use when the elements are present on it, you should find operation.
Two, m.find (k) ==========> If the element indexed by k m present container, the iterator returns the element. If not, then the end iterator returns.
for example:
int occurs = 0;
map<string, int>::iterator it = word_count.find("foobar");
if(it != word_count.end()){
occurs = it->second;
}
For map object, count values can only return members is 0 or 1, map container allows only one key corresponds to one example. So count can effectively indicate the existence of a key. count returns the number of occurrences.
find returns an iterator pointing element, if the element is not present, end iterator returns.
Directly subscript presence of a dangerous side effect: If the key is not in the container map, then the subscript operator will insert a new element of the bond. In most cases, however, the user does not want to insert a key does not exist in the present container.
c provides two does not modify the map object query ++ in:
A, m.count (k) ==========> Returns the number of occurrences of k m
for example:
int occurs = 0;
if(word_count.count("foobar")){
occurs = word_count["foobar"];
}
// execution count before using the subscript operator symbol, in fact, made two Find the elements . If you want to use when the elements are present on it, you should find operation.
Two, m.find (k) ==========> If the element indexed by k m present container, the iterator returns the element. If not, then the end iterator returns.
for example:
int occurs = 0;
map<string, int>::iterator it = word_count.find("foobar");
if(it != word_count.end()){
occurs = it->second;
}