| 网站首页 | 业界新闻 | 小组 | 威客 | 人才 | 下载频道 | 博客 | 代码贴 | 在线编程 | 编程论坛
欢迎加入我们,一同切磋技术
用户名:   
 
密 码:  
共有 649 人关注过本帖
标题:[求助]关于string类的一些问题
只看楼主 加入收藏
ljhwahaha
Rank: 1
等 级:新手上路
帖 子:28
专家分:0
注 册:2007-3-13
收藏
 问题点数:0 回复次数:3 
[求助]关于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
Satyr
Rank: 1
等 级:新手上路
帖 子:83
专家分:0
注 册:2006-4-7
收藏
得分:0 
string函数了解不多
capacity是什么

C++的博大精深让我叹服
2007-04-08 13:54
yuyunliuhen
Rank: 6Rank: 6
等 级:贵宾
威 望:20
帖 子:1435
专家分:0
注 册:2005-12-12
收藏
得分:0 
以下是引用ljhwahaha在2007-4-8 13:20:26的发言:

这是书本上的代码`` 我想问的是红字部分,为什么要用常应用呢,似乎直接用值传递也可以达到同样的效果;
还有书上的运行结果是
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 //在VC++6.0下调试和这个相同

但我运行后的结果是
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)为什么会不一样呢?//在VC++2005下调试会出现错误.


#include <iostream>
#include <string>
using namespace std;
void PrintAttribute(const string &str); //consgt ?

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;

}

下面是MSDN中查到的有关capacity 资料

Standard C++ Library Reference
basic_string::capacity

Returns the largest number of elements that could be stored in a string without increasing the memory allocation of the string.

size_type capacity( ) const;

Return Value
The size of storage currently allocated in memory to hold the string
.


Remarks
The member function returns the storage currently allocated to hold the controlled sequence, a value at least as large as size.


Example
Copy Code
// basic_string_capacity.cpp
// compile with: /EHsc
#include <string>
#include <iostream>

int main( )
{
using namespace std;
string str1 ("Hello world");
cout << "The original string str1 is: " << str1 << endl;

// The size and length member functions differ in name only
basic_string <char>::size_type sizeStr1, lenStr1;
sizeStr1 = str1.size ( );
lenStr1 = str1.length ( );

basic_string <char>::size_type capStr1, max_sizeStr1;
capStr1 = str1.capacity ( );
max_sizeStr1 = str1.max_size ( );

// Compare size, length, capacity & max_size of a string
cout << "The current size of original string str1 is: "
<< sizeStr1 << "." << endl;
cout << "The current length of original string str1 is: "
<< lenStr1 << "." << endl;
cout << "The capacity of original string str1 is: "
<< capStr1 << "." << endl;
cout << "The max_size of original string str1 is: "
<< max_sizeStr1 << "." << endl << endl;

str1.erase ( 6, 5 );
cout << "The modified string str1 is: " << str1 << endl;

sizeStr1 = str1.size ( );
lenStr1 = str1.length ( );
capStr1 = str1.capacity ( );
max_sizeStr1 = str1.max_size ( );

// Compare size, length, capacity & max_size of a string
// after erasing part of the original string
cout << "The current size of modified string str1 is: "
<< sizeStr1 << "." << endl;
cout << "The current length of modified string str1 is: "
<< lenStr1 << "." << endl;
cout << "The capacity of modified string str1 is: "
<< capStr1 << "." << endl;
cout << "The max_size of modified string str1 is: "
<< max_sizeStr1 << "." << endl;
}

Sample Output
The original string str1 is: Hello world
The current size of original string str1 is: 11.
The current length of original string str1 is: 11.
The capacity of original string str1 is: 15.
The max_size of original string str1 is: 4294967294.

The modified string str1 is: Hello
The current size of modified string str1 is: 6.
The current length of modified string str1 is: 6.
The capacity of modified string str1 is: 15.
The max_size of modified string str1 is: 4294967294.

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


Go confidently in the  directions of your dreams,live the life you have imagined!Just do it!
It is no use learning without thinking!
2007-04-08 14:01
ljhwahaha
Rank: 1
等 级:新手上路
帖 子:28
专家分:0
注 册:2007-3-13
收藏
得分:0 
回复:(yuyunliuhen)以下是引用ljhwahaha在2007-4-8...

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

2007-04-09 09:59
快速回复:[求助]关于string类的一些问题
数据加载中...
 
   



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

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