sorting a vector of objects c 2b 2b

Solutions on MaxInterview for sorting a vector of objects c 2b 2b by the best coders in the world

showing results for - "sorting a vector of objects c 2b 2b"
Enrico
10 Aug 2017
1#include <vector>
2#include <algorithm>
3
4using namespace std;
5
6vector< MyStruct > values;
7
8sort( values.begin( ), values.end( ), [ ]( const MyStruct& lhs, const MyStruct& rhs )
9{
10   return lhs.key < rhs.key;
11});
Emilia
18 Jan 2019
1struct MyStruct
2{
3    int key;
4    std::string stringValue;
5
6    MyStruct(int k, const std::string& s) : key(k), stringValue(s) {}
7};
8
9struct less_than_key
10{
11    inline bool operator() (const MyStruct& struct1, const MyStruct& struct2)
12    {
13        return (struct1.key < struct2.key);
14    }
15};
16
17std::vector < MyStruct > vec;
18
19vec.push_back(MyStruct(4, "test"));
20vec.push_back(MyStruct(3, "a"));
21vec.push_back(MyStruct(2, "is"));
22vec.push_back(MyStruct(1, "this"));
23
24std::sort(vec.begin(), vec.end(), less_than_key());
queries leading to this page
c 2b 2b sort vector objectssortting array of objects c 2b 2bsort vector custom comparator cppcustom sorted vector c 2b 2bhow to sort my objects c 2b 2b vector of classsort class vectorcustom sort vector c 2b 2bsort a vector of objects c 2b 2bsort array of custom objects c 2b 2bsort custom class c 2b 2bhow to sort object array in cppsort using custom comparator class c 2b 2bvector sort custom comparatorsorting of class in vectorc 2b sorting customdefining own sorting in cppc 2b 2b sort vector by object variablesort vector c 2b 2b custom how to sort a vector of objects c 2b 2bsort objects cppcomparator sort cpp customsort vector c 2b 2b classsort with custom function c 2b 2bsorting vector of objects c 2b 2b stlsorting vector of objects c 2b 2bc 2b 2b sort object vectorc 2b 2b custom vector sortc 2b 2b sort custom functionhow to create a vector of objects in c 2b 2b and sort themsorting a vector of objects c 2b 2bcustom sort comparison function c 2b 2bsort elements in a class vector c 2b 2bc 2b 2b use sort on custom objectsort vector of custom objects c 2b 2bsort with custom function cppsort c 2b 2b custom classhow to use std 3a 3asort for custom objectghow to sotrt vector of objects in c 2b 2bsort vector of obects in cppvector c 2b 2b sort objectshow to sort vector of objects in c 2b 2bsort objects in vector c 2b 2bsorting in class in c 2b 2bcustom compare function c 2b 2b for vectorhow to implement std 3a 3asort for custom objectvector custom sortcreating a custom comaprison function for sort in c 2b 2b cpp sort custom comparatorc 2b 2b order a vector of objectsorder vector c 2b 2b of objectscustom comparator struct how to sort a vector of structures in c 2b 2bsort vector of class objects c 2b 2bhow to sort vector of objects on basis of avlueclasse vector c 2b 2b sortc 2b 2b sort vector by value o fobjectsorting vector c 2b 2b with classsort vectors cusstomizable functionvector class sort c 2b 2bhow to sort a vector of custom objects in c 2b 2bcustom sort struct c 2b 2bc 2b 2b sort vector of objectssorting an vector of oncjects in cppsort is a class function c 2b 2bhow to sort the class in vector in c 2b 2bsort objects in cppsort vector objects c 2b 2bcomparator in c 2b 2b vectorhow to sort objects using particular vaue in vectorcpp sort a vector of objectssort a vector of objects in c 2b 2bcustom sorted vector class c 2b 2bobject vector c 2b 2b sortsorting vector of class c 2b 2bhow to create your own function to sort a vector in c 2b 2bsort object using operator 3e in c 2b 2bsort a vector c 2b 2b with custom classsort a set of custom objects c 2b 2bcomparator sort for vectors cppfunction to sort vector of class objects in c 2b 2bc 2b 2b sort vector with custom comparatorcpp stl sort custom class operator overloadhow to sort a vector of class objects in c 2b 2bcpp stl sort custom classsort vector of objects c 2b 2bsorting a vector of objects c 2b 2b