标题:[求助]关于string类的一些问题
取消只看楼主
ljhwahaha
Rank: 1
等 级:新手上路
帖 子:28
专家分:0
注 册:2007-3-13
 问题点数:0 回复次数:1 
[求助]关于string类的一些问题

这是书本上的代码`` 我想问的是红字部分,为什么要用常应用呢,似乎直接用值传递也可以达到同样的效果;
还有书上的运行结果是
size:0
length:0
capacity:0
max size:4294967293
size:16
length:16
capacity:31
max size:4294967293
size:10
length:10
capacity:31
max size:4294967293

但我运行后的结果是
size:0
length:0
capacity:15
max size:4294967294
size:16
length:16
capacity:31
max size:4294967294
size:10
length:10
capacity:15
max size:4294967294
(我用的是vc++2005)为什么会不一样呢?


#include <iostream>
#include <string>
using namespace std;
void PrintAttribute(congst string &str);

void main()
{
string s1,s2;
PrintAttribute(s1);
s1="My string object";
PrintAttribute(s1);
s2="new string";
PrintAttribute(s2);
system("pause");

}
void PrintAttribute(const string &str)
{
cout<<"size:"<<str.size()<<endl; //返回串长
cout<<"length:"<<str.length()<<endl; //返回串长
cout<<"capacity:"<<str.capacity()<<endl; //返回容量
cout<<"max size:"<<str.max_size()<<endl; //返回最大允许串长

}

[此贴子已经被作者于2007-4-8 14:03:27编辑过]

搜索更多相关主题的帖子: string 
2007-04-08 13:20
ljhwahaha
Rank: 1
等 级:新手上路
帖 子:28
专家分:0
注 册:2007-3-13
得分:0 
回复:(yuyunliuhen)以下是引用ljhwahaha在2007-4-8...

那红字部分那个问题呢??

2007-04-09 09:59



参与讨论请移步原网站贴子:https://bbs.bccn.net/thread-130119-1-1.html




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

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