我想根据自定义数据类型对向量进行排序。我跟着Sorting a vector of custom objects 回答。我正在使用lambda函数来比较对象。但是我在排序时遇到编译错误:
/ usr / include / c ++ / 7 / bits / stl_algo.h:1852:错误:无法绑定类型' MyData&'的非常量左值引用。到类型' std :: remove_reference :: type {aka MyData}'的rvalue * __ 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,但不明白为什么会在这里发生。 谢谢。
答案 0 :(得分:2)
可以有某种类型的声明复制赋值运算符。
当可以使用复制和交换习惯用法时,这是复制赋值运算符的典型声明:
MyData& operator=(MyData other);
这是典型的复制赋值运算符声明 不能使用复制和交换习惯用法(不可交换类型或降级 性能):
MyData& operator=(const MyData& other);
因此,要在实现中使用swap,可以将复制赋值运算符声明为MyData& operator=(MyData other);
答案 1 :(得分:0)
你应该修改你的代码:
#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;
}