stdmap

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

本小妞迷上赌 提交于 2019-12-03 04:30:00
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 not? The comment in stl_pair.h is misleading in this specific case. There will be no copy, since the

LevelDB vs. std::map

孤者浪人 提交于 2019-12-03 01:36:45
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 LevelDB is that LevelDB is persistent and std::map works in memory. So does it mean the disk I/O

When I should use std::map::at to retrieve map element

强颜欢笑 提交于 2019-12-03 01:21:32
问题 I have read different articles on web and questions at stackoverflow, but for me it is not clear is there any exclusive case when it is better to use std::map::at to retrieve map element. According to definition, std::map::at Returns a reference to the mapped value of the element identified with key k. If k does not match the key of any element in the container, the function throws an out_of_range exception. For me only case when it is worth to use std::map::at when you 100% sure that element

advantages of std::set vs vectors or maps

自古美人都是妖i 提交于 2019-12-02 17:53:52
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. Both std::set and std::map are associative containers. The difference is that std::set s contain only the key, while in std::map there

Use of for_each on map elements

拥有回忆 提交于 2019-12-02 17:16:35
I have a map where I'd like to perform a call on every data type object member function. I yet know how to do this on any sequence but, is it possible to do it on an associative container? The closest answer I could find was this: Boost.Bind to access std::map elements in std::for_each . But I cannot use boost in my project so, is there an STL alternative that I'm missing to boost::bind? If not possible, I thought on creating a temporary sequence for pointers to the data objects and then, call for_each on it, something like this: class MyClass { public: void Method() const; } std::map<int,

When I should use std::map::at to retrieve map element

限于喜欢 提交于 2019-12-02 14:37:09
I have read different articles on web and questions at stackoverflow , but for me it is not clear is there any exclusive case when it is better to use std::map::at to retrieve map element. According to definition , std::map::at Returns a reference to the mapped value of the element identified with key k. If k does not match the key of any element in the container, the function throws an out_of_range exception. For me only case when it is worth to use std::map::at when you 100% sure that element with particular key exist, otherwise you should consider exception handling. Is there any case where

initialize std::map value when its key does not exist

房东的猫 提交于 2019-12-01 23:41:08
I read in here that the std::map operator[] create an object if the key doesn't exist ! 1st of all may I know where I can find a reference for this claim?(Although I know it is true) Next, imagin the following code snippet: #include <iostream> #include <vector> #include<map> class Value { //.. int some_member; //is used for any purpose that you like std::vector<int> some_container; public: Value(int some_member_) : some_member(some_member_) { std::cout << "Hello from the one-argument constructor" << std::endl; } Value() { std::cout << "Hello from the no argument constructor" << std::endl; }

initialize std::map value when its key does not exist

懵懂的女人 提交于 2019-12-01 23:31:17
问题 I read in here that the std::map operator[] create an object if the key doesn't exist ! 1st of all may I know where I can find a reference for this claim?(Although I know it is true) Next, imagin the following code snippet: #include <iostream> #include <vector> #include<map> class Value { //.. int some_member; //is used for any purpose that you like std::vector<int> some_container; public: Value(int some_member_) : some_member(some_member_) { std::cout << "Hello from the one-argument

std::map::operator[]

狂风中的少年 提交于 2019-12-01 23:29:11
问题 I was doing a simple map program but ended up with this question. The c++ doc says this: Access element If k matches the key of an element in the container, the function returns a reference to its mapped value. If k does not match the key of any element in the container, the function inserts a new element with that key and returns a reference to its mapped value. Notice that this always increases the container size by one, even if no mapped value is assigned to the element (the element is

std::map::operator[]

老子叫甜甜 提交于 2019-12-01 21:19:24
I was doing a simple map program but ended up with this question. The c++ doc says this: Access element If k matches the key of an element in the container, the function returns a reference to its mapped value. If k does not match the key of any element in the container, the function inserts a new element with that key and returns a reference to its mapped value. Notice that this always increases the container size by one, even if no mapped value is assigned to the element (the element is constructed using its default constructor). The part I don't really get is where it says "the element is