C++ 字符串超出范围c++;

C++ 字符串超出范围c++;,c++,C++,当我尝试检查str[index]是否等于时,我得到异常字符串超出范围 std::string Test::getTheText(std::string str){ int指数=7; 字符串文本; 库特 为什么下面的代码有效 而只有到那时你才会陷入: […],它会导致未定义的行为 C++标准参考: §21.4.5基本字符串元素访问[string.access] const_reference operator[](size_type pos) const; reference operator[]

当我尝试检查
str[index]
是否等于时,我得到异常字符串超出范围

std::string Test::getTheText(std::string str){
int指数=7;
字符串文本;
库特
为什么下面的代码有效

只有到那时你才会陷入:

[…],它会导致未定义的行为

C++标准参考:

§21.4.5基本字符串元素访问[string.access]

const_reference operator[](size_type pos) const;
reference operator[](size_type pos);

>P>要求:< > > > PoS

数组条目通常为0计数,所以在0和String:长度1之间定义搜索范围是至关重要的。在C++中,有时当你访问Str[n](n>LeN)时,会返回垃圾,并且根据我的悲惨经历编写它通常会导致段错误。

std::string str = "bla bla";
int index = 7;
cout << str[index] << endl; // Work!!
str[index++] // in second iteration, the first is ok though
const_reference operator[](size_type pos) const;
reference operator[](size_type pos);