[英]Sorting vectors in c++
我需要先按sbp.second.second向量vector<pair<unsigned, pair<vector<unsigned>, vector<unsigned> > > > sbp
數據結構vector<pair<unsigned, pair<vector<unsigned>, vector<unsigned> > > > sbp
>>>> vector<pair<unsigned, pair<vector<unsigned>, vector<unsigned> > > > sbp
進行排序,並以sbp.second等於sbp.second.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
與rvalue:
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)
。 如果為true,則從函數返回true,這是正確的。 但是,如果它為假,則繼續檢查較低優先級的條件,而忽略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
重載, std::tuple
對其元素進行字典編排比較。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.