vector<pair<int, int>> reconstructQueue(vector<pair<int, int>>& people) {
auto comp = [](const pair<int, int>& p1, const pair<int, int>& p2)
{ return p1.first > p2.first  (p1.first == p2.first && p1.second < p2.second); };
sort(people.begin(), people.end(), comp);
vector<pair<int, int>> res;
for (auto& p : people)
res.insert(res.begin() + p.second, p);
return res;
}
6 lines Concise C++



@guanmy97
This is one of C++ attributes.
Function can be variable, so it can be used when it is required.
You can just google the lambda function to learn more about it.


@guanmy97 It is c++11 features>lambda expression.You can find the explanation on a c++ syntax book.


@zyoppy008 said in 6 lines Concise C++:
vector<pair<int, int>> reconstructQueue(vector<pair<int, int>>& people) { auto comp = [](const pair<int, int>& p1, const pair<int, int>& p2) { return p1.first > p2.first  (p1.first == p2.first && p1.second < p2.second); }; sort(people.begin(), people.end(), comp); vector<pair<int, int>> res; for (auto& p : people) res.insert(res.begin() + p.second, p); return res; }
Would return of 'res' the way it's being done in the code make a copy of 'res' in the caller scope, or does it need to be returned using reference (because 'res' is a local variable within the function "reconstructQueue" and will be lost (not have valid value) once the call to reconstructQueue is finished)?

@edaengineer caller is getting a copy:
vector<pair<int, int>> res = caller.reconstructQueue(people);
copy constructor of
vector& operator=( const vector& other ); will be called
