| 网站首页 | 业界新闻 | 小组 | 威客 | 人才 | 下载频道 | 博客 | 代码贴 | 在线编程 | 编程论坛
欢迎加入我们,一同切磋技术
用户名:   
 
密 码:  
共有 542 人关注过本帖
标题:请教迭代器如何工作?
只看楼主 加入收藏
wumingsx
Rank: 1
等 级:新手上路
帖 子:20
专家分:0
注 册:2006-3-10
收藏
 问题点数:0 回复次数:3 
请教迭代器如何工作?
#include <iostream>
#include <string>
using namespace std;
int main()
{
string str;
str = "Good";
str += ' ';
str += "afternoon!";
string::iterator it;
it = str.begin();
while( it != str.end() ){
cout<<*it;
it++;
}
cout<<endl;
string::reverse_iterator rit;
rit = str.rbegin();
while( rit != str.rend() ){
cout<<*rit;
rit++;
}
cout<<endl;
return 0;
}

中的str函数都是什么意思,谢谢
搜索更多相关主题的帖子: 工作 afternoon include return 
2006-03-17 20:10
sunnvya
Rank: 5Rank: 5
等 级:贵宾
威 望:17
帖 子:1094
专家分:0
注 册:2005-11-23
收藏
得分:0 
C++中类的封装,都封装在STD里面

http://www. 第二站>>>提供源码下载
2006-03-17 21:48
sunnvya
Rank: 5Rank: 5
等 级:贵宾
威 望:17
帖 子:1094
专家分:0
注 册:2005-11-23
收藏
得分:0 
哦,看错了
STR是你定义的字符串
STRING在C++中是新字符串类不是C中的哪个,C中的是用CSTRING取代了

http://www. 第二站>>>提供源码下载
2006-03-17 21:50
知秋一叶
Rank: 1
等 级:新手上路
帖 子:6
专家分:0
注 册:2006-3-11
收藏
得分:0 

#include <iostream>
#include <string>


using namespace std;


int main()
{
string str; // str
为空字符串
str = "Good"; // Good写入str
str += ' '; //Good后加空格
str += "afternoon!"; //在空格后加afternoon!
string::iterator it; //声明正向迭代器it
it = str.begin(); //it
指向字符串str的起始位置

while( it != str.end() ) //it指向字符串str的末尾时,循环结束

{
cout<<*it
; //输出str: Good afternoon!
it++;
}


cout<<endl;
string::reverse_iterator rit; //
声明反向迭代器it
rit = str.rbegin();
while( rit != str.rend() )
{
cout<<*rit; //
输出str:!noonretfa dooG
rit++;
}
cout<<endl;

system("pause"); //查看输出结果


return 0;
}

[此贴子已经被作者于2006-3-17 22:30:03编辑过]

2006-03-17 22:23
快速回复:请教迭代器如何工作?
数据加载中...
 
   



关于我们 | 广告合作 | 编程中国 | 清除Cookies | TOP | 手机版

编程中国 版权所有,并保留所有权利。
Powered by Discuz, Processed in 0.014995 second(s), 7 queries.
Copyright©2004-2024, BCCN.NET, All Rights Reserved