stdmap

Using a (mathematical) vector in a std::map

白昼怎懂夜的黑 提交于 2019-12-12 07:54:18
问题 Related: what can I use as std::map keys? I needed to create a mapping where specific key locations in space map to lists of objects. std::map seemed the way to do it. So I'm keying a std::map on an xyz Vector class Vector { float x,y,z } ; , and I'm making a std::map<Vector, std::vector<Object*> > . So note the key here is not a std::vector , its an object of class Vector which is just a math xyz vector of my own making. To produce a "strictly weak ordering" I've written the following

std::set doesn't detect duplicate custom objects

☆樱花仙子☆ 提交于 2019-12-12 04:13:29
问题 I have a map for keeping the team name and players of the team std:pair<std::string, std::vector<std::string> > and a set of pointers to players to keep the players sorted in descending order by wins. And there's the catch - one player can participate in more than one team. class Player { public: int wins; std::string name; Player() {} Player(std::string name) : wins(0), name(name) {} bool operator<(const Player* rhs) const { return this->wins > rhs->wins; } bool operator()(const Player* lhs,

C++ assign map different classes

烂漫一生 提交于 2019-12-11 18:37:40
问题 At the moment I having following problem: I want to assign an object of a class too a map struct My goal is: If I call the map with the ids inside the brackets, the function must be start! I know the following approach doesn't work. But I would be very nice, I someone can give me an idea or a tip how I can realize this approach... Here is an example: #include <map> #include <functional> #include <iostream> class start { public: void sayhello() { std::cout << "Hallo!!" << std::endl; } }; class

Atomically accessing resources stored in a map

落花浮王杯 提交于 2019-12-11 11:42:32
问题 I want to store some std::shared_ptr to C++ class instances in a map, e.g. a std::map, using an integer key. However, I need this map to have two properties: If the key doesn't exist, then return an error rather than create a new object. If the key does exist then grab a copy of the std::shared_ptr atomically. I.e. it should not be possible to delete an object from the map in one thread while simultaneously retrieving it from the map in another. I'd like to avoid having a single mutex (even

How to find the depth of each node in std::map?

五迷三道 提交于 2019-12-11 10:08:31
问题 If I construct, my own binary tree, then I can find the depth of each node. The sample code is as follows template<class datatype> void binary_node<datatype>::printNodeWithDepth(int currentNodeDepth) { if ( left ) left->printNodeWithDepth(currentNodeDepth+1); std::cout << value << " and the depth is " << currentNodeDepth << std::endl; if ( right) right->printNodeWithDepth(currentNodeDepth+1); } But wondering, since map is a b-tree, is it possible to write something similar to this for a std:

Iterating over a container bidirectionally

て烟熏妆下的殇ゞ 提交于 2019-12-11 00:55:17
问题 Is there a better way than the below code, to iterate over a container in either direction, using the same iterators? #include <iostream> #include <map> int main() { const bool descend = false; std::map<int, int> mapp; mapp[1] = 1; mapp[2] = 2; mapp[3] = 3; mapp[4] = 4; std::map<int, int>::iterator startIter = descend ? --(mapp.end()) : mapp.begin(); std::map<int, int>::iterator endIter = descend ? --(mapp.begin()) : mapp.end(); while (startIter != endIter) { std::cout << startIter->first <<

Can't create map of MoveConstructibles

人盡茶涼 提交于 2019-12-10 20:59:13
问题 I have a class containing a std::unique_ptr<> and I want to put instances of this class inside of an std::map<> . I thought one of the things that motivated the introduction of move semantics to C++ was the possibility of putting things like unique_ptrs inside standard containers (and that really works, in the case of vectors). But it seems to me that std::map<> doesn't like this idea. Why is this so? #include <map> #include <memory> int main() { std::map<int,std::unique_ptr<int>> map; //

Boost.Assign: using objects with map_list_of?

喜欢而已 提交于 2019-12-10 20:32:19
问题 Using C++ with boost. In Boost.Assign can I use the new operator with map_list_of? For example: std::map<int, MyObject*> objects = boost::assign::map_list_of (1, new MyObject())(2, new MyObject())(3, new MyObject()) If not, is there another way to do it? 回答1: Seems yes. This compiles fine with VS2010 & boost 1.47. #include <boost\assign.hpp> class MyObject{ public: MyObject(int i):m_i(i){} private: int m_i; }; int main (void) { std::map<int, MyObject*> objects = boost::assign::map_list_of(1,

Map with multiple keys in C++

一世执手 提交于 2019-12-10 14:51:43
问题 I want to store data by both, their name and their index. In other words, I want to map string names to objects and also give them a custom order. What I came up with first is a std::vector of pairs of the string key and the object. The order was given by the position in the vector. std::vector<std::pair<std::string, object> > But this approach seems to be suboptimal since it doesn't automatically check for the uniqueness of string names. Moreover it feels wrong to group the objects by their

C++, accessing std::map element via const reference

前提是你 提交于 2019-12-10 14:47:00
问题 I have a proble with const. say I have : class A{ friend std::ostream& operator<<(std::ostream& os,const A& myObj); private: std::map<int,int> someMap; int someInteger; }; std::ostream& operator<<(std::ostream& os,const A& myObj){ os<<myObj.someInteger<<std::endl; os<<myObj.someMap[0]<<std::endl; } This kind of code generates an error at compilation due to a const conflict with the map (if I comment the line printing the map value all is fine), and if I get rid of the 'const' in the function