简体   繁体   English

c ++中字符串的这两个不同初始化之间的区别是什么?

[英]what's the difference between these two different initialization for a string in c++?

The source code 源代码

#include <iostream>
#include <string>
using namespace std;
int main(){
    std::string s{'a', 'b', '\0', 'c'};
    std::string s1="ab\0c";
    cout<<s.size()<<" "<<s<<endl;
    cout<<s1.size()<<" "<<s1<<endl;
    return 0;
}

and the output is 而输出是

4 abc
2 ab

I wonder why this phenomenon occurs and are there any difference between these two types of initialization in C++? 我想知道为什么会出现这种现象,这两种类型的初始化在C ++中有什么区别吗? Thanks. 谢谢。

For s you're matching the constructor that accepts an initialiser-list of characters: that's (9) in the list here . 对于s你匹配接受初始化字符列表的构造函数: 这里的列表中的(9)。 The string class lets you construct strings from arbitrary data which may include embedded NULs, as it does in this case. string类允许您从可能包含嵌入式NUL的任意数据构造字符串,就像在这种情况下一样。 The initialiser list knows its own length, so the string captures all the characters. 初始化列表知道自己的长度,因此string捕获所有字符。

For s1 , the matching constructor is (5) in the above-linked list, which accepts a const char* - the compiler lets the array of char provided decay to such a pointer before calling that constructor, which means the constructor has no knowledge of the length of the array. 对于s1 ,匹配构造函数是上面链接列表中的(5),它接受一个const char* - 编译器允许char调用数组在调用该构造函数之前衰减到这样的指针,这意味着构造函数不知道数组的长度。 Instead, it assumes you're deliberately using the ASCIIZ NUL-terminated string convention (as in "C" strings), and scans through the data to find the first NUL, considering that the terminator. 相反,它假设您故意使用ASCIIZ NUL终止的字符串约定(如“C”字符串),并扫描数据以找到第一个NUL,考虑到终结符。 Consequently, only 2 characters are captured in the string. 因此,字符串中只捕获了2个字符。

Note that you can explicitly capture 4 characters with... 请注意,您可以使用...明确捕获4个字符

std::string s1 { "ab\0c", 4};

...which matches constructor (4) in the list. ...匹配列表中的构造函数(4)。

Rakete1111's comment below illustrates another, newer way to create such strings: auto s1 = "ab\\0c"s; 下面的Rakete1111评论说明了另一种创建这种字符串的新方法: auto s1 = "ab\\0c"s; .

The reason is that std::strings are not first class objects, they are standard library objects and have to obey the rules of C++ syntax. 原因是std :: strings不是第一类对象,它们是标准库对象,必须遵守C ++语法规则。 Unlike string literals which are first class constructs. 与作为第一类构造的字符串文字不同。

An std::string is allowed embedded nuls, so when it is initialised to an array, it sees the whole of the array. std :: string允许嵌入nuls,因此当它初始化为数组时,它会看到整个数组。 When it is initialised to a string literal, it sees a char *, and its only way of detecting string end is to search for the null. 当它初始化为字符串文字时,它会看到一个char *,它检测字符串结尾的唯一方法是搜索null。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 C ++中的两个副本构造函数调用之间有什么区别? - what's the difference between the two copy constructor invocation in C++? 这两个 C++ 语句连接字符串有什么区别? - What is the difference between these two C++ statements to concatenate a string? C ++中的“ ABC”和字符串(“ ABC”)有什么区别? - What's the difference between “ABC” and string(“ABC”) in C++? 对于C++向量初始化,“向量”有什么区别<int> v = n;”和“向量<int> v(n);"</int></int> - For C++ vector initialization, what's the difference between "vector<int>v = n;" and "vector<int>v(n);" 这两种在C++中存储字符串的方式有什么不同 - What is different between these two ways to store string in C++ 这两个参数在C ++中有什么区别? - What is the difference between these two parameters in C++? 这两者在c ++中有什么区别 - what is the difference between these two in c++ C ++中的string.function()或function(string)有什么区别? - What's the difference between string.function() or function(string) in C++? C++ 中传递字符串 S 和字符串 S[ ] 之间的区别 - Difference between passing string S and string S[ ] in C++ 在C ++中,“C”函数和“C”链接之间有什么区别? - In C++, what's the difference between a “C” function and a “C” linkage?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM