C++ 排序向量内的值时,使用排序方法生成错误

C++ 排序向量内的值时,使用排序方法生成错误,c++,sorting,vector,stl,g++,C++,Sorting,Vector,Stl,G++,当我们在debian机器上使用g++编译下面的代码时,会产生以下错误…有人能告诉我为什么会出现错误吗?我试图通过注释排序行,然后出现错误,但是我们的任务需要排序,那么可能的解决方案是什么 代码: #包括 #包括 #包括 使用名称空间std; //这里是一个简单的结构 结构MyStruct { int-Num; //定义运算符< bool运算符;_Size=int]'/usr/include/c++/4.7/bits/stl_algo.h:5476:4:从“void std::sort(_RAIt

当我们在debian机器上使用g++编译下面的代码时,会产生以下错误…有人能告诉我为什么会出现错误吗?我试图通过注释排序行,然后出现错误,但是我们的任务需要排序,那么可能的解决方案是什么

代码:

#包括
#包括
#包括
使用名称空间std;
//这里是一个简单的结构
结构MyStruct
{
int-Num;
//定义运算符<
bool运算符;_Size=int]'/usr/include/c++/4.7/bits/stl_algo.h:5476:4:从“void std::sort(_RAIter,_RAIter)[带_RAIter]中需要=
__gnu_cxx::_normal_iterator>]'testvect.cpp:33:41:此处为必填项
/usr/include/c++/4.7/bits/stl_algo.h:2271:4:错误:传递常数
MyStruct'作为'bool MyStruct::operator'的'this'参数更正了代码:

#include <iostream>
#include <vector>
#include <algorithm>

using namespace std;

// Here is a simple struct
struct MyStruct
{
   int Num;
   // Define the operator <
   bool operator <(const MyStruct& Rhs)const
   {
      return (Num < Rhs.Num);
   }
};

int main()
{
   vector<MyStruct> MyVector;
   // Let the size be 5.
   MyVector.resize(5);
   // Push 5 instances of MyStruct with Num ranging
   // from 5 to 1
   MyStruct TestStruct;
   int i = 0;
   for (i = 0; i < 5; ++i)
   {
      TestStruct.Num = 5 - i;
      MyVector[i] = TestStruct;
   }
   // Now sort the vector
   sort(MyVector.begin(), MyVector.end());
   // Try to display Num for each element. It is sorted
   for (i = 0; i < 5; ++i)
   {
      cout << MyVector[i].Num << '\n';
   }
   return 0;
#包括
#包括
#包括
使用名称空间std;
//这里是一个简单的结构
结构MyStruct
{
int-Num;
//定义运算符<

布尔运算符在stl使用常量和参数的情况下,您使用的是相当陈旧的编译器,在更现代的版本中,这些都是通过右值引用传递的,不需要常量运算符也无法复制。由于比较方法不会改变它所操作的对象,所以将其标记为
const
。这应该会有所帮助。即使将其标记为const,它也会生成同样的错误…我用
-std=c++14-O2-Wall编译,并添加了@Revolver\u Ocelot的const建议,没有错误。使用c++14编译器…thnx…你能分享一下通过右值引用传递的概念吗?如何实现吗?@farhantel这是一个大主题,你应该阅读它,即:,在你的代码中,我看不到它是任何用途(我指的是以右值引用作为参数的构造函数/复制运算符)。另外@marcin.,请建议进一步阅读stl关联和顺序容器…thnx和regards右值参数与运算符为
const
有什么关系?我想这更多地与stl实现的差异有关,我可以用VS2005编译原始代码(我看到它使用非常量迭代器进行比较)但是gcc4.8.2失败了。从第2271行我可以看到,pivot是常量&在进行比较时需要常量运算符
#include <iostream>
#include <vector>
#include <algorithm>

using namespace std;

// Here is a simple struct
struct MyStruct
{
   int Num;
   // Define the operator <
   bool operator <(const MyStruct& Rhs)const
   {
      return (Num < Rhs.Num);
   }
};

int main()
{
   vector<MyStruct> MyVector;
   // Let the size be 5.
   MyVector.resize(5);
   // Push 5 instances of MyStruct with Num ranging
   // from 5 to 1
   MyStruct TestStruct;
   int i = 0;
   for (i = 0; i < 5; ++i)
   {
      TestStruct.Num = 5 - i;
      MyVector[i] = TestStruct;
   }
   // Now sort the vector
   sort(MyVector.begin(), MyVector.end());
   // Try to display Num for each element. It is sorted
   for (i = 0; i < 5; ++i)
   {
      cout << MyVector[i].Num << '\n';
   }
   return 0;
  bool operator <(const MyStruct& Rhs)
  bool operator <(const MyStruct& Rhs) const
                                       ^^^^^