( std::map > std::set ) ? true : false

anrxhzh 2002-07-23 04:22:28
以前在 Microsoft Visual C++ 6.0 STL 下做过一个测试,比较 set、map 的差别。发现对于同样的数据结构(比如数据库中某个有主键的表的记录),map 使用的内存大约是 set 的两倍。谁能从数据结构的角度分析一下这个结果?
...全文
119 10 打赏 收藏 转发到动态 举报
写回复
用AI写文章
10 条回复
切换为时间正序
请发表友善的回复…
发表回复
prototype 2002-07-25
  • 打赏
  • 举报
回复
> 那么 Andrei Alexandrescu 展示的设计模式和经典的设计模式有什么不同呢?
the same. conventionally, people believe that design patterns are pure abstract thingies (souls) that cannot be demonstrated without concrete examples (bodies), iow, they can't be codedly expressed without examples. andrei shows you can... with c++ templates.
prototype 2002-07-25
  • 打赏
  • 举报
回复
wrong thread. sorry.
prototype 2002-07-25
  • 打赏
  • 举报
回复
try:
http://perso.club-internet.fr/cbeaudet/

i never used it. so not sure of its quality. let's know the results you get of using it.
zhouzicn 2002-07-24
  • 打赏
  • 举报
回复
OOP和GP,我觉得,从算法和数据结构角度,OOP注重数据结构的设计,算法为数据结构服务;GP用把算法与数据结构放在同等位置,通过中间的iterator连接;传统的过程编程更关心算法。这可能是三种程序设计模式的差异。
anrxhzh 2002-07-24
  • 打赏
  • 举报
回复
哎呀,我怎么无法重现那一次的测试结果了。记得那次测试的内存使用量是Gb量级的,现在没有这个环境了。算了,取消这个帖子,不浪费大家的时间了。我换个问题好了:GP + DP = ?

在 Modern C++ Design 一书中,Andrei Alexandrescu 通过 Generic programming using templates 演绎了一些经典的设计模式。这种新的思想别有风味,甚至不易被人接受。我相信这本书的动机肯定不是为了证明“OO能做到的GP也能做到”,那样就太无聊了。那么 Andrei Alexandrescu 展示的设计模式和经典的设计模式有什么不同呢?

附:测试程序
#pragma warning ( disable:4786 )
#include <map>
#include <set>
#include <memory.h>
#include <iostream>
#include <cstdlib>

const int key_len( 32 );
const int data_len( 256 );
const int size ( 200000 );

struct Key {
char buf[key_len];
};

struct Data {
char buf[data_len];
};

struct Record {
Key k;
Data d;
};

struct key_cmp {
bool operator() ( const Key& l, const Key& r )
{ return memcmp(l.buf,r.buf,key_len)<0; }
};

struct rec_cmp {
bool operator() ( const Record& l, const Record& r )
{ return key_cmp()(l.k,r.k); }
};

typedef std::set< Record, rec_cmp > Set;
typedef std::map< Key, Data, key_cmp > Map;

void test_map( int c )
{
static Map m;
for( int i=0; i<c; i++ ) {
Key k;
memcpy( k.buf, &i, sizeof(i) );
m[k];
}
std::cout << "map size=" << m.size() << std::endl;
}

void test_set( int c )
{
static Set s;
for( int i=0; i<c; i++ ) {
Record r;
memcpy( r.k.buf, &i, sizeof(i) );
s.insert( r );
}
std::cout << "set size=" << s.size() << std::endl;
}

int main( int arg)
{
std::cout << "Key Length:" << key_len << "\nData Length:" << data_len << std::endl;
( arg==1 ) ? test_map( size ) : test_set( size );
system( "pause" );
return 0;
}
elvahuang 2002-07-24
  • 打赏
  • 举报
回复
gz
anrxhzh 2002-07-23
  • 打赏
  • 举报
回复
Example:

struct Data;

struct Record {
K key;
Data data;
}

std::set<Record>
std::map<K,Data>
rosysun 2002-07-23
  • 打赏
  • 举报
回复
map不是比set多了一个存储Key的存储单元吗?
rosysun 2002-07-23
  • 打赏
  • 举报
回复
map不是比set多了一个存储Key的存储单元吗?
e2wugui 2002-07-23
  • 打赏
  • 举报
回复
map set 都是用同一个 tree (rbtree) 结构实现的。

从数据结构角度来看,消耗是一样的,他们的区别是存贮的东西不一样。

map<K,T> 存储 pair<const K, T>
set<K> 存储 K

你当时测试的时候是怎么定义 K, T 的?


!·#!#¥#¥!#¥!#¥

今天好csdn好慢!半天上不去。歇几天再来看。:)

69,373

社区成员

发帖
与我相关
我的任务
社区描述
C语言相关问题讨论
社区管理员
  • C语言
  • 花神庙码农
  • 架构师李肯
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

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