C++ C+中的字符串索引问题+;

C++ C+中的字符串索引问题+;,c++,C++,如何在C++中访问字符串索引? 例如:如果我有字符串变量名test,我想从索引号5到9访问它。我怎样才能用C++做到这一点 string test; cout<<test[5:9]; 字符串测试; cout如果包含字符串库,则可以使用substr方法: cout此切片语法在C++中不起作用。您必须使用,std::string::substr 例如: std::string test = "Some test string"; std::cout << test.sub

如何在C++中访问字符串索引? 例如:如果我有字符串变量名test,我想从索引号5到9访问它。我怎样才能用C++做到这一点

string test;
cout<<test[5:9];
字符串测试;

cout如果包含字符串库,则可以使用substr方法:


cout此切片语法在C++中不起作用。您必须使用,
std::string::substr

例如:

std::string test = "Some test string";
std::cout << test.substr(5, 4) << std::endl;
std::string test=“一些测试字符串”;
标准::不能使用


在C++中没有类似于
[x:y]
的切片运算符。

同样值得一提的是,这将返回一个新字符串。OP可能希望引用现有字符串的一个范围而不进行复制。如果将来有人遇到此帖子(仅供参考),如果您使用的是CString,模拟函数将是CString::mid
std::string test = "Some test string";
std::cout << test.substr(5, 4) << std::endl;