string取下标返回的是什么类型?

J_X_Q 2008-09-27 01:50:54
是char还是string?
...全文
704 15 打赏 收藏 转发到动态 举报
写回复
用AI写文章
15 条回复
切换为时间正序
请发表友善的回复…
发表回复
yuanlong_zheng 2009-06-13
  • 打赏
  • 举报
回复
受教了!
yanhang1589 2008-09-29
  • 打赏
  • 举报
回复
char
winterdebin 2008-09-28
  • 打赏
  • 举报
回复
char?
lann64 2008-09-27
  • 打赏
  • 举报
回复
[Quote=引用 4 楼 steedhorse 的回复:]
严格讲是char的引用类型:

const_reference operator[](size_type _Off) const;
reference operator[](size_type _Off);

string是basic_string <char>,于是,
string::reference就是:char&
string::const_reference就是:const char&
[/Quote]
up
wxb_nudt 2008-09-27
  • 打赏
  • 举报
回复
用[]返回的是不加范围检查的char,
用.at()返回的是加了越界检查的char或者null
小猴饲养员 2008-09-27
  • 打赏
  • 举报
回复
强悍的四楼。学习。
帅得不敢出门 2008-09-27
  • 打赏
  • 举报
回复
up 4楼.
const_reference operator[](
size_type _Off
) const;
reference operator[](
size_type _Off
);

Return Value
A reference to the character of the string at the position specified by the parameter index.

返回的是char的引用
kingteng 2008-09-27
  • 打赏
  • 举报
回复
返回的是string::size_type 是一个unsigned int的值
zhuwanglove 2008-09-27
  • 打赏
  • 举报
回复
学习.
wind_breez 2008-09-27
  • 打赏
  • 举报
回复
返回的是字符类型
双子东宝 2008-09-27
  • 打赏
  • 举报
回复
受教了
晨星 2008-09-27
  • 打赏
  • 举报
回复 1
严格讲是char的引用类型:

const_reference operator[](size_type _Off) const;
reference operator[](size_type _Off);

string是basic_string<char>,于是,
string::reference就是:char&
string::const_reference就是:const char&
太乙 2008-09-27
  • 打赏
  • 举报
回复


#include <iostream>
#include <string>
using namespace std;
int main()
{
string s="hello world";
cout<<s[3]<<endl;
cout<<&s[3]<<endl;
return 0;
}
---------------------

l
lo world
Press any key to continue
晨星 2008-09-27
  • 打赏
  • 举报
回复
char。
太乙 2008-09-27
  • 打赏
  • 举报
回复
返回的地址!

64,676

社区成员

发帖
与我相关
我的任务
社区描述
C++ 语言相关问题讨论,技术干货分享,前沿动态等
c++ 技术论坛(原bbs)
社区管理员
  • C++ 语言社区
  • encoderlee
  • paschen
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
  1. 请不要发布与C++技术无关的贴子
  2. 请不要发布与技术无关的招聘、广告的帖子
  3. 请尽可能的描述清楚你的问题,如果涉及到代码请尽可能的格式化一下

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