I would like to sort a vector holding objects of a class with a const
member variable.
Unfortunately, I get an error stating that there is "no matching function for call to "swap"".
When I remove the const
keyword for id
, then std::sort()
works with both the overloaded operator<()
and the custom compare function.
Why is this the case? Can I in general not sort objects belonging to a class with a const member variable?
#include <iostream>
#include <vector>
#include <algorithm>
using namespace std;
struct A
{
const int id;
A(int id) : id(id) {}
bool operator<(const A &other) const
{
return id < other.id;
}
};
bool cmp(const A &lhs, const A &rhs)
{
return lhs.id < rhs.id;
}
int main()
{
vector<A> vec;
vec.emplace_back(3);
vec.emplace_back(0);
vec.emplace_back(2);
vec.emplace_back(1);
std::sort(vec.begin(), vec.end());
std::sort(vec.begin(), vec.end(), cmp);
}
Yes, you can sort a const vector in C++. Let a const vector is v. const vector<int> v={5,4,3,2,1}; If you want to sort this vector using sort(v.
You can sort a vector of custom objects using the C++ STL function std::sort. The sort function has an overloaded form that takes as arguments first, last, comparator. The first and last are iterators to first and last elements of the container.
A vector in C++ can be easily sorted in ascending order using the sort() function defined in the algorithm header file. The sort() function sorts a given data structure and does not return anything. The sorting takes place between the two passed iterators or positions.
The vector can use the array notation to access the elements. If you don't want to use the default std::sort , or std::sort with custom comparator, you can use qsort or write your own.
Well, the way that sort is going to be implemented is that it will swap objects as needed. A class with a const member and no copy or move assignment operators is not going to be "swappable".
I would probably make the id
member private and non-const. Then I'd have it accessed via a getter (but not providing a setter). Making it logically const, but the class would be copyable.
Something like this:
class A {
public:
A(int id) : id_(id) {}
bool operator<(const A &other) const {
return id_ < other.id_;
}
int id() const { return id_; }
private:
int id_;
};
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With