stdmap

How to get the first n elements of a std::map

青春壹個敷衍的年華 提交于 2019-12-04 05:37:45
Since there is no .resize() member function in C++ std::map I was wondering, how one can get a std::map with at most n elements. The obvious solution is to create a loop from 0 to n and use the nth iterator as the first parameter for std::erase(). I was wondering if there is any solution that does not need the loop (at least not in my user code) and is more "the STL way to go". You can use std::advance( iter, numberofsteps ) for that. Universal solution for almost any container, such as std::list, std::map, boost::multi_index. You must check the size of your map only. template<class It> It

efficient way to get key from std::map value

百般思念 提交于 2019-12-04 03:24:07
问题 I have a map as below : std::map< std::string ,int> mapobj; mapobj["one"] = 1; mapobj["two"] = 2; mapobj["three"] =3 ; how to get key when input is value EX : input : 1 output : one Note : In my case value is unique 回答1: A one-to-one mapping is actually quite easy, the fastest way to do it is to probably maintain two maps, one for each direction. It becomes more complicated if it's not one-to-one since you'll need to provide a way to get a collection of values or key, rather than a single one

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

半城伤御伤魂 提交于 2019-12-03 22:07:31
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 would be an stl::map<int, int> and translation would be a simple lookup with the code used as the key value. Which one is better/more efficient/cleaner/accepted? Why? Personally, I would use the map, as its use implies a data lookup - using a switch usually indicates a

Does std::map::iterator return a copy of value or a value itself?

女生的网名这么多〃 提交于 2019-12-03 14:44:57
问题 I'm trying to create a map inside a map: typedef map<float,mytype> inner_map; typedef map<float,inner_map> outer_map; Will I be able to put something inside inner map, or does iterator::second returns a copy? stl_pair.h suggests the latter: 74: _T2 second; ///< @c second is a copy of the second object but my test program run fine with the code like this: it = my_map.lower_bound(3.1415); (*it).second.insert(inner_map::value_type(2.71828,"Hello world!"); So where is the truth? Is this a copy or

Most efficient way to assign values to maps

谁都会走 提交于 2019-12-03 11:05:17
Which way to assign values to a map is most efficient? Or are they all optimized to the same code (on most modern compilers)? // 1) Assignment using array index notation Foo["Bar"] = 12345; // 2) Assignment using member function insert() and STL pair Foo.insert(std::pair<string,int>("Bar", 12345)); // 3) Assignment using member function insert() and "value_type()" Foo.insert(map<string,int>::value_type("Bar", 12345)); // 4) Assignment using member function insert() and "make_pair()" Foo.insert(std::make_pair("Bar", 12345)); (I know I could benchmark and check compiler output, but this question

Why does std::unordered_map have a reserve method?

我的未来我决定 提交于 2019-12-03 10:20:19
According to this you cannot reserve space for std::map : No, the members of the map are internally stored in a tree structure. There is no way to build the tree until you know the keys and values that are to be stored. From this it is obvious why std::map would lack a reserve() method, which it does on cppreference.com. However, std::unordered_map does have a reserve() method, but when I try to use it with operator[] , insert() or emplace() they all go to allocate memory despite me having called reserve() first. What's up with this? Why won't reserve() properly reserve the required space? And

LevelDB vs. std::map

試著忘記壹切 提交于 2019-12-03 10:03:20
问题 In our application we use std::map to store (key, value) data and use serialization to store that data on disk. With this approach we are finding that the disk I/O is performance bottleneck and finding values using key is not very fast. I have come across LevelDB and thinking of using it. But I have some questions. LevelDB's documentation says its made for (string, string) key value pair. Does it mean that I can not use for custom key value pairs? It seems the difference between std::map and

How can I use a struct as key in a std::map?

半世苍凉 提交于 2019-12-03 09:33:54
I have the following code, but I get an error on on the last line: struct coord { int x, y; bool operator=(const coord &o) { return x == o.x && y == o.y; } bool operator<(const coord &o) { return x < o.x || (x == o.x && y < o.y); } }; map<coord, int> m; pair<coord, int> p((coord{0,0}),123); m.insert(p); // ERROR here How can I use a struct as key in a map? I tried to change the code to this: struct coord { int x, y; bool const operator==(const coord &o) { return x == o.x && y == o.y; } bool const operator<(const coord &o) { return x < o.x || (x == o.x && y < o.y); } }; But I'm still getting

VS2010 RC - only 100 std::map elements in debugger

浪子不回头ぞ 提交于 2019-12-03 07:00:42
I have a small problem during debugging my App in VS 2010 RC when I want to see all the elements of std::map container. When debugger reaches the breakpoint and I want to check the values of the map in element inspector (in 'Locals' windows and in pop-up windows after hovering the variable name with mouse as well) and I'm scrolling down the list of the elements it stops on the 100 element and I can't next elements. The map contains more than 200 elements (map's counter parameter shows this properly) but I can't view them all in the element inspector. The problem appears even in the most simple

advantages of std::set vs vectors or maps

故事扮演 提交于 2019-12-03 04:32:50
问题 This may be a stupid question, I am quite new to C++ and programming in general. I wish to understand the use of several STL containers and with that in mind, I was wondering what the advantages are of using std::set vs for example using vectors or maps? I can't seem to find an explicit answer to this question. I noticed that sets use maps, but then why not always use maps or always use sets. Instead 2 quite similar containers are provided. Thanks in advance. 回答1: Both std::set and std::map