随笔-174  评论-598  文章-0  trackbacks-0
#include  < iostream >
#include 
< map >
#include 
< string >

using   namespace  std;

int  main()
{
    map
< string int >  map1;
    map
< string , map < string int >   >  map2;
    map1[
" test " =   2 ;
    map2[
" test1 " =  map1;

    cout 
<<  map2[ " test1 " ][ " test " <<  endl;
    
return   0 ;
}
以上代码简单的实现了map对"二维"形式的支持,很简单,不是么?可是我第一次面对这个问题的时候模板套模板的整糊涂了,记录一个留个念想~~
posted on 2006-03-22 00:06 那谁 阅读(692) 评论(5)  编辑 收藏 引用 所属分类: C\C++

评论:
# re: 模板套模板~~ 2006-03-22 09:08 | 沐枫
typedef一下就不糊涂了。  回复  更多评论
  
# re: 模板套模板~~ 2006-04-07 17:59 | 红叶搂风
#include <iostream>
#include <map>
#include <string>

using std::map;
using std::cout;
using std::endl;
using std::string;

template<class T1,class T2>
void PrintMap(map<T1,T2 >& maptemp,string tempstr = "")
{
cout<<tempstr
<<endl;
typename map<T1,T2 >::const_iterator pos;

for (pos = maptemp.begin();pos != maptemp.end();++pos) {
cout<<"map["
<<pos->first
<<"]="
<<pos->second
<<" ";
}

}

int main(void)
{
map<string,int> map1;
map<string,map<string,int> > map2;

map1["string"] = 2;
map2["string2"] = map1;

PrintMap<string,int>(map1,"map1");

return 0;
}  回复  更多评论
  
# re: 模板套模板~~ 2006-04-07 18:01 | 绾㈠彾鎼傞
你那样使用根本不符合STL的习惯的  回复  更多评论
  
# re: 模板套模板~~ 2006-07-09 00:27 | Corner
记住,STL容器提供的是元素的值拷贝语义。
想想map2["string2"] = map1;是发生了什么事情?明白的话,就不应该这样使用map了  回复  更多评论
  
# re: 模板套模板~~ 2006-07-09 00:35 | 创系
to corner,那应该如何写?请明示,谢谢~~  回复  更多评论
  


标题  
姓名  
主页
验证码 *
内容(提交失败后,可以通过“恢复上次提交”恢复刚刚提交的内容)  
  登录  使用高级评论  新用户注册  返回页首  恢复上次提交      
[使用Ctrl+Enter键可以直接提交]
.NET频道  博客园社区  闪存
网站导航: