为什么 .c_str() 不在字符串末尾添加 '\0'?
Why is .c_str() not adding '\0' at the end of a string?
当运行这个代码我得到"Hello Worldaaa"
,但是根据.c_str()
函数的工作,string a
应该是"Hello World[=15=]"
和string b
应该是 "Hello World[=17=]aaa"
所以 "aaa"
不应该出现在输出中。
#include <bits/stdc++.h>
using namespace std;
int main()
{
string a = "Hello World";
a = a.c_str();
string b = a + string("aaa");
cout << b;
return 0;
}
这是不直观的。请记住,终止空字符未被用户显式使用。语言的语法添加了它。
如果 "abcd" + "xyz"
的结果最终是 "abcd[=11=]xyz"
而不是直观的结果 - "abcdxyz"
.
,将会造成很多混乱
在 C 和 C++ 中,连接两个字符串意味着忽略结果字符串中第一个字符串的终止空字符。查看 strcat
的文档。空字符是明确的,而 std::string::operator+
的文档不是。
不太...
我们来看看这一行:
a = a.c_str();
您有一个 std::string
,您正试图将 const char*
分配给它。因此,您将使用 the std::string
assignment operator 的重载 (3)。但这并没有神奇地改变 a
的类型。也就是说,我们还有一个std::string
!
因此该行实际上根本没有修改 a
的值。
然后使用 operator+
将两个 std::string
相加。这将导致另一个 std::string
,即第一个附加第二个,这正是您所拥有的!
我不清楚你想在这里完成什么,但如果你用这些值创建了一个 c 字符串:
const char cstr[] = "Hello World[=11=]aaa";
并尝试打印它,您会看到您所期待的。
是的,你是对的
std::string::c_str.
的确如此,
Returns a pointer to a null-terminated character array with data equivalent to those stored in the string.
但是,有一些注意事项可能会误导您。
首先声明:
a = a.c_str();
在语义上是一个无操作。
您得到 const char*
(即 "Hello World[=14=]"
)并将其分配给 a
。
但是 a
是一个 std::string
,它是一个 class,旨在抽象出 C++ 中的 "string type"。它透明地自动处理 '[=18=]'
。用户不应该关心它的管理1.
诀窍就在里面std::string::operator=(const char*)
.
Replaces the contents with those of null-terminated character string pointed to by s as if by assign(s, Traits::length(s)).
最后一点,约string concatenation.
a + string("aaa");
如前所述,在这种情况下,运算符 std::string::operator+
将处理 '[=18=]'
。
很快,它的行为如下:
"Hello World[=12=]" + "aaa[=12=]" ===> "Hello Worldaaa[=12=]"
它将关心 "inner '[=18=]'
" 返回符合标准的空终止字符串。
1除非he/she在玩内存
当运行这个代码我得到"Hello Worldaaa"
,但是根据.c_str()
函数的工作,string a
应该是"Hello World[=15=]"
和string b
应该是 "Hello World[=17=]aaa"
所以 "aaa"
不应该出现在输出中。
#include <bits/stdc++.h>
using namespace std;
int main()
{
string a = "Hello World";
a = a.c_str();
string b = a + string("aaa");
cout << b;
return 0;
}
这是不直观的。请记住,终止空字符未被用户显式使用。语言的语法添加了它。
如果 "abcd" + "xyz"
的结果最终是 "abcd[=11=]xyz"
而不是直观的结果 - "abcdxyz"
.
在 C 和 C++ 中,连接两个字符串意味着忽略结果字符串中第一个字符串的终止空字符。查看 strcat
的文档。空字符是明确的,而 std::string::operator+
的文档不是。
不太...
我们来看看这一行:
a = a.c_str();
您有一个 std::string
,您正试图将 const char*
分配给它。因此,您将使用 the std::string
assignment operator 的重载 (3)。但这并没有神奇地改变 a
的类型。也就是说,我们还有一个std::string
!
因此该行实际上根本没有修改 a
的值。
然后使用 operator+
将两个 std::string
相加。这将导致另一个 std::string
,即第一个附加第二个,这正是您所拥有的!
我不清楚你想在这里完成什么,但如果你用这些值创建了一个 c 字符串:
const char cstr[] = "Hello World[=11=]aaa";
并尝试打印它,您会看到您所期待的。
是的,你是对的 std::string::c_str.
的确如此,
Returns a pointer to a null-terminated character array with data equivalent to those stored in the string.
但是,有一些注意事项可能会误导您。
首先声明:
a = a.c_str();
在语义上是一个无操作。
您得到 const char*
(即 "Hello World[=14=]"
)并将其分配给 a
。
但是 a
是一个 std::string
,它是一个 class,旨在抽象出 C++ 中的 "string type"。它透明地自动处理 '[=18=]'
。用户不应该关心它的管理1.
诀窍就在里面std::string::operator=(const char*)
.
Replaces the contents with those of null-terminated character string pointed to by s as if by assign(s, Traits::length(s)).
最后一点,约string concatenation.
a + string("aaa");
如前所述,在这种情况下,运算符 std::string::operator+
将处理 '[=18=]'
。
很快,它的行为如下:
"Hello World[=12=]" + "aaa[=12=]" ===> "Hello Worldaaa[=12=]"
它将关心 "inner '[=18=]'
" 返回符合标准的空终止字符串。
1除非he/she在玩内存