tags:

views:

102

answers:

1

I am initializing a map map<string,int> ex; in C++. I could not find contains_key or similar function in stl, hence I am just using ex[inputString]++;

The debugger shows the int to be initialized to zero correctly, is it a good assumption?

+4  A: 

Yes, values that do not exist when accessed with operator[] are default-constructed. For numeric values, this is 0.

That said, you're looking for the count method:

bool hasElement = ex.count("element");
GMan
thanks, good to know that it is initialized to zero, saves the if !exists then initialize kind of structure needed in python
@akshayubhat C++'s `map[k]` and Python's `dict.setdefault(k[,d])` are similar in that a non-existent key will be set to a default, as C++'s `map.find(k)->second` and Python's `dict[k]` similar in that a non-existent key will give an error. The two languages just chose different options for which to be the "common" operation.
ephemient