What is the data type of one character in a string as an array?

string s;
cin>>s;

suppose s = "stackoverflow"

now, if we get access to s[3], it should issue'c'

will s[3]be 'c'or "c"?

like in a char file or string data?

+4
source share
3 answers

std::stringis not a built-in type, therefore the operator []in s[3]is a call to the member function that defines this operator in the string template.

You can find this type by looking at the man page foroperator [] :

Returns a reference to the character at the specified location pos.

reference const_reference , . " " std::basic_string<CharT>.

std::string 1, 3, substr:

s.substr(3, 1); // This produces std::string containing "c"
+8

, [] std::string

char& operator[] (size_t pos);
const char& operator[] (size_t pos) const;

s [3] be 'c' "c"?

'c', "c".

+8

, std::string char is, class, chars .

std::string C [], char , :

s[3] 'c' "c"?

: 'c'

+1

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


All Articles