有趣的string.resize行为 [英] Interesting string.resize behavior
问题描述
#include< string>
#include< iostream>
使用命名空间std;
int main( )
{
string str;
str.resize(5);
str [ 0] =''t'';
str [1] =''e'';
str [2] =''s'';
str [3] =''t'';
str [4] =''\ 0'';
str + =" -test2";
str + =" -test3";
cout<< str<< endl;
返回0;
}
#include <string>
#include <iostream>
using namespace std;
int main()
{
string str;
str.resize(5);
str[0] = ''t'';
str[1] = ''e'';
str[2] = ''s'';
str[3] = ''t'';
str[4] = ''\0'';
str += "-test2";
str += "-test3";
cout << str << endl;
return 0;
}
推荐答案
v4vijayakumar写道:
v4vijayakumar wrote:
#include< string>
#include< iostream>
using namespace std;
int main()
{
string str;
str.resize( 5);
str [0] =''t'';
str [1] =''e'';
str [2] =''s'';
str [3] =''t'';
str [4] =''\ 0'';
str + =" -test2";
str + =" -test3";
cout<< str<<结束;
返回0;
}
#include <string>
#include <iostream>
using namespace std;
int main()
{
string str;
str.resize(5);
str[0] = ''t'';
str[1] = ''e'';
str[2] = ''s'';
str[3] = ''t'';
str[4] = ''\0'';
str += "-test2";
str += "-test3";
cout << str << endl;
return 0;
}
你发现什么有趣关于它?字符串附加
到它的* end *保留它已经拥有的所有字符,而不是
"最后一个字符,然后尾随空字符。
我猜你发现这个行为不同于
a C string。是的,这是不同的。由于C字符串不能以其他方式知道其大小,因此必须跟踪空字符
(因为它们被认为是*终止*)。 C ++''std :: string''具有
对于null字符没有特殊含义。它以不同的大小跟踪
。
V
-
请删除资金''A'在通过电子邮件回复时
我没有回复最热门的回复,请不要问
What do you find "interesting" about it? The string is appended
to its *end* keeping all characters that it already has, not to the
"last character before trailing null characters".
I am guessing that you find this behaviour different from that of
a C string. Yes, it''s different. Since a C string cannot have any
other way of knowing its size, it has to keep track of the null chars
(since they are considered *terminating*). The C++ ''std::string'' has
no such special meaning for the null character. It keeps track of
its size differently.
V
--
Please remove capital ''A''s when replying by e-mail
I do not respond to top-posted replies, please don''t ask
你将不得不说出你发现的确切内容
有趣的是这个。
2007-06-21 09:31 :50 -0700,v4vijayakumar
< vi ****************** @ gmail.comsaid:
You''re going to have to state exactly what it is that you find
interesting about this.
On 2007-06-21 09:31:50 -0700, v4vijayakumar
<vi******************@gmail.comsaid:
#include< string>
#include< iostream>
使用命名空间std;
int main()
{
string str;
str.resize(5);
str [0] =''t'';
str [1] =''e'';
str [2] =''s'';
str [3] =''t'';
str [4] =''\ 0'';
#include <string>
#include <iostream>
using namespace std;
int main()
{
string str;
str.resize(5);
str[0] = ''t'';
str[1] = ''e'';
str[2] = ''s'';
str[3] = ''t'';
str[4] = ''\0'';
好的,str现在包含字符串test \0。
OK, str now contains the string "test\0".
>
str + =" -test2";
>
str += "-test2";
str现在包含字符串test\0-test2。
str now contains the string "test\0-test2".
str + =" -test3" ;;
str += "-test3";
str现在包含字符串test\0-test2-test3。
str now contains the string "test\0-test2-test3".
cout<< str<<结束;
返回0;
}
cout << str << endl;
return 0;
}
-
Clark S. Cox III
cl*******@gmail.com
--
Clark S. Cox III
cl*******@gmail.com
6月21日,9:49 pm,Clark Cox< clarkc ... @ gmail.comwrote:
On Jun 21, 9:49 pm, Clark Cox <clarkc...@gmail.comwrote:
你必须准确说明你发现的是什么
有趣的。
You''re going to have to state exactly what it is that you find
interesting about this.
...
string str;
str.resize(5);
str [0] =''t'';
str [1] =''e'';
str [2] = 's';;
str [3] =''t'';
str [4] =''\ 0'';
str + =" -test2";
str + =" -test3";
cout<< str<< endl;
...
...
string str;
str.resize(5);
str[0] = ''t'';
str[1] = ''e'';
str[2] = ''s'';
str[3] = ''t'';
str[4] = ''\0'';
str += "-test2";
str += "-test3";
cout << str << endl;
...
好吧,惊喜!
输出不是,test-test1-test2,而只是测试。 :)
[在MS VS 6.0中尝试过。 ]
Well, surprise!
Output is not, "test-test1-test2", but just "test". :)
[ Tried in MS VS 6.0. ]
这篇关于有趣的string.resize行为的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!