stdmap

Unusual std::map runtime error

牧云@^-^@ 提交于 2019-12-01 18:19:50
I'm hacking together an editor for a game I'm working on and as part of that editor, I need to have textures, obviously. I've created a std::map variable as so, std::map<std::string, unsigned int> textures; In my image loading code, I have the following snippet. unsigned int id; glGenTextures(1, &id); glBindTexture(GL_TEXTURE_2D, id); glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR); glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR); glTexImage2D(GL_TEXTURE_2D, 0, GL_RGB, imageWidth, imageHeight, 0, GL_RGB, GL_UNSIGNED_BYTE, imageData); glBindTexture(GL_TEXTURE

Unusual std::map runtime error

一世执手 提交于 2019-12-01 17:27:38
问题 I'm hacking together an editor for a game I'm working on and as part of that editor, I need to have textures, obviously. I've created a std::map variable as so, std::map<std::string, unsigned int> textures; In my image loading code, I have the following snippet. unsigned int id; glGenTextures(1, &id); glBindTexture(GL_TEXTURE_2D, id); glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR); glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR); glTexImage2D(GL_TEXTURE_2D,

efficient way to get key from std::map value

一笑奈何 提交于 2019-12-01 17:27:17
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 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. Happily, you only have the one-to-one requirement. One of the maps is the one you have now, the other will

Copy std::map into std::vector of pairs

你说的曾经没有我的故事 提交于 2019-12-01 15:46:07
I'm trying to copy a map into a vector of pair, so I can then sort the vector by the second data member of the pairs. I have resolved this doing like this: void mappedWordsListSorter(){ for (auto itr = mappedWordsList.begin(); itr != mappedWordsList.end(); ++itr){ vectorWordsList.push_back(*itr); } sort(vectorWordsList.begin(), vectorWordsList.end(), [=](pair<string, int>& a, pair<string, int>& b){return a.second > b.second;}); } I need to find a way to do this without using a raw loop, using the standard library instead. I have come across a lot of examples doing this by only transferring

C++ Dynamically assign std::map comparator

喜你入骨 提交于 2019-12-01 11:44:53
So I have two classes containing std::map members with effectively identical functionality except that the ordering of one map is std::less and the other std::greater. If I create an abstract parent class and declare a single map member, is there any way to dynamically assign the comparator for this member in the derived class constructors? That way the functionality can obviously all reside in the parent class. You can't change the comparator after the fact. But you can use the same comparator class and get either "greater" or "less" at the time of construction. You just need a stateful

Can you create a std::map of inherited classes?

岁酱吖の 提交于 2019-12-01 08:21:29
I'm wondering if it's possible to create a map of pointers of inherited classes. Here's an example of what I'm trying to do: #include <string> #include <map> using namespace std; class BaseClass { string s; }; class Derived1 : public BaseClass { int i; }; class Derived2 : public Derived1 { float f; }; // Here's what I was trying, but isn't working template<class myClass> map<string, myClass>m; int main() { // Add BaseClasses, Derived1's, and/or Derived2's to m here return 0; } The errors I get are: main.cpp(23): error C2133: 'm' : unknown size main.cpp(23): error C2998: 'std::map<std::string

Can you create a std::map of inherited classes?

不想你离开。 提交于 2019-12-01 07:19:50
问题 I'm wondering if it's possible to create a map of pointers of inherited classes. Here's an example of what I'm trying to do: #include <string> #include <map> using namespace std; class BaseClass { string s; }; class Derived1 : public BaseClass { int i; }; class Derived2 : public Derived1 { float f; }; // Here's what I was trying, but isn't working template<class myClass> map<string, myClass>m; int main() { // Add BaseClasses, Derived1's, and/or Derived2's to m here return 0; } The errors I

iterator vs reverse_iterator

纵饮孤独 提交于 2019-12-01 06:06:55
I'm using std::map to store a lot of elements (pairs of elements) and I have a "little" doubt. What is more efficient to iterate all elements over my std::map , iterator or reverse_iterator ? Does it really matter? these are the types of the micro optimizations you must try to avoid IMHO. Also, even if the iteration time changes for very large number of elements in the map, the fact that you are trying to iterate through all the elements of such a big map means that most probably you have chosen a wrong data structure. vladr For the record, dereferencing reverse_iterator on std::map and std:

Can I access a C++11 std::map entry while inserting/erasing from another thread?

有些话、适合烂在心里 提交于 2019-12-01 05:50:23
Can I access (without locking) an std::map entry while another thread inserts/erases entrys? example pseudo C++: typedef struct { int value; int stuff; }some_type_t; std::map<char,some_type_t*> my_map; //thread 1 does: my_map.at('a')->value = 1; //thread 2 does: some_type_t* stuff = my_map.at('b'); //thread 3 does: my_map.erase('c'); //I'm not modifying any elements T is a pointer to an previously allocated "some_type_t" std C++11 says that all members should be thread safe (erase() is not const). No no no no no! map::erase modifies the links between the map entries and that messes with the

std::map change key_comp after initialization

怎甘沉沦 提交于 2019-12-01 05:12:24
It is possible to change the comparison method of a std::map after it has been created and initialized? Or maybe only after it has been created?? I want to alter somehow the behavior of a class that contains a map that I cannot change the definition. I want to change it's comparison behavior maybe by passing another map. Maybe it is possible, this is untested: Define your own custom comparator, which internally has a pointer to the real implementation of the comparison function Pass an instance of this to the constructor of the map (you have to type the map using this comparator too.) Set the