结构列表,如何根据某个字段排序?

rechardfeng 2004-03-29 02:44:26
//请看代码
//环境vc 6.0

#include <iostream>
#include <algorithm>
#include <list>

using namespace std;

struct Test_Struct
{
long Item1;
long Item2;
};

struct item1_less_than{
bool operator()(const Test_Struct& _X, const Test_Struct& _Y) const
{return (_X.Item1 < _Y.Item1); }
};

int main(int, char**)
{
list<Test_Struct> lstTest;

Test_Struct t1 = {1,13};
Test_Struct t2 = {22,2};
Test_Struct t3 = {3,33};
Test_Struct t4 = {34,4};

lstTest.push_back(t1);
lstTest.push_back(t4);
lstTest.push_back(t2);
lstTest.push_back(t3);

list<Test_Struct>::iterator itr = lstTest.begin();

cout << "before sort" << endl;

for(; itr != lstTest.end(); itr++)
{
cout << "(" << itr->Item1 << "," << itr->Item2 << ")" << endl;
}

//根据Item1排序
//sort(lstTest.begin(), lstTest.end(), item1_less_than());

cout << "sort by item1" << endl;

for(; itr != lstTest.end(); itr++)
{
cout << "(" << itr->Item1 << "," << itr->Item2 << ")" << endl;
}


cout << endl;

return 0;
}

//但是好像不行,我如何是好?




...全文
83 13 打赏 收藏 转发到动态 举报
写回复
用AI写文章
13 条回复
切换为时间正序
请发表友善的回复…
发表回复
rechardfeng 2004-04-01
  • 打赏
  • 举报
回复
keiy() 50 答复问题,表达比较清晰
xueweizhong 40 问题答复得有深度
shallchen 10 答复问题时,可能没有实际去测试,而且不详细,没有解决我的问题
rechardfeng 2004-04-01
  • 打赏
  • 举报
回复
谢谢各位。
对stl的理解深入一步


rorot 2004-03-30
  • 打赏
  • 举报
回复
对,俺得方法时有BUG,向楼主和上面得人学习。。。
rechardfeng 2004-03-30
  • 打赏
  • 举报
回复
Re:rorot
1.首先谢谢你的答复;
2.你把问题复杂了,而且你的方法有bug;
rechardfeng 2004-03-30
  • 打赏
  • 举报
回复
//代码改为如下,即用vector来实现,vc6.0通过编译
#include <iostream>
#include <algorithm>
#include <vector>

using namespace std;

struct Test_Struct
{
long Item1;
long Item2;
};

struct item1_less_than{
bool operator()(const Test_Struct& _X, const Test_Struct& _Y) const
{return (_X.Item1 < _Y.Item1); }
};

int main(int, char**)
{
vector<Test_Struct> lstTest;

Test_Struct t1 = {1,13};
Test_Struct t2 = {22,2};
Test_Struct t3 = {3,33};
Test_Struct t4 = {34,4};

lstTest.push_back(t1);
lstTest.push_back(t4);
lstTest.push_back(t2);
lstTest.push_back(t3);

vector<Test_Struct>::iterator itr = lstTest.begin();

cout << "before sort" << endl;

for(; itr != lstTest.end(); itr++)
{
cout << "(" << itr->Item1 << "," << itr->Item2 << ")" << endl;
}

//根据Item1排序
sort(lstTest.begin(), lstTest.end(), item1_less_than());

cout << "sort by item1" << endl;

for(itr = lstTest.begin(); itr != lstTest.end(); itr++)
{
cout << "(" << itr->Item1 << "," << itr->Item2 << ")" << endl;
}

cout << endl;

return 0;
}


rechardfeng 2004-03-30
  • 打赏
  • 举报
回复
//1.首先谢谢各位。
//2.代码改为如下,vc6.0编译不能通过

#include <iostream>
#include <algorithm>
#include <list>

using namespace std;

struct Test_Struct
{
long Item1;
long Item2;
};

bool operator<(const Test_Struct &x, const Test_Struct &y)
{
return (x.Item1 < y.Item1);
}

//struct item1_less_than{
// bool operator()(const Test_Struct& _X, const Test_Struct& _Y) const
// {return (_X.Item1 < _Y.Item1); }
//};

int main(int, char**)
{
list<Test_Struct> lstTest;

Test_Struct t1 = {1,13};
Test_Struct t2 = {22,2};
Test_Struct t3 = {3,33};
Test_Struct t4 = {34,4};

lstTest.push_back(t1);
lstTest.push_back(t4);
lstTest.push_back(t2);
lstTest.push_back(t3);

list<Test_Struct>::iterator itr = lstTest.begin();

cout << "before sort" << endl;

for(; itr != lstTest.end(); itr++)
{
cout << "(" << itr->Item1 << "," << itr->Item2 << ")" << endl;
}

//根据Item1排序
//sort(lstTest.begin(), lstTest.end(), item1_less_than());
sort(lstTest.begin(), lstTest.end());

cout << "sort by item1" << endl;

for(itr = lstTest.begin(); itr != lstTest.end(); itr++)
{
cout << "(" << itr->Item1 << "," << itr->Item2 << ")" << endl;
}


cout << endl;

return 0;
}


柯本 2004-03-30
  • 打赏
  • 举报
回复
我不是试过了吗?用LIST是可以的:(VC6.0)


#include <iostream>
#include <algorithm>
#include <list>



using namespace std;


struct Test_Struct
{
long Item1;
long Item2;
};

bool operator<(const Test_Struct &x, const Test_Struct &y)
{
return (x.Item1 < y.Item1);
}


int main(int, char**)
{
list<Test_Struct> lstTest;

Test_Struct t1 = {1,13};
Test_Struct t2 = {22,2};
Test_Struct t3 = {3,33};
Test_Struct t4 = {34,4};

lstTest.push_back(t1);
lstTest.push_back(t4);
lstTest.push_back(t2);
lstTest.push_back(t3);

list<Test_Struct>::iterator itr = lstTest.begin();

cout << "before sort" << endl;

for(; itr != lstTest.end(); itr++)
{
cout << "(" << itr->Item1 << "," << itr->Item2 << ")" << endl;
}
//根据Item1排序
//sort(lstTest.begin(), lstTest.end());//, item1_less_than());

lstTest.sort();

cout << "sort by item1" << endl;
for(itr = lstTest.begin(); itr != lstTest.end(); itr++)
{
cout << "(" << itr->Item1 << "," << itr->Item2 << ")" << endl;
}


cout << endl;
return 0;
}

shallchen 2004-03-29
  • 打赏
  • 举报
回复
struct item1_less_than:public binary_function<Test_Struct, Test_Struct, bool> {
bool operator()(const Test_Struct& _X, const Test_Struct& _Y) const
{return (_X.Item1 < _Y.Item1); }
};
rorot 2004-03-29
  • 打赏
  • 举报
回复
不好意思啦,本来想用MAP做,利用MAP得索引可能要好。
但是俺水平有限,,先用sort + vector做了,再考虑别得吧,晕~
-----------------------------------------------------
#include <iostream>
#include <iomanip>
#include <vector>
#include <algorithm>

using namespace std;

// 自定义结构
struct Test_Struct
{
long Item1;
long Item2;
};

// 自定义装载, 排序, 显示函数
void LoadValue( vector<Test_Struct> & );
void SortByItem( const vector<Test_Struct> &, int );
void Output( const vector<Test_Struct> &tvec, int );

int main()
// 测试字段排序
{
// 节点容器
vector<Test_Struct> tvec;

// 装载测试数据
LoadValue( tvec );

// 按字段item排序
cout << "sort by Item1....\n";
SortByItem( tvec, 1 );
cout << "sort by Item2....\n";
SortByItem( tvec, 2 );

return 0;
}

// 装载数据
void LoadValue( vector<Test_Struct> &tvec )
{
Test_Struct t1 = {1,13};
Test_Struct t2 = {22,2};
Test_Struct t3 = {3,33};
Test_Struct t4 = {34,4};

tvec.push_back(t1);
tvec.push_back(t4);
tvec.push_back(t2);
tvec.push_back(t3);
}

void SortByItem( const vector<Test_Struct> &tvec, int nItem )
{
int n, i = 0;
// 临时容器(当数组用)
vector<long> svec;
vector<long> lvec;

for ( i=0; i<tvec.size(); ++i )
{
// 排序方式
if ( nItem == 1 )
{
lvec.push_back( tvec[i].Item1 );
svec.push_back( tvec[i].Item1 );
}
else
{
lvec.push_back( tvec[i].Item2 );
svec.push_back( tvec[i].Item2 );
}

}

// 临时字段排序
sort( lvec.begin(), lvec.end() );
// 泛性指针
vector<long>::iterator it = svec.begin();

// 显示结果
for ( i=0; i<lvec.size(); ++i )
{
n = find( it, it+svec.size(), lvec[i] ) - it;
Output( tvec, n );
}
}

// 格式化输出数据
void Output( const vector<Test_Struct> &tvec, int n )
{
cout << setiosflags( ios::left )
<< setw(8) << tvec[n].Item1;
cout << setiosflags( ios::left )
<< setw(8) << tvec[n].Item2;
cout << endl;
}
rorot 2004-03-29
  • 打赏
  • 举报
回复
用map
柯本 2004-03-29
  • 打赏
  • 举报
回复
用dev-cpp编译
lstTest.sort(item1_less_than());
也是通过的,而VC只能用
lstTest.sort(); // 当然要先定义bool operator<(
WHY???
xueweizhong 2004-03-29
  • 打赏
  • 举报
回复
template <typename Iter, typename Func>
void sort(Iter begin, Iter end, Func func);

要求Iter是Random Iterator(随机迭代子)
而list<xxx>::Iterator不是,只是一个双向迭代子(Birdirectional Iterator)

所以解决方案有两个:
1: 把list改为vector
或者
2: sort(l.begin(), l.end(), xxx)
改为
l.sort(...) // list的成员函数版本。
柯本 2004-03-29
  • 打赏
  • 举报
回复
我试了一下,sort对list无效(对vector有效),不过list自已就有sort

//根据Item1排序
lstTest.sort(item1_less_than());
还有下面的有问题,itr要重赋
for(; itr != lstTest.end(); itr++)
改为
for(itr = lstTest.begin(); itr != lstTest.end(); itr++)
在BCB下通过,在VC下说类型不对,没办法只好用
bool operator<(const Test_Struct &x, const Test_Struct &y)
{
return (x.Item1 < y.Item1);
}
...
lstTest.sort();
vc6.0下编译通过!!!


24,855

社区成员

发帖
与我相关
我的任务
社区描述
C/C++ 工具平台和程序库
社区管理员
  • 工具平台和程序库社区
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

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