stdmap

std::map insert or std::map find?

Assuming a map where you want to preserve existing entries. 20% of the time, the entry you are inserting is new data. Is there an advantage to doing std::map::find then std::map::insert using that returned iterator? Or is it quicker to attempt the insert and then act based on whether or not the iterator indicates the record was or was ...

For std::map, how will insert behave if it has to resize the container and the memory is not available?

For std::map, how will insert behave if it has to resize the container and the memory is not available? ...

Can anyone recommend a C++ std::map replacement container?

Maps are great to get things done easily, but they are memory hogs and suffer from caching issues. And when you have a map in a critical loop that can be bad. So I was wondering if anyone can recommend another container that has the same API but uses lets say a vector or hash implementation instead of a tree implementation. My goal here...

Initializing a static std::map<int, int> in C++

What is the right way of initializing a static map? Do we need a static function that will initialize it? ...

last key in a std::map

I am looking for the highest key value (a defined by the comparison operator) of a std::map. Is this guaranteed to be map.rbegin()->first ? (I am a bit shaky on reverse iterators, and how much freedom there is in the implementation of std::map) If not, please advise. I cannot change the data structure. ...

In STL maps, is it better to use map::insert than []?

A while ago, I had a discussion with a colleague about how to insert values in STL maps. I preferred map[key] = value; because it feels natural and is clear to read whereas he preferred map.insert(std::make_pair(key, value)) I just asked him and neither of us can remember the reason why insert is better, but I am sure it was not just a...

Checking value exist in a std::map - C++

I know find method finds the supplied key in std::map and return an interator to the element. Is there anyway to find the value and get an iterator to the element? What I need to do is to check specified value exist in std::map. I have done this by looping all items in the map and comparing. But I wanted to know is there any better appro...

C++ std::map of template-class values

Hi everyone, I'm attempting to declare a Row and a Column class, with the Row having a private std::map with values pointing to a templated Column. Something like this: template <typename T> class DataType { private: T type; }; template <typename T> class Field { private: T value; DataType<T> value; }; class Row { pri...

Why is memory still accessible after std::map::clear() is called?

Hello, I am observing strange behaviour of std::map::clear(). This method is supposed to call element's destructor when called, however memory is still accessible after call to clear(). For example: struct A { ~A() { x = 0; } int x; }; int main( void ) { std::map< int, A * > my_map; A *a = new A(); a->x = 5; my_map.insert...

Nested struct in templated class with std::map::const_iterator?

The folowing code generates a syntax error at the line where the iterator is declared: template <typename T> class A { public: struct B { int x, y, z; }; void a() { std::map<int, B>::const_iterator itr; // error: ; expected before itr } std::vector<T> v; std::map<int, B> m; }; This o...

How can i estimate memory usage of stl::map?

For example, I have a std::map with known sizeof(A) and sizefo(B), while map has N entries inside. How would you estimate its memory usage? I'd say it's something like (sizeof(A) + sizeof(B)) * N * factor But what is the factor? Different formula maybe? Update: Maybe it's easier to ask for upper bound? ...

std::map iterator not iterating in MFC app

I have a std::map declared thusly in a legacy MFC application: typedef std::map<long, CNutrientInfo> NUTRIENT_INFO_MAP; typedef NUTRIENT_INFO_MAP::const_iterator NUTRIENT_INFO_ITER; typedef NUTRIENT_INFO_MAP::value_type NUTRIENT_INFO_PAIR; static NUTRIENT_INFO_MAP m_NutrientInfoMap; m_NutrientInfoMap is populated when the app loads by...

iterator vs reverse_iterator

Hi, I'm using std::map to store a lot of elements (pairs of elements) and I have a "little" doubt, what is more efficient to iterate all elements over my std::map, iterator or reverse_iterator? Thank's. Salu2. ...

Custom types as key for a map - C++

I am trying to assign a custom type as a key for std::map. Here is the type which I am using as key. struct Foo { Foo(std::string s) : foo_value(s){} bool operator<(const Foo& foo1) { return foo_value < foo1.foo_value; } bool operator>(const Foo& foo1) { return foo_value > foo1.foo_value; } std::string foo_value; };...

Unusual std::map runtime error

I'm hacking together an editor for a game I'm working on and as part of that editor, I need to have textures, obviously. I've created a std::map variable as so, std::map<std::string, unsigned int> textures; In my image loading code, I have the following snippet. unsigned int id; glGenTextures(1, &id); glBindTexture(GL_TEXTURE_2D, id)...

C++ Storing references to values in std::map

Am I right in assuming that adding/removing elements to an std::map does not effect the other elements (ie cause them to be relocated in memory) and so that the following is safe: I looked at various sites with info on the container but only found out about the cases where iterators are invalidated, which I already know... std::map<std...

How do I initialize a static std::map?

I've made a message-only window class, and I'm trying to map HWNDs back to the objects with those handles. I'm trying to do that using a private static std::map<HWND, CMyClass*> belonging to the class, like this: MyClass.h: class CMyClass { ... private: HWND m_hWnd; HINSTANCE m_hInstance; LPCSTR m_szClas...

Is there a difference between std::map<int, int> and std::map<const int, int>?

From what I understand, the key in a value pair in an std::map cannot be changed once inserted. Does this mean that creating a map with the key template argument as const has no effect? std::map<int, int> map1; std::map<const int, int> map2; ...

How to pass 2D map as a parameter to a function in c++?

Hi all, I have a map like std::map< int, int> random[50]; How can i pass this map as a parameter to a function say Perform()? Thanks in advance. ...

Why does std::map operator[] create an object if the key doesn't exist?

Hi, I'm pretty sure I already saw this question somewhere (comp.lang.c++? Google doesn't seem to find it there either) but a quick search here doesn't seem to find it so here it is: Why does the std::map operator[] create an object if the key doesn't exist? I don't know but for me this seems counter-intuitive if you compare to most othe...