stdmap

map, lambda, remove_if

给你一囗甜甜゛ 提交于 2019-11-27 02:48:08
问题 So, i've problem with std::map, lambda and stl algorithm(remove_if). Actually, same code with std::list or std::vector works well. My test example : #include <map> #include <iostream> #include <algorithm> struct Foo { Foo() : _id(0) {} Foo(int id) : _id(id) { } int _id; }; typedef std::map<int, Foo> FooMap; int main() { FooMap m; for (int i = 0; i < 10; ++i) m[i + 100] = Foo(i); int removeId = 6; // <<< Error here >>> std::remove_if(m.begin(), m.end(), [=](const FooMap::value_type & item) {

Is there a Java Map keySet() equivalent for C++'s std::map?

不羁的心 提交于 2019-11-26 23:21:27
问题 Is there a Java Map keySet() equivalent for C++'s std::map ? The Java keySet() method returns "a set view of the keys contained in this map." 回答1: All of the answers presented thus far end up creating a std::set directly, which may not be ideal: if you only want to be able to iterate over the keys, you don't want to have the overhead of creating a whole new container. A more flexible option would be to use a transform iterator that converts a std::map iterator into some type of iterator that

std::map thread-safety

女生的网名这么多〃 提交于 2019-11-26 22:51:32
问题 Is reference to object in std::map is thread safe? std::map< std::string, Object > _objects; map can be changed from many threads and this access is synchronized, but reference to value (Object &) accessable just from 1 instance and thread. is write operations with Object & is safe if another thread will add items to map? will it reallocate? 回答1: The C++11 standard guarantees that const method access to containers is safe from different threads (ie, both use const methods). In addition,

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

回眸只為那壹抹淺笑 提交于 2019-11-26 22:38:48
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 other operator[] (like std::vector) where if you use it you must be sure that the index exists. I'm wondering what's the rationale for implementing this behavior in std::map. Like I said wouldn't it be more intuitive to act more like an index in a vector and crash (well

std::map default value for build-in type

喜夏-厌秋 提交于 2019-11-26 22:32:05
Recently, I was confused by the std::map operator[] function. In the MSDN library, it says: "If the argument key value is not found, then it is inserted along with the default value of the data type." I tryed to search much more exactly explanation for this issue. For example here: std::map default value In this page, Michael Anderson said that "the default value is constructed by the default constructor(zero parameter constructor)". Now my quest comes to this:"what the default value for the build-in type?". Was it compiler related? Or is there a standard for this issue by the c++ stardard

std::map default value

人盡茶涼 提交于 2019-11-26 22:04:23
Is there a way to specify the default value std::map 's operator[] returns when an key does not exist? No, there isn't. The simplest solution is to write your own free template function to do this. Something like: #include <string> #include <map> using namespace std; template <typename K, typename V> V GetWithDef(const std::map <K,V> & m, const K & key, const V & defval ) { typename std::map<K,V>::const_iterator it = m.find( key ); if ( it == m.end() ) { return defval; } else { return it->second; } } int main() { map <string,int> x; ... int i = GetWithDef( x, string("foo"), 42 ); } C++11

Custom types as key for a map - C++

烈酒焚心 提交于 2019-11-26 20:23:13
问题 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; }; When used with std::map , I am getting the following error. error C2678: binary '<' : no operator found which takes a left-hand operand of type 'const Foo' (or there is no

Find mapped value of map

老子叫甜甜 提交于 2019-11-26 19:56:31
问题 Is there a way in C++ to search for the mapped value (instead of the key) of a map, and then return the key? Usually, I do someMap.find(someKey)->second to get the value, but here I want to do the opposite and obtain the key (the values and keys are all unique). 回答1: Because of how a map is designed, you'll need to do the equivalent of a search on unordered data. for (auto it = someMap.begin(); it != someMap.end(); ++it) if (it->second == someValue) return it->first; 回答2: Using lambdas (C++11

Intersection of two `std::map`s

孤人 提交于 2019-11-26 18:26:02
问题 Given that I have two std::map s, 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 way using the standard-library? 回答1: template<typename KeyType, typename LeftValue, typename RightValue> map<KeyType, pair<LeftValue, RightValue> > IntersectMaps(const map<KeyType,

Why Can't I store references in a `std::map` in C++?

好久不见. 提交于 2019-11-26 18:13:27
问题 I understand 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 thinking of references as syntactic suger over pointers, just not sure how to :/ 回答1: They way I understand it, references are implemented as pointers under the hood. The