stlmap

How can I increase the performance in a map lookup with key type std::string?

I'm using an std::map (VC++ implementation) and it's a little slow for lookups via the map's find method. The key type is an std::string. Can I increase the performance of this std::map lookup via a custom key compare override for the map? For example, maybe std::string < compare doesn't take into consideration a simple string::size()...

C++ const std::map reference fails to compile

Is there a reason why passing a reference to a STL map as const causes the [] operator to break? I get this compiler error (gcc 4.2) when I use const: error: no match for ‘operator[]’ in ‘map[name]’ Here's the function prototype: void func(const char ch, std::string &str, const std::map<std::string, std::string> &map); And, I ...

Weird bug while inserting into C++ std::map

I'm trying to insert some value pairs into a std::map. In the first case, I receive a pointer to the map, dereference it and use the subscript operator to assign a value. i.e. (*foo)[index] = bar; Later, when I try to iterate over the collection, I'm returned key/value pairs which contain null for the value attribute in all cases ex...

C++ Thread-Safe Map

Does anyone know where I can find an implimentation that wraps an STL map and makes it thread safe? When I say thread safe I mean that it offers only serial access to the map, one thread at a time. Optimally, this map should use only STL and or boost constructs. ...

Why Can't I store references in an STL map in C++?

Hi I undersatnd that references are not pointers, but an alias to an object. However, I still don't understand what exactly this means to me as a programmer, i.e. what are references under the hood? I think the best way to understand this would be to understand why it is I can't store a reference in a map. I know I need to stop think...

What operations are thread-safe on std::map ?

Suppoes I have: stl::map<std::string, Foo> myMap; is the following function thread safe? myMap["xyz"] ? I.e. I want to have this giant read-only map that is shared among many threads; but I don't know if even searching it is thread safe. Thanks! EDIT: Everything is written to once first. Then after that, multiple threads read ...

C++ Long switch statement or look up with a map?

In my C++ application, I have some values that act as codes to represent other values. To translate the codes, I've been debating between using a switch statement or an stl map. The switch would look something like this: int code; int value; switch(code) { case 1: value = 10; break; case 2: value = 15; break; } The map...

User Defined Class as a Template Parameter

Hi, I' m implementing a custom STL map. I need to make sure that any data type (basic or user defined) key will work with it. I declared the Map class as a template which has two parameters for the key and the value. My question is if I need to use a string as the key type, how can I overload the < and > operators for string type keys o...

Displaying map stl

Declared a map early on: map<char*,char*> rtable; // used to store routing information Now I'm attempting to display the contents of the map: void Routes::viewroutes(){ typedef map<char*, char*>::const_iterator iter; for (iter=rtable.begin(); iter != rtable.end(); ++iter) { cout << iter->second << " " << iter->firs...

How to find the depth of each node in std::map?

If I construct, my own binary tree, then I can find the depth of each node. The sample code is as follows template<class datatype> void binary_node<datatype>::printNodeWithDepth(int currentNodeDepth) { if ( left ) left->printNodeWithDepth(currentNodeDepth+1); std::cout << value << " and the depth is " << currentNodeDept...

Intersection of two STL maps

Given that I have two STL maps, say: map<int, double> A; map<int, double> B; I'd like to get the intersection of the two maps, something of the form: map<int, pair<double,double> > C; Where the keys are the values in both A and B and the value is a pair of the values from A and B respectively. Is there a clean STL-like way to go ab...

Does map<key, data> in C++ STL support native data types like Structures?

I am trying to write something like this. My question is how do I map a key to a native data type like structure. I wrote this snipped but I couldn't compile it. Do you have any ideas on how to achieve this thing? #include <map> #include <iostream> typedef struct _list { int a,b; }list; map<int,list> test_map; int main(void) { cou...