Understanding C ++ Strings

I am trying to understand how strings really work in C ++ because I was just confused after unexpected behavior.

Given a string, I insert a character (without using append()) using the operator []:

string str;
str[0] = 'a';

Let print a line:

cout << "str:" << str << endl;

I get NULL as output:

str:

Ok, try typing a single character on a line:

cout << "str[0]:" << str[0] << endl;

Conclusion:

str[0]:a

Q1. What happened there? Why was anot printed in the first case?

Now I am doing something that should cause a compilation error, but it is not, and my question is again, why.

str = 'ABC';

Q2. How is this not incorrect semantics, that is, to assign a character (which is not really a character, but essentially a string in single quotes), into a string?

, , i.e C ( , A):

cout << "str:" << str << endl;

:

str:C

Q3. , ?

+4
4

, ( append()), []:

string str;
str[0] = 'a';

. operator[](size_type pos) pos. pos == size(), undefined. , size() == 0 , , str[0] undefined.

Q1. ? ?

undefined.


-, , , , .

str = 'ABC';

Q2. , ... ?

. .

Q2.... ( , ​​ )...

. int. , .

, int. int char, , char.

char , int , . char , undefined.


Q3. , ?

. , , . , , char, , , int.


. GCC:

: [-Wmultichar]

 str = 'ABC';

: [-Woverflow]


str[0] = 'a' , char str[] = "" ( , ). , [] , ?

std::string.

char str[] = "";

1, . , :

str[0] = 'a';

. str , undefined:

out << "str:" << str << endl; // oops, str is not a null terminated string

, std::string , - std::string. . , , .

+5

http://en.cppreference.com/w/cpp/string/basic_string/operator_at. , "If pos == size(), undefined."

:

string str;

size() 0.

+2

str string; str[0]='a' - undefined, " ++ 11" " ++ 11 on". , str . , ++ 11 (read) , str[pos] pos == size() str , undefined. ++ 11, ( '\0'. undefined. Cpp std:: basic_string:: operator_at.

, , ; ( ):

string str = "bbbb";

const char* cstr = str.data();
printf("adress: %p; content:%s\n", cstr, cstr);
// yields "adress: 0x7fff5fbff5d9; content:bbbb"

str[0] = 'a';
const char* cstr2 = &str[0];
printf("adress: %p; content:%s\n", cstr2, cstr2);
// yields "adress: 0x7fff5fbff5d9; content:abbb"

cout << "str:" << str << endl;
// yields "str:abbb"

, , str.data() , str.data() , &str[0].

string str = "", ( undefined, , ):

string str;  // is the same as string str = ""

const char* cstr = str.data();
printf("adress: %p; content:%s\n", cstr, cstr);
// yields "adress: 0x7fff5fbff5c1; content:"

str[0] = 'a';
const char* cstr2 = &str[0];
printf("adress: %p; content:%s\n", cstr2, cstr2);
// yields "adress: 0x7fff5fbff5c1; content:a"

cout << "str:" << str << endl;
// yields "str:"

, str.data() , &str[0], 'a' ( , , , , , ). str.data() a ( , 'a' , char). str[0]='a' , 0, cout << str .

, -.

+2
string str;

0.

str[0] = 'a';

'a'. , 0. , "", , - .

cout << "str:" << str << endl;

. 0, .

cout << "str[0]:" << str[0] << endl;

undefined "a". , undefined. , , undefined.

str = 'ABC';

, , , , , , , , , .

cout << "str:" << str << endl;

Your guess is as good as mine, which the compiler will do, since it str = 'ABC';was logically incorrect (albeit syntactically correct). The compiler seems to be truncated ABCto the last character, since putting 257 in an 8-bit integer can only save the least significant bit.

+1
source

Source: https://habr.com/ru/post/1665779/


All Articles