简体   繁体   English

有关字符和字符串的C ++

[英]C ++ about char and string

what is char ctemp = ' '; 什么是char ctemp = ' '; and string stemp = ""; string stemp = ""; means? 手段? when they put ' ' and " " inside without writing anything inside? 当他们将' '" "放入其中而没有在其中写任何东西时? Help please! 请帮忙! Will appreciate who answer it. 会感谢谁回答的。

Single quotes ( ' ) indicate a character literal: a single character. 单引号( ' )表示字符文字:单个字符。 Double quotes ( " ) denote a string literal, ie: an array of characters. 双引号( " )表示字符串文字,即:字符数组。

' ' is a single space character, while " " is a single space character followed by a null terminator, as is customary for C-style strings. ' '是单个空格字符,而" "是单个空格字符,后跟一个空终止符,这是C样式字符串的习惯用法。

Character literals are directly assignable to char variables. 字符文字可以直接分配给char变量。

The type of a string literal is const char[N] , where N is the length of the literal, including the null terminator. 字符串文字的类型为const char[N] ,其中N是文字的长度,包括空终止符。 In C and C++, a static array decays to (is implicitly convertible to) a pointer to the first element, and std::string is constructible from a const char * pointer (see constructor (5)), which in C usually means a pointer to an array of characters terminated by a null terminator. 在C和C ++中,静态数组会衰减为(可隐式转换为)指向第一个元素的指针,并且std::string const char *指针构造(请参见构造函数(5)),在C中通常意味着指向以空终止符终止的字符数组的指针。

The char ctemp = ' ' will put the value ' ' (32 in ASCII decimal) inside the ctemp variable. char ctemp = ' '将把值' ' (32 in ASCII decimal)放入ctemp变量中。
The string stemp = ""; string stemp = ""; will create an empty string in stemp . 将在stemp创建一个空字符串。

Here 这里

char ctemp = ' ';

you are assigning a whitespace character ' ' to ctemp . 您正在为ctemp分配一个空格字符' '

Here 这里

string stemp = "";

the initializer "" creates a empty string. 初始化程序""创建一个空字符串。

' ' is the space character. ' '是空格字符。 "" is an empty string. ""是一个空字符串。 " " is a string that contains only the space character. " "是仅包含空格字符的字符串。 Note that a statement like string stemp = "" implicitly invokes the string(char const *) constructor to create a new string instance from a char const * pointer. 请注意,类似string stemp = ""的语句会隐式调用string(char const *)构造函数以从char const *指针创建新的字符串实例。

the first one means a "whitespace" like when you write something and need to divide the words with the space key. 第一个意思是一个“空白”,就像您写一些东西并需要用空格键将单词分开一样。 That empty space is still part of the string and so you can say your char is only that empty space. 该空格仍然是字符串的一部分,因此您可以说char只是该空格。

The second one is of type string but it is even less than a white space. 第二个是字符串类型,但它甚至小于空格。 It is a completely empty string. 它是一个完全空的字符串。

string is array(collection) of char 字符串是char的array(collection)

ctemp = ' ' 

mean whitespace character 平均空白字符

stemp = ""

mean empty string no character in string 表示空字符串,字符串中没有字符

you can put ' ' to char variable. 您可以将''放入char变量。 you can put " " to array of char. 您可以将“”放入char数组。

The answers above only state about the space literal, but I think OP wants to ask why people write something like 上面的答案仅说明了空间字面量,但我认为OP想要问为什么人们写这样的东西
string temp ="" ;

The answer is that it initializes a string object. 答案是它初始化了一个字符串对象。 If you would write 如果你愿意写
string temp;
You would get undefined behaviour if you would try to access it since it isn't initialized. 如果由于未初始化而尝试访问它,将会得到未定义的行为。 Initializing an empty string let's you concatenate it with something easily. 初始化一个空字符串让我们轻松地将它与某些东西连接起来。 Concatenating an empty string with another string is basically a replace. 将空字符串与另一个字符串连接起来基本上是一种替换。

It's a bit different for char since it is a primitive type and not an object. 对于char来说有点不同,因为它是原始类型而不是对象。 Writing 写作
char tmp; already initializes it to the default value of \\0 . 已经将其初始化为默认值\\0 chars can be used right away. 字符可以立即使用。

In C++ the single quote is used to identify the single character, and double quotes are used for string literals. 在C ++中,单引号用于标识单字符,双引号用于字符串文字。 A string literal “x” is a string, it is containing character 'x' and a null terminator '\\0'. 字符串文字“ x”是一个字符串,它包含字符“ x”和空终止符“ \\ 0”。 So “x” is two-character array in this case. 因此,在这种情况下,“ x”是两个字符的数组。

Some Examples:
string s = "" ; => empty string
char s =' ' ;   => space (you should have only one character inside the single quotes)
string s = " " ; => space followed by '\0' character (two character array)

Whitespace character and empty string. 空格字符和空字符串。 You can see the string as a sequence of characters, but they are two different types 您可以将字符串视为字符序列,但是它们是两种不同的类型

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM