免费注册 查看新帖 |

Chinaunix

  平台 论坛 博客 文库
12
最近访问板块 发新帖
楼主: jchc
打印 上一主题 下一主题

[C++] 小小请教一下:vector的引用如何遍历(和类型转换)? [复制链接]

论坛徽章:
0
11 [报告]
发表于 2008-12-24 17:38 |只看该作者

回复 #10 jchc 的帖子

用智能指针吧(std::tr1::shared_ptr)

论坛徽章:
0
12 [报告]
发表于 2008-12-24 19:00 |只看该作者
没有必要,去知道iterator的类型。

#include<vector>
#include<string>
#include<iostream>
using namespace std;

template< typename T>
inline void print( T begin, T end){
&nbsp;&nbsp;&nbsp;&nbsp;while( begin != end)
&nbsp;&nbsp;&nbsp;&nbsp;{
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;cout<<*begin++<<endl;
&nbsp;&nbsp;&nbsp;&nbsp;}
}

template< typename T>
inline void print_all( const T& container ){
&nbsp;&nbsp;&nbsp;print( container.begin(), container.end());
}


int main(){
&nbsp;&nbsp;&nbsp;vector<string> values;
&nbsp;&nbsp;&nbsp;values.push_back( "this");
&nbsp;&nbsp;&nbsp;values.push_back( "is");
&nbsp;&nbsp;&nbsp;values.push_back( "only");
&nbsp;&nbsp;&nbsp;values.push_back( "a");
&nbsp;&nbsp;&nbsp;values.push_back( "test");
&nbsp;&nbsp;&nbsp;print_all( values);
&nbsp;&nbsp;&nbsp;return 0;
}
您需要登录后才可以回帖 登录 | 注册

本版积分规则 发表回复

  

北京盛拓优讯信息技术有限公司. 版权所有 京ICP备16024965号-6 北京市公安局海淀分局网监中心备案编号:11010802020122 niuxiaotong@pcpop.com 17352615567
未成年举报专区
中国互联网协会会员  联系我们:huangweiwei@itpub.net
感谢所有关心和支持过ChinaUnix的朋友们 转载本站内容请注明原作者名及出处

清除 Cookies - ChinaUnix - Archiver - WAP - TOP