map.find() efficiency

If I have a map:
map myMap<string,vector<int>>

What would the best,average, and worst case time complexity be to find a key, and then iterate through the vector to find a specific int?

I know the map.find() method is O(log n), but does the fact that I have to then search for an int within a vector change the time complexity?

Thanks
I think doing it will still have the same complexity, but the overall time will be high because of the vector<int>'s equality operator, as it will check all the elements it has got.
Last edited on
It will be come
O( log n ) + O( m )
( map.find ) + ( search trough the vector )

If you want to iterate trough the vector then don't search for the array more than once

For Example :
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
vector< int > arr = { 1,5,3,2 };
map mmap< string, vector< int > >;
mmap[ "foo" ] = arr;

// Don't do this
for( unsigned i = 0; i < mmap[ "foo" ].size(); ++i ){

}

// And please do this
vector<int>& tmp = mmap[ "foo" ];
for( unsigned i = 0; i < tmp.size(); ++i ){

}
Topic archived. No new replies allowed.