在对向量中的值进行排序时,使用 sort 方法生成错误
Error is generated with the sort method while sorting the values inside a vector
当我们在 debian 机器上使用 g++ 编译以下代码时,会产生以下错误...谁能帮我解释为什么会出现错误?我尝试通过评论排序行然后错误消失但是我们的任务需要完成排序那么可能的解决方案是什么
代码:
#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)
{
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;
}
输出:
In file included from /usr/include/c++/4.7/algorithm:63:0,
from testvect.cpp:3: /usr/include/c++/4.7/bits/stl_algo.h: In instantiation of
‘_RandomAccessIterator
std::__unguarded_partition(_RandomAccessIterator,
_RandomAccessIterator, const _Tp&) [with _RandomAccessIterator = __gnu_cxx::__normal_iterator >; _Tp = MyStruct]’: /usr/include/c++/4.7/bits/stl_algo.h:2309:70: required
from ‘_RandomAccessIterator
std::__unguarded_partition_pivot(_RandomAccessIterator,
_RandomAccessIterator) [with _RandomAccessIterator = __gnu_cxx::__normal_iterator >]’ /usr/include/c++/4.7/bits/stl_algo.h:2340:54: required from ‘void
std::__introsort_loop(_RandomAccessIterator, _RandomAccessIterator,
_Size) [with _RandomAccessIterator = __gnu_cxx::__normal_iterator >; _Size = int]’ /usr/include/c++/4.7/bits/stl_algo.h:5476:4: required from ‘void std::sort(_RAIter, _RAIter) [with _RAIter =
__gnu_cxx::__normal_iterator >]’ testvect.cpp:33:41: required from here
/usr/include/c++/4.7/bits/stl_algo.h:2271:4: error: passing ‘const
MyStruct’ as ‘this’ argument of ‘bool MyStruct::operator<(const
MyStruct&)’ discards qualifiers [-fpermissive]
更正后的代码:
#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;
您使用相当过时的编译器,其中 stl 使用 const& 参数,在更现代的版本中,这些参数通过右值引用传递并且不需要 const operator<,因此修复它:
变化:
bool operator <(const MyStruct& Rhs)
至
bool operator <(const MyStruct& Rhs) const
^^^^^
或者,使用支持更现代版本的 C++ 的更高版本的编译器,然后使用“-std=c++11”或“-std=c++14”启用更现代的版本。
当我们在 debian 机器上使用 g++ 编译以下代码时,会产生以下错误...谁能帮我解释为什么会出现错误?我尝试通过评论排序行然后错误消失但是我们的任务需要完成排序那么可能的解决方案是什么
代码:
#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)
{
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;
}
输出:
In file included from /usr/include/c++/4.7/algorithm:63:0, from testvect.cpp:3: /usr/include/c++/4.7/bits/stl_algo.h: In instantiation of ‘_RandomAccessIterator std::__unguarded_partition(_RandomAccessIterator, _RandomAccessIterator, const _Tp&) [with _RandomAccessIterator = __gnu_cxx::__normal_iterator >; _Tp = MyStruct]’: /usr/include/c++/4.7/bits/stl_algo.h:2309:70: required from ‘_RandomAccessIterator std::__unguarded_partition_pivot(_RandomAccessIterator, _RandomAccessIterator) [with _RandomAccessIterator = __gnu_cxx::__normal_iterator >]’ /usr/include/c++/4.7/bits/stl_algo.h:2340:54: required from ‘void std::__introsort_loop(_RandomAccessIterator, _RandomAccessIterator, _Size) [with _RandomAccessIterator = __gnu_cxx::__normal_iterator >; _Size = int]’ /usr/include/c++/4.7/bits/stl_algo.h:5476:4: required from ‘void std::sort(_RAIter, _RAIter) [with _RAIter = __gnu_cxx::__normal_iterator >]’ testvect.cpp:33:41: required from here /usr/include/c++/4.7/bits/stl_algo.h:2271:4: error: passing ‘const MyStruct’ as ‘this’ argument of ‘bool MyStruct::operator<(const MyStruct&)’ discards qualifiers [-fpermissive]
更正后的代码:
#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;
您使用相当过时的编译器,其中 stl 使用 const& 参数,在更现代的版本中,这些参数通过右值引用传递并且不需要 const operator<,因此修复它:
变化:
bool operator <(const MyStruct& Rhs)
至
bool operator <(const MyStruct& Rhs) const
^^^^^
或者,使用支持更现代版本的 C++ 的更高版本的编译器,然后使用“-std=c++11”或“-std=c++14”启用更现代的版本。