学了一段STL,深为它的精巧所折服。 一个习作,还请高人指点,看何处还可以改进。

rtdb 2003-04-11 01:49:09
程序主要功能:按名(名不可重复)管理一个状态字链表。 

#include "stdafx.h"

#include <string>
#include <list>
#include <algorithm>
#include <iostream>
#include <fstream>
using namespace std;


class CKeyUser : public string
{
public:
string m_Status ;

CKeyUser(string name, string status):string(name), m_Status(status){};

bool operator == (const string& rh)
{
return string(*this) == rh ;
}
bool operator > (const string& rh)
{
return string(*this) > rh ;
}
bool operator < (const string& rh)
{
return string(*this) < rh ;
}
};


class CUserList : public list<CKeyUser>
{
public:

/* add a user, if user exist, update its status */
void Add(string name, string status)
{
list<CKeyUser>::iterator it ;
it = find(begin(), end(), name) ;
if ( it == end())
{
CKeyUser cUser(name, status) ;
push_back(cUser) ;
}
else
it->m_Status = status ;

}

void Del(string name)
{
list<CKeyUser>::iterator it ;
it = find(begin(), end(), name) ;
if ( it != end())
erase(it) ;
}

};

ostream& operator << (ostream &cout, CUserList& cList)
{
cout << endl << "Current list:" << endl;

list<CKeyUser>::iterator it ;
for ( it = cList.begin(); it != cList.end(); it++)
{
cout << "status: " << it->m_Status << " key: " << string(*it) << endl ;
}
return cout ;
}

int main(int argc, char* argv[])
{
cout << "Hello World!" << endl ;

CUserList cList ;
cList.Add("aaa", "1") ;
cList.Add("bbb", "2") ;
cList.Add("ccc", "3") ;
cList.Add("ddd", "4") ;
cout << cList ;

cList.Add("aaa", "11") ;
cList.Add("bbb", "22") ;
cout << cList ;

cList.Del("bbb") ;
cout << cList ;

cList.Del("ddd") ;
cout << cList ;

ofstream of("test.txt") ;
of << cList ;
return 0;
}

...全文
76 17 打赏 收藏 转发到动态 举报
写回复
用AI写文章
17 条回复
切换为时间正序
请发表友善的回复…
发表回复
熊主任 2003-04-14
  • 打赏
  • 举报
回复
map<key, value>
是按key排序的,如果对value排序,导出到其他容器先,然后调用sort。
liu_feng_fly 2003-04-14
  • 打赏
  • 举报
回复
map只能按照key进行排序,如果你要按照value进行排序,可以把map导出到一个vector里面,然后对vector进行排序。
我有一个例子,是对一个类里面的不同成员变量进行排序的,不能应用于map,但是对vector,deque这样的非关联式容器是可以的,算是对stl的一个小小扩充吧
// sort.cpp : Defines the entry point for the console application.
//

#include "stdafx.h"
#include <time.h>
#include <iostream>
#include <algorithm>
#include <functional>
using namespace std;

struct A
{
A(int n1 = 0,int n2 = 0):val1(n1),val2(n2){}
int val1;
int val2;
friend ostream& operator << (ostream& os, const A& a);
};
ostream& operator << (ostream& os, const A& a)
{
os << a.val1 << ' ' << a.val2 << endl;
return os;
}

//仅仅对一个类里面的某一个成员函数进行比较
template<typename C,typename T>//C:classname T:class's member's type
class CompareMember : public binary_function<C,C,bool>
{
public:
CompareMember(T C::* pMem) : m_pMem(pMem){};
bool operator()(const C& first,const C& second) const
{
return first.*m_pMem < second.*m_pMem;
}
private:
T C::* m_pMem;
};

//使CompareMember更加容易使用
template<typename C,typename T>//C:classname T:class's member's type
CompareMember<C,T> Compare_Member(T C::* pMem)
{
return CompareMember<C,T>(pMem);
}

const nCount = 20;
A arrA[nCount];

void Display()
{
for( int i = 0; i < nCount; i++)
{
cout << arrA[i];
}
}

int main(int argc, char* argv[])
{

srand((unsigned)time(NULL));//random_shuffle调用rand作为随机数,所以先初始化一下

for(int i = 0; i < nCount; i++)
{
arrA[i].val1 = 0;
arrA[i].val2 = nCount - i;
}

cout << "after init" << endl;

Display();

random_shuffle(arrA,arrA + nCount);

cout << "after random shuffle" << endl;

Display();

cout << "will sort val2 as asc" << endl;

stable_sort(arrA,arrA + nCount,Compare_Member(&A::val2));

cout << "after sort val2 as asc" << endl;

Display();

random_shuffle(arrA,arrA + nCount);

cout << "after random shuffle" << endl;

Display();

cout << "will sort val1 as asc" << endl;

stable_sort(arrA,arrA + nCount,(Compare_Member(&A::val1)));

cout << "after sort val1 as asc" << endl;

Display();

//Compare_Member可以搭配各种函数配接器进行使用,下面是一个例子
//寻找数组里面val2的值不小于16的元素
A* pA = find_if(arrA,arrA + nCount,bind2nd(not2(Compare_Member(&A::val2)),A(10,16)));

if(pA != arrA + nCount)
{
cout << "Find,and value is:" << endl;
cout << *pA << endl;
}
else
{
cout << "Not find!" << endl;
}

return 0;
}
rtdb 2003-04-14
  • 打赏
  • 举报
回复
To panda_lin(熊猫) :
map也可以排序么?

不知可不可以照我的程序给个例子,200分全归你?
熊主任 2003-04-14
  • 打赏
  • 举报
回复
怎么不可以?重载比较操作符。
rtdb 2003-04-14
  • 打赏
  • 举报
回复
若是要按人名排序, 还可以用map么?
rtdb 2003-04-14
  • 打赏
  • 举报
回复
谢谢大家。 结帐先。 讨论设计而不是STL的朋友就少给分了。
njustar 2003-04-12
  • 打赏
  • 举报
回复
我个人认为用map更为合适,这样CUserList也没有存在的必要
zhouhu 2003-04-12
  • 打赏
  • 举报
回复
STL
熊主任 2003-04-12
  • 打赏
  • 举报
回复
为什么不用map?因为你用线性链表的话,每插入一个新数据的效率是非常低的,复杂度为O(n),而如果你用map,内部是用二叉树实现,插入新数据的复杂度降为O(log2(N))。如果数据量大的话,优势是非常明显的。或者你也可以用hash表实现,不过hash表排序不方便。
elvahuang 2003-04-11
  • 打赏
  • 举报
回复
mark一下

暑假学
rtdb 2003-04-11
  • 打赏
  • 举报
回复
嗯, 用map是个好主意。
liu_feng_fly 2003-04-11
  • 打赏
  • 举报
回复
为什么不试试map,他会自动按照key进行排序。
即使你继承了string,还不是一样要写operator<,operator ==等等这些东西,如果不从string继承,提供了这些operator不是一样可以简单的排序。况且,public继承反映了is-a的关系,你认为你这样设计能够反映这样的关系么?
rtdb 2003-04-11
  • 打赏
  • 举报
回复
从string继承, 是因为这个类的主键是名字(保存在string中), 要实现按名查找、排序等操作,
可以直接以其基类string的形式进行。我认为这样能简单些。

若是改为
class CKeyUser
{
string m_Status ;
string m_Name ;
}

那么按名按名查找、排序等操作还会这样简单么?
ThinkX 2003-04-11
  • 打赏
  • 举报
回复
你去看看string,它是否又虚拟的构造函数,如果没有,就不要继承它,这是一个类的默认规则。
用构造上讲,从string继承也毫无意义。
一般上说,如果有==运算符,也要有!=运算符
liu_feng_fly 2003-04-11
  • 打赏
  • 举报
回复
实在看不出你为什么要继承string
winespirit 2003-04-11
  • 打赏
  • 举报
回复
同感,应该用聚合而不是从string继承。
alula 2003-04-11
  • 打赏
  • 举报
回复
using composition instead of inheritance

24,853

社区成员

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

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