This question already has answers here:
Standard library sort and user defined types
(3 answers)
Closed 8 years ago.
I have a two dimensional vector A
vector < vector <int> > A;
the inner vector is of constant length 3. I want to sort A according to the values of 2nd element in the inner vector. Like we can sort a database table according to any of its column.
How do I do that?
std::sort(std::begin(m), std::end(m),
[](std::vector<int> const &lhs,
std::vector<int> const &rhs) { return lhs[1] < rhs[1]; });
Be careful though as this is not a stable sort and will not preserve the order of lines that have the same second element. Use std::stable_sort if you want this. The usage is identical.
Related
This question already has answers here:
Sorting multiple vectors according to one vector [duplicate]
(3 answers)
Sorting zipped (locked) containers in C++ using boost or the STL
(5 answers)
Closed 1 year ago.
In my program I have a collection of some "entries" representing some files. It looks like this:
class Entries
{
public:
//...
private:
std::vector<std::filesystem::path> paths_;
std::vector<std::string> mimeTypes_;
std::vector<std::size_t> sizes_
std::vector<std::filesystem::file_time_type> lastWrites_;
//...
};
Let's say I want to sort my entries by one of the properties(path, type, size or modification time). Is there any way I can "link" all the vectors together so that when sorting one of them the elements of the others will be sorted in exactly the same way?
This question already has answers here:
C++11 range based loop: How does it really work
(3 answers)
C++ ranged based for loop
(2 answers)
'colon' and 'auto' in for loop c++? need some help understanding the syntax
(3 answers)
Closed 6 months ago.
v4 is a vector, the loop is to iterate through the vector list and display the elements in the vector. what does the expression inside the for loop means.
the code is as follows :
This is a range-based for loop. It iterates over all the elements of the v4 vector, and lets you process via the item variable in each iteration.
It iterates through the container v4, whatever that is, let's say it's a vector
The & in "int&" means that it's a reference, if you change the item, it's gonna change the value in the as well.
Similar to
for (int i = 0; i < v4.size(); i++){
item = v4[i];
//...
}
This question already has answers here:
Does std::sort change the relative order of equal elements?
(5 answers)
Closed 4 years ago.
I'd like to reverse-sort a vector of strings by size, with the constraint that if there are 2 strings of equal length, I'd like them to retain their original order. For eg: sorting the following set of strings :-
aab
aac
aacghgh
aabghgh
should yield :-
aacghgh
aabghgh
aab
aac
Currently I'm doing the sorting as follows :-
struct comp_functor {
bool operator()(const string& s1, const string& s2) {
return s1.size() > s2.size();
}
};
struct comp_functor c;
vector<string> vecs;
sort(vecs.begin(), vecs.end(), c);
Is there any way to specify in the overloaded method that I want to retain the original ordering if they have the same length? If not, what would be the best way to use STL libraries to solve this problem?
I believe you are looking for std::stable_sort.
It preserves the order of elements which are considered equal.
If you don't want equivalent elements to change order, then don't use std::sort. Use std::stable_sort instead - it exists exactly because it has the property of not reordering equivalent elements.
This question already has answers here:
Sorting a vector of custom objects
(14 answers)
Closed 7 years ago.
I have a vector of vectors and i want to sort it by size of each vector !
how could I do that !
I want to use sort function but I can't find any parameter in sort function that determines I want to sort it with size of each content
You just need to specify a predicate:
vector<vector<int>> vecs;
vecs.push_back(vector<int>(4));
vecs.push_back(vector<int>(2));
vecs.push_back(vector<int>(1));
vecs.push_back(vector<int>(3));
std::sort(vecs.begin(), vecs.end(), [](const vector<int> & a, const vector<int> & b){ return a.size() < b.size(); });
This code sorts the vectors by smallest to largest.
This question already has answers here:
Sorting a vector<Struct> alphabetically [duplicate]
(4 answers)
Closed 9 years ago.
I have a vector container holding some objects. The objects have various attributes, primarily ints and strings. I want an STL algorithm for sorting the container by its different attributes.
For example, if a collection of baseball cards has a player name which is a string, and a year the player started baseball, which is an integer, how can I sort the vector container by year number and then later sort it alphabetically by player name?
I never really learned STL because my professors forbade its use in the past, so I'm trying to learn it now so I can program more quickly.
The std::sort() function uses a binary predicate as third argument which can be used to customize the sort order. You can just use two different predicates:
std::sort(v.begin() v.end(),
[](card const& c0, card const& c1){
return c0.name() < c1.name();
});
... and likewise for other attributes.
All you need to do is
#include <algorithm>
bool operator<(const MyObject& x, const MyObject& y)
{
...
}
std::sort(vec.begin(), vec.end());
Use operator< to define the order you want, it returns true if x should be before y after sorting and false otherwise. From your description is sounds like you want to compare years first, and if they are equal then compare names.
Shame about your professors.