注册 登录
编程论坛 C++教室

[求助]关于string类的一些问题

ljhwahaha 发布于 2007-04-08 13:20, 653 次点击

这是书本上的代码`` 我想问的是红字部分,为什么要用常应用呢,似乎直接用值传递也可以达到同样的效果;
还有书上的运行结果是
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编辑过]

3 回复
#2
Satyr2007-04-08 13:54
string函数了解不多
capacity是什么
#3
yuyunliuhen2007-04-08 14:01
以下是引用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编辑过]

#4
ljhwahaha2007-04-09 09:59
回复:(yuyunliuhen)以下是引用ljhwahaha在2007-4-8...

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

1