无法使用带有 const 引用的 lambda 对 std::vector 进行排序
Cannot sort std::vector using lambda with const reference
我想根据自定义数据类型对向量进行排序。我遵循了 Sorting a vector of custom objects
回答。我正在使用 lambda 函数来比较对象。但是,我在排序时遇到如下编译器错误:
/usr/include/c++/7/bits/stl_algo.h:1852: error: cannot bind non-const lvalue reference of type 'MyData&' to an rvalue of type 'std::remove_reference::type {aka MyData}'
*__first = _GLIBCXX_MOVE(__val);
^
main.cpp
#include "mydata.h"
#include <vector>
int main()
{
std::vector<MyData> tv {MyData(2,21), MyData(3,20), MyData(10,100), MyData(9,20)};
std::sort(tv.begin(), tv.end(), []( MyData const& lhs, MyData const& rhs ){
return lhs.get_size() < rhs.get_size();
});
return 0;
}
mydata.cpp
#ifndef MYDATA_H
#define MYDATA_H
#include <iostream>
#include <algorithm>
class MyData
{
private:
int *m_data;
int m_x;
size_t m_size;
public:
MyData(const size_t &size,int const &x):
m_data(new int[size]),
m_x(x),
m_size(size)
{
std::fill_n(m_data,m_size, m_x);
std::cout << *m_data << " ctor" << m_size << std::endl;
}
MyData(const MyData& other):
m_data(new int[other.m_size]),
m_x(other.m_x),
m_size(other.m_size)
{
std::fill_n(m_data,m_size, m_x);
std::cout << *m_data << " cctor" << m_size << std::endl;
}
MyData& operator=(MyData& other)
{
std::cout << *m_data << " cbctor" << m_size << std::endl;
swap(*this,other);
std::cout << *m_data << " cactor" << m_size << std::endl;
return *this;
}
~MyData(){
std::cout << *m_data << " dtor" << m_size << std::endl;
delete[] m_data;
}
size_t get_size() const{
return m_size;
}
friend void swap(MyData& first, MyData& second){ // (1)
std::swap(first.m_size, second.m_size);
std::swap(first.m_x, second.m_x);
std::swap(first.m_data, second.m_data);
}
friend std::ostream& operator<< (std::ostream& stream, const MyData& mydata) {
stream << *(mydata.m_data) << " " << mydata.m_size << " "<< mydata.m_x;
return stream;
}
};
#endif // MYDATA_H
我不明白错误。我没有更改引用的值,这就是我收到此错误的原因。
我也读过 this 但不明白为什么它会出现在这里。
谢谢。
您应该像这样修改您的代码:
#include <iostream>
#include <fstream>
#include <thread>
#include <atomic>
#include <algorithm>
#include <vector>
using namespace std;
class MyData
{
private:
int *m_data;
int m_x;
size_t m_size;
public:
MyData(const size_t &size, int const &x) :
m_data(new int[size]),
m_x(x),
m_size(size)
{
std::fill_n(m_data, m_size, m_x);
std::cout << *m_data << " ctor" << m_size << std::endl;
}
MyData(const MyData& other) :
m_data(new int[other.m_size]),
m_x(other.m_x),
m_size(other.m_size)
{
std::fill_n(m_data, m_size, m_x);
std::cout << *m_data << " cctor" << m_size << std::endl;
}
MyData& operator=(const MyData& other)
{
std::cout << *m_data << " cbctor" << m_size << std::endl;
//swap(*this, other);
if (this != &other)
{
this->m_data = new int[other.m_size];
for (size_t i = 0; i < other.m_size; ++i)
{
this->m_data[i] = other.m_data[i];
}
this->m_x = other.m_x;
this->m_size = other.m_size;
}
std::cout << *m_data << " cactor" << m_size << std::endl;
return *this;
}
~MyData() {
std::cout << *m_data << " dtor" << m_size << std::endl;
delete[] m_data;
}
size_t get_size() const {
return m_size;
}
friend void swap(MyData& first, MyData& second) { // (1)
std::swap(first.m_size, second.m_size);
std::swap(first.m_x, second.m_x);
std::swap(first.m_data, second.m_data);
}
friend std::ostream& operator<< (std::ostream& stream, const MyData& mydata) {
stream << *(mydata.m_data) << " " << mydata.m_size << " " << mydata.m_x;
return stream;
}
};
int main()
{
std::vector<MyData> tv{ MyData(2,21), MyData(3,20), MyData(10,100), MyData(9,20) };
std::sort(tv.begin(), tv.end(), [](MyData const& lhs, MyData const& rhs) {
return lhs.get_size() < rhs.get_size();
});
std::system("pause");
return 0;
}
可以有某种类型的声明复制赋值运算符。
当可以使用copy-and-swap idiom时,这是典型的复制赋值运算符声明:
MyData& operator=(MyData other);
这是复制赋值运算符的典型声明
不能使用复制和交换习语(不可交换类型或降级
性能):
MyData& operator=(const MyData& other);
所以要在您的实现中使用交换,您可以将复制赋值运算符声明为 MyData& operator=(MyData other);
我想根据自定义数据类型对向量进行排序。我遵循了 Sorting a vector of custom objects 回答。我正在使用 lambda 函数来比较对象。但是,我在排序时遇到如下编译器错误:
/usr/include/c++/7/bits/stl_algo.h:1852: error: cannot bind non-const lvalue reference of type 'MyData&' to an rvalue of type 'std::remove_reference::type {aka MyData}' *__first = _GLIBCXX_MOVE(__val); ^
main.cpp
#include "mydata.h"
#include <vector>
int main()
{
std::vector<MyData> tv {MyData(2,21), MyData(3,20), MyData(10,100), MyData(9,20)};
std::sort(tv.begin(), tv.end(), []( MyData const& lhs, MyData const& rhs ){
return lhs.get_size() < rhs.get_size();
});
return 0;
}
mydata.cpp
#ifndef MYDATA_H
#define MYDATA_H
#include <iostream>
#include <algorithm>
class MyData
{
private:
int *m_data;
int m_x;
size_t m_size;
public:
MyData(const size_t &size,int const &x):
m_data(new int[size]),
m_x(x),
m_size(size)
{
std::fill_n(m_data,m_size, m_x);
std::cout << *m_data << " ctor" << m_size << std::endl;
}
MyData(const MyData& other):
m_data(new int[other.m_size]),
m_x(other.m_x),
m_size(other.m_size)
{
std::fill_n(m_data,m_size, m_x);
std::cout << *m_data << " cctor" << m_size << std::endl;
}
MyData& operator=(MyData& other)
{
std::cout << *m_data << " cbctor" << m_size << std::endl;
swap(*this,other);
std::cout << *m_data << " cactor" << m_size << std::endl;
return *this;
}
~MyData(){
std::cout << *m_data << " dtor" << m_size << std::endl;
delete[] m_data;
}
size_t get_size() const{
return m_size;
}
friend void swap(MyData& first, MyData& second){ // (1)
std::swap(first.m_size, second.m_size);
std::swap(first.m_x, second.m_x);
std::swap(first.m_data, second.m_data);
}
friend std::ostream& operator<< (std::ostream& stream, const MyData& mydata) {
stream << *(mydata.m_data) << " " << mydata.m_size << " "<< mydata.m_x;
return stream;
}
};
#endif // MYDATA_H
我不明白错误。我没有更改引用的值,这就是我收到此错误的原因。 我也读过 this 但不明白为什么它会出现在这里。 谢谢。
您应该像这样修改您的代码:
#include <iostream>
#include <fstream>
#include <thread>
#include <atomic>
#include <algorithm>
#include <vector>
using namespace std;
class MyData
{
private:
int *m_data;
int m_x;
size_t m_size;
public:
MyData(const size_t &size, int const &x) :
m_data(new int[size]),
m_x(x),
m_size(size)
{
std::fill_n(m_data, m_size, m_x);
std::cout << *m_data << " ctor" << m_size << std::endl;
}
MyData(const MyData& other) :
m_data(new int[other.m_size]),
m_x(other.m_x),
m_size(other.m_size)
{
std::fill_n(m_data, m_size, m_x);
std::cout << *m_data << " cctor" << m_size << std::endl;
}
MyData& operator=(const MyData& other)
{
std::cout << *m_data << " cbctor" << m_size << std::endl;
//swap(*this, other);
if (this != &other)
{
this->m_data = new int[other.m_size];
for (size_t i = 0; i < other.m_size; ++i)
{
this->m_data[i] = other.m_data[i];
}
this->m_x = other.m_x;
this->m_size = other.m_size;
}
std::cout << *m_data << " cactor" << m_size << std::endl;
return *this;
}
~MyData() {
std::cout << *m_data << " dtor" << m_size << std::endl;
delete[] m_data;
}
size_t get_size() const {
return m_size;
}
friend void swap(MyData& first, MyData& second) { // (1)
std::swap(first.m_size, second.m_size);
std::swap(first.m_x, second.m_x);
std::swap(first.m_data, second.m_data);
}
friend std::ostream& operator<< (std::ostream& stream, const MyData& mydata) {
stream << *(mydata.m_data) << " " << mydata.m_size << " " << mydata.m_x;
return stream;
}
};
int main()
{
std::vector<MyData> tv{ MyData(2,21), MyData(3,20), MyData(10,100), MyData(9,20) };
std::sort(tv.begin(), tv.end(), [](MyData const& lhs, MyData const& rhs) {
return lhs.get_size() < rhs.get_size();
});
std::system("pause");
return 0;
}
可以有某种类型的声明复制赋值运算符。
当可以使用copy-and-swap idiom时,这是典型的复制赋值运算符声明:
MyData& operator=(MyData other);
这是复制赋值运算符的典型声明 不能使用复制和交换习语(不可交换类型或降级 性能):
MyData& operator=(const MyData& other);
所以要在您的实现中使用交换,您可以将复制赋值运算符声明为 MyData& operator=(MyData other);