求助关于STL sort排序的问题!100分求助!!!!

qiannianmen_110 2012-10-19 05:53:03
我现在用sort排序一个vector,但是我发现它好像不是每次都把所有数据排一遍。(比如,8个数据它排了不到20次)
我写的比较函数较复杂,有三个出口。这样就导致排序最后的结果不是我预期的
我想问,第一,我描述的情况是不是会出现
第二,出现了有没有什么解决办法。(因为我实在想不到什么好方法能简化比较函数,这比较函数基本上只能这么写了。。。)
而且还有个什么情况阿,这个排序基本上每帧都调用,而且排序的vector有可能非常大,所以从效率上来讲,我擦,蛋疼!好像还离不开sort!
...全文
241 10 打赏 收藏 转发到动态 举报
AI 作业
写回复
用AI写文章
10 条回复
切换为时间正序
请发表友善的回复…
发表回复
ri_aje 2012-10-21
  • 打赏
  • 举报
回复
[Quote=引用 7 楼 的回复:]

是这样,举例来说吧,我要对几个这种结构体
{
int metaID;
int weight;
float distance;

}
排序
排序的规则是这样的
{
if(a.metaID == b.metaID)
{
if(a.weight == b.weight)
{return a.dista……
[/Quote]
这个比较函数没有定义严格弱序。比如对于

x = {1,3,5.5};
y = {1,2,3.5};
z = {2,2,4.5};

这个比较函数认为,x<y,y<z 并且 !(x<z)。而严格弱序要求 x<y,y<z 必须推出 x<z。简而言之,这样的比较规则定义的顺序是混乱的。
qiannianmen_110 2012-10-21
  • 打赏
  • 举报
回复
[Quote=引用 9 楼 的回复:]

引用 7 楼 的回复:

是这样,举例来说吧,我要对几个这种结构体
{
int metaID;
int weight;
float distance;

}
排序
排序的规则是这样的
{
if(a.metaID == b.metaID)
{
if(a.weight == b.weight)
{return a.dista……

这个比较函数没有定义严格弱序。比……
[/Quote]

谢谢举例详解,我懂了!我再看看我应该怎么更改我这个比较函数吧。谢谢啦!给分!
FancyMouse 2012-10-21
  • 打赏
  • 举报
回复
>我写的比较函数较复杂,有三个出口
STL的sort接受的比较函数不是三出口函数

>第一,我描述的情况是不是会出现
它没排出来说明你给的比较函数有问题。
qiannianmen_110 2012-10-21
  • 打赏
  • 举报
回复
是这样,举例来说吧,我要对几个这种结构体
{
int metaID;
int weight;
float distance;

}
排序
排序的规则是这样的
{
if(a.metaID == b.metaID)
{
if(a.weight == b.weight)
{return a.distance < b.distance;}
else
{return a.weight > b.weight;}
}
return a.distance < b.distance;
}
ri_aje 2012-10-20
  • 打赏
  • 举报
回复
std::sort 要求比较函数定义严格若弱序,如果你的比较函数不满足的话(出口多的比较函数很容易不满足),sort 的结果就不好说了,最好把你比较函数的代码贴上来看看,同时也说一下要达到的排序效果。
mujiok2003 2012-10-19
  • 打赏
  • 举报
回复

//could be very big
struct Data
{
};

std::vector<Data*> v; //since sizeof(Data) could be quite big, we keep Data* instead Data

//function to compare Data. You have to give a correct implemention
bool comp(Data const* p1, Data* const p2);

//revesve memory to reduce the times of memory allocation. you should adjust this number for your case
v.reserve(10000);

//collecting data....
v.push_back(new Data);

std::sort(v.begin(), v.end(), comp);
漫步者、 2012-10-19
  • 打赏
  • 举报
回复


//都给你搬出来
template <class RandomAccessIterator>
void sort ( RandomAccessIterator first, RandomAccessIterator last );

template <class RandomAccessIterator, class Compare>
void sort ( RandomAccessIterator first, RandomAccessIterator last, Compare comp );


//Sort elements in range


Sorts the elements in the range [first,last) into ascending order.

The elements are compared using operator< for the first version, and comp for the second.

Elements that would compare equal to each other are not guaranteed to keep their original relative order.
漫步者、 2012-10-19
  • 打赏
  • 举报
回复

例子:
#include <iostream>
#include <algorithm>
#include <vector>
using namespace std;

bool myfunction (int i,int j) { return (i<j); }

struct myclass {
bool operator() (int i,int j) { return (i<j);}
} myobject;

int main () {
int myints[] = {32,71,12,45,26,80,53,33};
vector<int> myvector (myints, myints+8); // 32 71 12 45 26 80 53 33
vector<int>::iterator it;

// using default comparison (operator <):
sort (myvector.begin(), myvector.begin()+4); //(12 32 45 71)26 80 53 33

// using function as comp
sort (myvector.begin()+4, myvector.end(), myfunction); // 12 32 45 71(26 33 53 80)

// using object as comp
sort (myvector.begin(), myvector.end(), myobject); //(12 26 32 33 45 53 71 80)

// print out content:
cout << "myvector contains:";
for (it=myvector.begin(); it!=myvector.end(); ++it)
cout << " " << *it;

cout << endl;

return 0;
}
图灵狗 2012-10-19
  • 打赏
  • 举报
回复
vector排序没什么问题啊,给你个例子吧:

#include <iostream>
#include <vector>
#include <algorithm>

//先自定义一个结构体
struct Test {
int member1;
int member2;
};

//自定义排序函数
bool SortByM1( const Test &v1, const Test &v2)//注意:本函数的参数的类型一定要与vector中元素的类型一致
{
return v1.member1 < v2.member1;//升序排列
}

void MyPushback(std::vector<Test> & vecTest, const int &m1, const int &m2)
{
Test test;
test.member1 = m1;
test.member2 = m2;
vecTest.push_back(test);
}

void PrintVector( std::vector<Test> & vec)
{
/*
插一句,
vec.begin()对应的位置是向量的第一个位置,
vec.end()对应的是vector中的最后的一个元素位置的后面的一个位置(我认为,实际上是一个无效位置)
文档上的定义:Returns an iterator referring to the past-the-end element in the vector container.
*/
for(std::vector<Test>::iterator it = vec.begin() ; it != vec.end() ; it++ )
{
std::cout<<it->member1<<'\t'<<it->member2<<std::endl;
}
}

int main(int argc, char* argv[])
{
std::vector<Test> vecTest;
MyPushback(vecTest,9,1);
MyPushback(vecTest,8,2);
MyPushback(vecTest,7,3);
MyPushback(vecTest,6,4);
MyPushback(vecTest,5,5);
MyPushback(vecTest,4,6);
MyPushback(vecTest,3,7);
MyPushback(vecTest,2,8);
MyPushback(vecTest,1,9);
//排序之前
std::cout<<"Before Sort:"<<std::endl;
PrintVector(vecTest);

std::cout<<"对向量中的所有元素按member1进行升序排列:"<<std::endl;
std::sort(vecTest.begin(),vecTest.end(),SortByM1);
PrintVector(vecTest);

//std::cout<<"对向量中的第2个到第5个元素按member1进行升序排列:"<<std::endl;
//std::sort(vecTest.begin()+1,vecTest.begin()+5,SortByM1);//vecTest.begin()+5为第6个位置
//PrintVector(vecTest);

return 0;
}

  • 打赏
  • 举报
回复
可以试试用set

65,186

社区成员

发帖
与我相关
我的任务
社区描述
C++ 语言相关问题讨论,技术干货分享,前沿动态等
c++ 技术论坛(原bbs)
社区管理员
  • C++ 语言社区
  • encoderlee
  • paschen
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
  1. 请不要发布与C++技术无关的贴子
  2. 请不要发布与技术无关的招聘、广告的帖子
  3. 请尽可能的描述清楚你的问题,如果涉及到代码请尽可能的格式化一下

试试用AI创作助手写篇文章吧