stdmap

Cant insert to std::map (G++)

為{幸葍}努か 提交于 2019-12-18 09:06:26
问题 I have a following trouble: struct ServerPP { std::string name; int id; int expires; }; std::map<std::string, std::set<ServerPP>> RemindTable; int test(std::string email, ServerPP serv) { RemindTable[email].insert(serv); // error when compile in this row below } Error in g++: In file included from /usr/include/c++/4.4/string:50, from /usr/include/c++/4.4/bits/locale_classes.h:42, from /usr/include/c++/4.4/bits/ios_base.h:43, from /usr/include/c++/4.4/ios:43, from /usr/include/c++/4.4/istream

Cant insert to std::map (G++)

╄→尐↘猪︶ㄣ 提交于 2019-12-18 09:06:12
问题 I have a following trouble: struct ServerPP { std::string name; int id; int expires; }; std::map<std::string, std::set<ServerPP>> RemindTable; int test(std::string email, ServerPP serv) { RemindTable[email].insert(serv); // error when compile in this row below } Error in g++: In file included from /usr/include/c++/4.4/string:50, from /usr/include/c++/4.4/bits/locale_classes.h:42, from /usr/include/c++/4.4/bits/ios_base.h:43, from /usr/include/c++/4.4/ios:43, from /usr/include/c++/4.4/istream

++it or it++ when iterating over a map?

蹲街弑〆低调 提交于 2019-12-18 06:08:51
问题 Examples showing how to iterate over a std::map are often like that: MapType::const_iterator end = data.end(); for (MapType::const_iterator it = data.begin(); it != end; ++it) i.e. it uses ++it instead of it++ . Is there any reason why? Could there be any problem if I use it++ instead? 回答1: Putting it to the test, I made three source files: #include <map> struct Foo { int a; double b; char c; }; typedef std::map<int, Foo> FMap; ### File 1 only ### void Set(FMap & m, const Foo & f) { for (FMap

Is is possible to use std::map in C++ with a class without any copy operator?

梦想的初衷 提交于 2019-12-18 04:41:46
问题 I'm using a Class (Object) that doesn't have any copy operator : it basically cannot be copied right now. I have a std::map<int,Object> objects variable that lists objects with an int identifier. How could I add an Object to this map without having to use copy operators? I tried objects.insert(std::pair<0,Object()>); but that won't compile. I would just like to create my object initially inside the map using the default constructor, but writing objects[0]; fails... Thanks :) 回答1: In C++03,

Is is possible to use std::map in C++ with a class without any copy operator?

ⅰ亾dé卋堺 提交于 2019-12-18 04:41:23
问题 I'm using a Class (Object) that doesn't have any copy operator : it basically cannot be copied right now. I have a std::map<int,Object> objects variable that lists objects with an int identifier. How could I add an Object to this map without having to use copy operators? I tried objects.insert(std::pair<0,Object()>); but that won't compile. I would just like to create my object initially inside the map using the default constructor, but writing objects[0]; fails... Thanks :) 回答1: In C++03,

How to modify key values in std::map container

我与影子孤独终老i 提交于 2019-12-18 04:16:33
问题 Given std::map<int,std::string> myMap; fillMyMapWithStuff(myMap); // modify key values - I need to add a constant value to each key for (std::map<int,std::string>::iterator mi=myMap.begin(); mi != myMap.end(); ++mi) { // ... } Whats a good way apply some re-indexing? Must I remove the old entry and add a new one with the new key and old value? 回答1: Looks like you are better off building a new map and swapping it afterward. You'll have only n insert operations instead of n deletions and n

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

妖精的绣舞 提交于 2019-12-17 23:39:40
问题 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> type; }; class Row { private: std::map<unsigned long,Field*> column; }; Well, I suppose in principle the Row class shouldn't have to know which kind of Field (or Column ) we'd like to use, i.e. whether it's a Field

Is it impossible to use an STL map together with a struct as key?

时间秒杀一切 提交于 2019-12-17 23:23:35
问题 I have the following code: struct Node { int a; int b; }; Node node; node.a = 2; node.b = 3; map<int, int> aa; aa[1]=1; // OK. map<Node, int> bb; bb[node]=1; // Compile error. When I tried to map an instance of my struct Node to an int , I got a compile error. Why? 回答1: For a thing to be usable as a key in a map, you have to be able to compare it using operator<() . You need to add such an operator to your node class: struct Node { int a; int b; bool operator<( const Node & n ) const { return

How can I find the minimum value in a map?

假如想象 提交于 2019-12-17 16:16:03
问题 I have a map and I want to find the minimum value (right-hand side) in the map. Here is how I did it: bool compare(std::pair<std::string ,int> i, pair<std::string, int> j) { return i.second < j.second; } //////////////////////////////////////////////////// std::map<std::string, int> mymap; mymap["key1"] = 50; mymap["key2"] = 20; mymap["key3"] = 100; std::pair<char, int> min = *min_element(mymap.begin(), mymap.end(), compare); std::cout << "min " << min.second<< " " << std::endl; The code

Braced initialisation of std::map member compiler error

爷,独闯天下 提交于 2019-12-14 02:32:32
问题 The following code does not compile using Visual Studio 2013. It does compile using Xcode 6.1 (Clang 3.5). std::string s1("one"); std::string s2("two"); std::string s3("three"); std::string s4("four"); class X { typedef std::map<std::string, std::string> MyMapType; MyMapType map1 = { { s1, s2 }, { s3, s4 } }; MyMapType map2 = { { std::make_pair(s1, s2) }, { std::make_pair(s3, s4) } }; }; The error reported for both declarations is: error C2664: 'std::map<std::string,std::string,std::less<_Kty