Sorting a vector of custom objects

A simple example using std::sort struct MyStruct { int key; std::string stringValue; MyStruct(int k, const std::string& s) : key(k), stringValue(s) {} }; struct less_than_key { inline bool operator() (const MyStruct& struct1, const MyStruct& struct2) { return (struct1.key < struct2.key); } }; std::vector < MyStruct > vec; vec.push_back(MyStruct(4, “test”)); vec.push_back(MyStruct(3, “a”)); vec.push_back(MyStruct(2, “is”)); vec.push_back(MyStruct(1, “this”)); std::sort(vec.begin(), … Read more

c++ push reference node to stl list

how does one push_back into list nodes by reference? so when I need to set fields in object both structors will be updated You cannot, unless your list is templated to contain pointers (or even better smart pointers). None of the standard library containers are able to contain references directly.

stl::map assert [closed]

The following method definition ListofName GetList(){ &m_List;} is simply wrong ! Replace it with ListofName& GetList(){ return m_List;} And use it as a reference to an object and not a pointer to an object, meaning use “.” instead of “->” for( ListofName:: iterator itrList = GetList().begin(); itrList != GetList().end(); itrList++) I suggest you read a … Read more