[英]sorting large vectors using user defined function in c/c++
我根據我的排序標准編寫了以下代碼來排序兩個向量:
typedef pair<unsigned, pair<vector<unsigned>, vector<unsigned> > > Elem;
bool bucketComparator(const Elem& a, const Elem& b) {
//find the min and max of "a" and "b"
// return true if a should go before b in the sort
unsigned minA,maxA;
unsigned minB,maxB;
if((a.second.first).size()<=1){
minA=maxA=*((a.second.first).begin());
} else{
minA=*std::min_element((a.second.first).begin(),(a.second.first).end());
maxA=*std::max_element((a.second.first).begin(),(a.second.first).end());
}
if((b.second.first).size()<=1){
minB=maxB=*((b.second.first).begin());
} else{
minB=*std::min_element((b.second.first).begin(),(b.second.first).end());
maxB=*std::max_element((b.second.first).begin(),(b.second.first).end());
}
if((minA<=minB)&&(maxA<=maxB)){
return true;
} else{
return false;
}
}
main()
{
vector<Elem> A;
//initializing vector A with values
std::sort(A.begin(), A.end(), bucketComparator);
//further computation using vector A
}
錯誤:大數據的分段錯誤。
當矢量A的大小為223080或更大時,我發現我正在獲得分段錯誤。 但是當向量A的大小小於100時,代碼運行良好。 由於我在64GB RAM上運行代碼,因此無法理解其原因。 有人可以幫我這一點。
此外,當我在linux上運行top命令時,我發現由於分段錯誤,程序在停止之前甚至不會消耗0.1%(64GB)可用RAM。
我甚至試圖通過首先使用std :: sort和冒泡排序對矢量進行排序來找到max和min - 但我仍然得到相同的錯誤。
我正在運行以下版本的gcc:gcc(Ubuntu / Linaro 4.6.3-1ubuntu5)4.6.3
有沒有什么方法可以編寫我的程序,以便根據我使用的排序標准對大型向量進行排序:bucketComparator。 我對c和c ++都很好。
另外,當我做一個簡單的std :: sort時,代碼不會給出分段錯誤:
std::sort(A.begin(), A.end());
我唯一能看到的可能是錯誤的是你要取消引用std::min_element
和std::max_element
返回的std::max_element
,而不檢查它們返回的是<vector>.end()
,這是可能的。 當一個指針在某個地方被解除引用而不應該被解除引用時,幾乎總會發生Seg錯誤。
例如,如果向量為空,則std::min_element
和std::max_element
將返回<vector>.end()
,您將取消引用它。
如果向量a.second.first
或b.second.first
為空,則程序將在取消引用從begin()
調用獲取的迭代器時崩潰。
bool bucketComparator(const Elem& a, const Elem& b) {
//find the min and max of "a" and "b"
// return true if a should go before b in the sort
unsigned minA,maxA;
unsigned minB,maxB;
const vector<unsigned> &vecA = a.second.first;
const vector<unsigned> &vecB = b.second.first;
//check if vectors empty
if (vecA.empty()){
return true;
}
if (vecB.empty()){
return false;
}
if((vecA).size()==1){
minA=maxA=*((vecA).begin());
} else{
minA=*std::min_element((vecA).begin(),(vecA).end());
maxA=*std::max_element((vecA).begin(),(vecA).end());
}
if((vecB).size()==1){
minB=maxB=*((vecB).begin());
} else{
minB=*std::min_element((vecB).begin(),(vecB).end());
maxB=*std::max_element((vecB).begin(),(vecB).end());
}
if((minA<=minB)&&(maxA<=maxB)){
return true;
} else{
return false;
}
}
我還建議使用本地const引用變量來提高代碼的可讀性和性能。
std::sort
的文檔描述了比較器屬性:
二進制函數,接受范圍中的兩個元素作為參數,並返回可轉換為bool的值。 返回的值表示作為第一個參數傳遞的元素是否被認為是在它定義的特定嚴格弱順序中的第二個參數之前。
該函數不得修改其任何參數。
這可以是函數指針或函數對象。
嚴格的弱排序由以下特征定義( 來源 ):
Irreflexivity f(x, x) must be false.
Antisymmetry f(x, y) implies !f(y, x)
Transitivity f(x, y) and f(y, z) imply f(x, z).
Transitivity of equivalence Equivalence (as defined above) is transitive: if x is equivalent to y and y is equivalent to z, then x is equivalent to z.
如果f(x,y)
和f(y,x)
都是假的,則認為兩個元素x
和y
是等價的。
您定義的函數不遵循這些規則,特別是它違反了反自反性和反對稱性; 當minA == minB && maxA == maxB
, bucketComparator(a,b)
和bucketComparator(b,a)
都會產生true
值。 由於仿函數無效,因此會導致未定義的行為。
您需要更新您的仿函數以提供嚴格的弱序。 一種解決方案可能是更改聲明:
if((minA<=minB)&&(maxA<=maxB))
至
if( minA <= minB && maxA < maxB )
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.