How can I use std::maps with user-defined types as key?

You don’t have to define operator< for your class, actually. You can also make a comparator function object class for it, and use that to specialize std::map. To extend your example: struct Class1Compare { bool operator() (const Class1& lhs, const Class1& rhs) const { return lhs.id < rhs.id; } }; std::map<Class1, int, Class1Compare> c2int; It … Read more

Copy map values to vector in STL [duplicate]

You could probably use std::transform for that purpose. I would maybe prefer Neils version though, depending on what is more readable. Example by xtofl (see comments): #include <map> #include <vector> #include <algorithm> #include <iostream> template< typename tPair > struct second_t { typename tPair::second_type operator()( const tPair& p ) const { return p.second; } }; template< … Read more

c++ deque vs queue vs stack

Moron/Aryabhatta is correct, but a little more detail may be helpful. Queue and stack are higher level containers than deque, vector, or list. By this, I mean that you can build a queue or stack out of the lower level containers. For example: std::stack<int, std::deque<int> > s; std::queue<double, std::list<double> > q; Will build a stack … Read more

What’s the difference between std::multimap and std::map

A std::map is an associative container, that allows you to have a unique key associated with your type value. For example, void someFunction() { typedef std::map<std::string, int> MapType; MapType myMap; // insertion myMap.insert(MapType::value_type(“test”, 42)); myMap.insert(MapType::value_type(“other-test”, 0)); // search auto it = myMap.find(“test”); if (it != myMap.end()) std::cout << “value for ” << it->first << ” … Read more

Hata!: SQLSTATE[HY000] [1045] Access denied for user 'divattrend_liink'@'localhost' (using password: YES)