【发布时间】:2012-10-25 00:25:18
【问题描述】:
我正在尝试排序
std::vector< std::vector< std::string> > perm;
我正在尝试使用算法头中的std::sort
我叫它
std::sort( perm.begin(), perm.end(), sortPerms);
这是我的排序函数:
bool sortPerms (const std::vector<std::string> &i, const std::vector<std::string> &j) {
for(unsigned int x = 0; x < i.size(); x++) {
if(i[x] != j[x])
return false;
}
//both are equal
return true;
}
排序的目的是然后调用 std::unique 来获得一个具有唯一值的向量。 当我在 cygwin 中使用 gcc 编译时,我没有收到任何错误,但我有重复,当我使用 Visual Studio 2010 进行编译时,我收到一个错误,即 operator
我不知道如何解决这个问题,有什么建议吗?
其他细节: 保证所有向量的大小相同。 它的目的是字符串原始向量的每个排列的向量。 每个字符串都是一个命令,我正在寻找可以改变这些命令的所有不同方式。所以我需要去掉重复的部分。
【问题讨论】:
-
你能指出你有错误的那一行吗?
-
你希望如何排序仅仅基于不平等的概念?
-
它是
内部的一个断言,它声明 operator -
我没有看到提供的运算符,而是 std::sort() 的回调函数。我猜在 Windows 上它使用
LEFT < RIGHT,在你的情况下LEFT是std::vector< std::string >。您可能需要在内部声明带有operator<的结构,该结构将派生自std::vector< std::string >并完成它。 -
@Grzegorz:为什么是派生
std::vector<std::string>的新类型?只要像理智的人一样免费(“全球”)operator<!bool operator<(const std::vector<std::string>& a, const std::vector<std::string>& b);
标签: c++ sorting vector comparator stl-algorithm