【发布时间】:2012-04-09 14:19:11
【问题描述】:
嗨,我有一个指针向量,实际上每个指针都是一个数组,其中每个数组是:
int a,int b,可变大小的整数序列。
无序向量示例:
rows[0] => points to [1,2,...]
rows[1] => points to [2,1,...]
rows[2] => points to [3,1,...]
rows[3] => points to [1,4,...]
rows[4] => points to [1,1,...]
输出示例:
rows[0] => points to [1,1,...]
rows[1] => points to [1,2,...]
rows[2] => points to [1,4,...]
rows[3] => points to [2,1,...]
rows[4] => points to [3,1,...]
我需要以这种方式对这个向量进行排序,我创建了以下自定义比较函数:
bool cmpRow(unsigned int *a, unsigned int *b)
{
//Mesmo id word
if(a[0] == b[0])
{
return (a[1] < b[1]);
}
else
{
return (a[0] < b[0]);
}
}
我使用它的方式如下:
std::vector<unsigned int*> rows;
.
.
//Insert some stuffs
.
.
std::sort (rows.begin(), rows.end(), cmpRow);
但结果出乎我的意料,谁能帮我解决这个问题?
编辑:
其实函数没问题,问题出在循环内的函数中,这个函数调用排序函数的次数超过了必要的次数,所以结果不是预期的。
【问题讨论】:
-
std::sort对 [first,last) 范围内的底层容器的元素进行排序。1,1,1,2,3的顺序表明您的元素确实已排序,请注意容器元素在你的情况下是数组,所以只有那些会被排序并且它们是。似乎你想要的是对每个向量元素的单个数组元素进行排序,这不是std::sort所做的。 -
同时发布您的预期输出。
-
rows的类型是什么?你得到什么结果(你说不是预期的结果)? -
请说明您的(1)预期结果和(2)观察结果
-
我没有发布当前输出,因为输入是一个大文件,我认为问题是我不明白排序函数在内部实际上做了什么。