string s;
cin>>s;
suppose s = "*"
假设s =“*”
now if we access s[3]
, it should give out 'c'
现在,如果我们访问s [3],它应该给出'c'
will s[3]
be 'c'
or "c"
?
将[3]成为'c'或“c”?
as in will it be a char data type or string data type?
它会是char数据类型还是字符串数据类型?
3 个解决方案
#1
8
std::string
is not a built-in type, so operator []
in s[3]
is a call to a member function defining this operator in the string template.
std :: string不是内置类型,因此s [3]中的operator []是对在字符串模板中定义此运算符的成员函数的调用。
You can find the type by looking up the reference page for operator []
:
您可以通过查找operator []的参考页面找到该类型:
Returns a reference to the character at specified location pos.
返回对指定位置pos处的字符的引用。
To look up the type reference
and const_reference
from the documentation see "member types" section of std::basic_string<CharT>
template.
要从文档中查找类型引用和const_reference,请参阅std :: basic_string
If you are looking for std::string
of length 1 starting at location 3, use substr
instead:
如果您要查找从位置3开始的长度为1的std :: string,请改用substr:
s.substr(3, 1); // This produces std::string containing "c"
#2
8
It returns reference to the character as the operator []
is overloaded for std::string
当st_ :: string重载operator []时,它返回对字符的引用
char& operator[] (size_t pos);
const char& operator[] (size_t pos) const;
will s[3] be 'c' or "c"?
将[3]成为'c'或“c”?
Character 'c', not string "c".
字符'c',而不是字符串“c”。
#3
1
It is easiest to remember that std::string
is not a native type like char
is, but a wrapper class
that contains an array of chars
to form a string.
最简单的方法是记住std :: string不是像char这样的本机类型,而是一个包含字符数组的包装类,用于形成字符串。
std::string
simply overloads the C array operator [] to return the char
at a given index, hence:
std :: string只是重载C数组operator []以返回给定索引处的char,因此:
will
s[3]
be'c'
or"c"
?将[3]成为'c'或“c”?
Answer:
'c'
#1
8
std::string
is not a built-in type, so operator []
in s[3]
is a call to a member function defining this operator in the string template.
std :: string不是内置类型,因此s [3]中的operator []是对在字符串模板中定义此运算符的成员函数的调用。
You can find the type by looking up the reference page for operator []
:
您可以通过查找operator []的参考页面找到该类型:
Returns a reference to the character at specified location pos.
返回对指定位置pos处的字符的引用。
To look up the type reference
and const_reference
from the documentation see "member types" section of std::basic_string<CharT>
template.
要从文档中查找类型引用和const_reference,请参阅std :: basic_string
If you are looking for std::string
of length 1 starting at location 3, use substr
instead:
如果您要查找从位置3开始的长度为1的std :: string,请改用substr:
s.substr(3, 1); // This produces std::string containing "c"
#2
8
It returns reference to the character as the operator []
is overloaded for std::string
当st_ :: string重载operator []时,它返回对字符的引用
char& operator[] (size_t pos);
const char& operator[] (size_t pos) const;
will s[3] be 'c' or "c"?
将[3]成为'c'或“c”?
Character 'c', not string "c".
字符'c',而不是字符串“c”。
#3
1
It is easiest to remember that std::string
is not a native type like char
is, but a wrapper class
that contains an array of chars
to form a string.
最简单的方法是记住std :: string不是像char这样的本机类型,而是一个包含字符数组的包装类,用于形成字符串。
std::string
simply overloads the C array operator [] to return the char
at a given index, hence:
std :: string只是重载C数组operator []以返回给定索引处的char,因此:
will
s[3]
be'c'
or"c"
?将[3]成为'c'或“c”?
Answer:
'c'