在c++中排序向量
Sorting vectors in c++
我需要首先按sbp.second.second向量对数据结构vector<pair<unsigned, pair<vector<unsigned>, vector<unsigned> > > > sbp
进行排序,并按sbp.second.second的相等值对sbp.second.first进行排序——这两个向量都通过(I)向量的大小进行比较;(ii)如果向量的大小相等,则向量按字典顺序排序。为此,我编写了以下代码。但我不知道为什么,但这段代码陷入了无限循环。有没有人能告诉我我哪里做错了?
#include <vector>
#include <iostream>
#include <algorithm>
using namespace std;
typedef std::pair<std::vector<unsigned>, std::vector<unsigned> > vec_pair;
bool sortingFunc(const pair<unsigned,vec_pair>& a, const pair<unsigned,vec_pair>& b)
{
if((a.second).second.size() == (b.second).second.size()) {
if(std::lexicographical_compare((a.second).second.begin(), (a.second).second.end(), (b.second).second.begin(), (b.second).second.end()))//a<b
{
return true;
}else{
if((a.second).first.size() == (b.second).first.size()) {
return std::lexicographical_compare((a.second).first.begin(), (a.second).first.end(), (b.second).first.begin(), (b.second).first.end());
} else {
// Sort by size.
return (a.second).first.size() < (b.second).first.size();
}
}
} else {
// Sort by size.
return (a.second).second.size() < (b.second).second.size();
}
}
int main()
{
vector<pair<unsigned, pair<vector<unsigned>, vector<unsigned> > > > sbp;
std::sort(sbp.begin(), sbp.end(), sortingFunc);
}
我使用的是c++ 11 (gcc 4.8.2)
我将使用std::tie
或make_tuple
与右值:
bool sortingFunc(const pair<unsigned, vec_pair>& a, const pair<unsigned, vec_pair>& b)
{
return std::make_tuple(a.second.second.size(), std::ref(a.second.second), a.second.first.size(), std::ref(a.second.first))
< std::make_tuple(b.second.second.size(), std::ref(b.second.second), b.second.first.size(), std::ref(b.second.first));
}
if(std::lexicographical_compare((a.second).second.begin(), (a.second).second.end(), (b.second).second.begin(), (b.second).second.end()))//a<b
{
return true;
}
没有b < a
条件。
else if(std::lexicographical_compare((b.second).second.begin(), (b.second).second.end(), (a.second).second.begin(), (a.second).second.end()))//b < a
{
return true;
}
代码的问题在于:
if(std::lexicographical_compare((a.second).second.begin(), (a.second).second.end(), (b.second).second.begin(), (b.second).second.end()))//a<b
您没有正确处理相反的条件。也就是说,上面这行只测试if (a.second.second < b.second.second)
。如果它为真,则从函数返回真,这是正确的。但如果它为假,则继续检查较低优先级条件,忽略b.second.second
可能小于a.second.second
的可能情况。
同时,对Jarod42的std::tie
方法做了一个小小的修改:
bool sortingFunc(const pair<unsigned, vec_pair>& a, const pair<unsigned, vec_pair>& b)
{
auto a1 = a.second.second.size();
auto& a2 = a.second.second;
auto a3 = a.second.first.size();
auto& a4 = a.second.first;
auto b1 = b.second.second.size();
auto& b2 = b.second.second;
auto b3 = b.second.first.size();
auto& b4 = b.second.first;
return std::tie(a1, a2, a3, a4) < std::tie(b1, b2, b3, b4);
}
std::tie
所做的是创建一个std::tuple
引用到它的参数。operator<
被重载,以便std::tuple
对其元素从第一个到最后一个进行字典顺序比较。
相关文章:
- 将结构向量排序为子组
- C++数组与向量排序(在我的情况下,向量比数组慢~2.5倍(无优化))
- 如何在对向量排序后更改索引值?c++
- C++向量排序给出0作为输出
- 将许多向量排序在一起
- 根据一个向量对多个向量排序
- 编译错误向量排序和联合
- C++我自己的函数进行向量排序
- 将字符串的向量排序为日期"yyyymmdd"
- C++ 通过使用旧向量进行预排序来改进向量排序
- C 向量排序 .h .cpp中的单独文件
- 向量排序-c++
- 将向量排序到一个无序映射c++11中
- 向量的向量排序
- c++向量排序方法编译失败,返回预期表达式
- 如何对bitset向量排序
- 向量排序的基础上只有先
- 我如何以相同的方式对两个向量排序,而条件只使用其中一个向量
- 列表排序和结构体向量排序之间的性能差距.c++
- 如何基于第二个字符串对字符串向量的向量排序