string :: resize的问题 [英] problem with string::resize

查看:84
本文介绍了string :: resize的问题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在下面的代码中:


string foo(" foo");


foo.resize(10);

foo.append(" bar");


cout<< foo<< ENDL; //输出是foo


我希望输出为foobar,尽管bar是bar。如果resize()重置字符串,也可以理解为
。但是foo?

" bar"去?

谢谢,

Metaosp

解决方案



" metaosp" <我***** @ gmail.com> skrev i meddelandet

新闻:11 ********************** @ localhost.localdomai n ...

在下面的代码中:

string foo(" foo");

foo.resize(10);
foo.append(" ; bar");

cout<< foo<< ENDL; //输出是foo

我希望输出是foobar,尽管bar是bar。如果resize()重置字符串也是可以理解的。但是foo?
bar在哪里go?




我希望输出为foo \\\\\\\\\\ 0 + 0 。


调用foo.resize(10)会将足够的string :: value_type()附加到

达到要求的大小。


如何检查结果?有什么东西在

之后停止''\ 0''?

Bo Persson


< blockquote> metaosp写道:

在下面的代码中:

string foo(" foo");

foo.resize(10 );
foo.append(" bar");

cout<< foo<< ENDL; //输出是foo

我希望输出是foobar,尽管bar是bar。如果resize()重置字符串也是可以理解的。但是foo?
bar在哪里go?

谢谢,
Metaosp




删除foo.resize(10);行。

没有必要,它将nul字节(''\ 0'')

附加到foo的末尾。使总大小为10.当你

追加bar时它被附加在nul字节之后。

与C字符串不同,C ++ std :: string不需要在附加之前调整大小
(它自己处理它) ,

和nul字节(''\''')在

std :: string中没有特殊含义。 C ++ std :: string可以包含二进制数据;

使用std :: string的length()成员查询字符串的

长度(即使它包含二元数据)。


问候,

拉里


On Sun,2006-02-12在18:59 +0100,Bo Persson写道:

" metaosp" <我***** @ gmail.com> skrev i meddelandet
新闻:11 ********************** @ localhost.localdomai n ...

在以下代码中:

string foo(" foo");

foo.resize(10);
foo.append(" bar") ;

cout<< foo<< ENDL; //输出是foo

我希望输出是foobar,尽管bar是bar。如果resize()重置字符串也是可以理解的。但是foo?
bar在哪里go?



我希望输出为foo \ 0 \ 0 \ 0 \ 0 \ 0 \ 0 \ 0bar。

调用foo.resize(10)将附加足够的string :: value_type()以达到所需的大小。

如何检查结果?有什么东西在
第一次''\ 0'后停止?




有趣的是它确实是foo \\\\ 0\0\0\0\0bar" ;.我想我需要使用

reserve()而不是resize()。谢谢:)


Metaosp



Hi, in the following code:

string foo("foo");

foo.resize(10);
foo.append("bar");

cout << foo << endl; // output is "foo"

I expect the output to be "foobar", although "bar" is also
understandable if resize() reset the string. But "foo"? Where does
"bar" go?
Thanks,
Metaosp

解决方案


"metaosp" <me*****@gmail.com> skrev i meddelandet
news:11**********************@localhost.localdomai n...

Hi, in the following code:

string foo("foo");

foo.resize(10);
foo.append("bar");

cout << foo << endl; // output is "foo"

I expect the output to be "foobar", although "bar" is also
understandable if resize() reset the string. But "foo"? Where does
"bar" go?



I would expect the output to be "foo\0\0\0\0\0\0\0bar".

The call foo.resize(10) will append enough string::value_type() to
reach the requested size.

How are you checking the result? With something that stops after the
first ''\0'' ?
Bo Persson


metaosp wrote:

Hi, in the following code:

string foo("foo");

foo.resize(10);
foo.append("bar");

cout << foo << endl; // output is "foo"

I expect the output to be "foobar", although "bar" is also
understandable if resize() reset the string. But "foo"? Where does
"bar" go?
Thanks,
Metaosp



Delete the "foo.resize(10);" line.
It is not necessary, and it appends nul bytes (''\0'')
to the end of "foo" to make the total size 10. When you
append "bar" it is appended after the nul bytes.
Unlike C strings, the C++ std::string does not have to
be resized before appending (it handles that on its own),
AND nul bytes (''\0'') have no special meaning in a
std::string. A C++ std::string can contain binary data;
Use the length() member of std::string to query the
length of the string (even if it contains binary data).

Regards,
Larry


On Sun, 2006-02-12 at 18:59 +0100, Bo Persson wrote:

"metaosp" <me*****@gmail.com> skrev i meddelandet
news:11**********************@localhost.localdomai n...

Hi, in the following code:

string foo("foo");

foo.resize(10);
foo.append("bar");

cout << foo << endl; // output is "foo"

I expect the output to be "foobar", although "bar" is also
understandable if resize() reset the string. But "foo"? Where does
"bar" go?



I would expect the output to be "foo\0\0\0\0\0\0\0bar".

The call foo.resize(10) will append enough string::value_type() to
reach the requested size.

How are you checking the result? With something that stops after the
first ''\0'' ?



Interesting it is indeed "foo\0\0\0\0\0\0\0bar". I think I need to use
reserve() instead of resize(). Thanks :)

Metaosp



这篇关于string :: resize的问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆