在 Visual Studio 和 GCC 中使用 stl sort 进行排序
编写一个应该在 Linux 和 Windows 环境中可移植的程序时,我发现在使用 Visual studio 和 gcc 编译时,STL 排序函数存在问题。 为了对复杂数据结构的向量进行排序,我为该形式的结构编写了一个 int 转换运算符:
struct result
{
public :
int Gene_a;
int Gene_b;
std::vector<int> score;
float total_score;
operator int() {return total_score;}
}
在这种情况下,我在 Visual Studio 中使用整数的标准排序算法没有问题:
sort(results.rbegin(),results.rend());
但是当尝试使用 GCC 编译它时(实际上是 g++ )会导致有趣的错误。 为了避免这种情况,我似乎必须编写一个排序函数:
inline bool better (result a, result b)
{
return a.total_score > b.total_score;
}
并以以下形式调用排序:
sort(results.begin(),results.end(),better);
我使用的是标准 C++ 之外的东西还是缺少 g++ STL 实现? 是否可以让 g++ 理解 struct 向量相当于 int 向量?
这是一个简短的主要内容来说明该错误:
#include <vector>
#include <algorithm>
#include <iostream>
using namespace std;
int main(int argc, char* argv[])
{
vector<result> r; // define a vector of struct
for (int i=0;i<10;i++) // fill up with data
{
result a;
a.Gene_a=i;
a.Gene_b=2*i;
for(int j=0;j<i;j++)
a.score.push_back(i); // fill the int vector in the struct
a.total_score=i;
r.push_back(a);
}
// sort(r.rbegin(),r.rend()); // this line will fail in g++
sort(r.rbegin(),r.rend(),better);
for (int i=0;i<10;i++) // demonstrate that the int operator works
cout << (int)r[i] << endl;
}// End main
Writing a program that should be portable in Linux and Windows enviroments I have found a issue with the STL sort function, when compiling with Visual studio and gcc.
In order to sort a vector of complex data structures I have written an int conversion operator for the structures in that form:
struct result
{
public :
int Gene_a;
int Gene_b;
std::vector<int> score;
float total_score;
operator int() {return total_score;}
}
In that case I have no problem in visual studio using the standard sort algorithm for integers:
sort(results.rbegin(),results.rend());
But when trying to compile that using GCC ( actually g++ ) that lead to funny errors.
To avoid that seems that I have to write an ordering function:
inline bool better (result a, result b)
{
return a.total_score > b.total_score;
}
and invoke sort in the form:
sort(results.begin(),results.end(),better);
Was I using something out of the standard C++ or is a lack of the g++ STL implementation ?
Is it possible to let g++ understand that the vector of struct is equivalent to a vector of int ?
Here is a short main to illustrate the error:
#include <vector>
#include <algorithm>
#include <iostream>
using namespace std;
int main(int argc, char* argv[])
{
vector<result> r; // define a vector of struct
for (int i=0;i<10;i++) // fill up with data
{
result a;
a.Gene_a=i;
a.Gene_b=2*i;
for(int j=0;j<i;j++)
a.score.push_back(i); // fill the int vector in the struct
a.total_score=i;
r.push_back(a);
}
// sort(r.rbegin(),r.rend()); // this line will fail in g++
sort(r.rbegin(),r.rend(),better);
for (int i=0;i<10;i++) // demonstrate that the int operator works
cout << (int)r[i] << endl;
}// End main
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
我发现这两种比较方法之间唯一有意义的区别是,当元素为 const 时,第二种方法将起作用。第一个可能应该是:
即使您正在修改容器,您的比较仍然要求它适用于 const 对象。在您的情况下,实现使用了该假设并出现了错误。
但这应该不重要,因为要对容器及其元素进行排序需要可修改...The only meaningful difference I see between the two comparison methods is that the second will work when the element is
const
. The first should probably be:Even though you're modifying the container, your comparison still has the requirement that it work on
const
objects. In your case, the implementation used that assumption and the error arose.But this shouldn't matter, since to sort the container and its elements needs to be modifiable...