C++ 读取字符串数组

标签 c++

问题: 如何从数组字符串中提取字符?

解释: 普通字符串

 string example=("Stack Over Flow");
 cout<<example[1];

输出将是:

 t

我想要的是从字符串数组中提取一个字母示例:

string str[4];
str[0]="1st";
str[1]="2nd";
str[2]="3rd";
str[3]="4th";
cout<<str[2];

将打印

3rd

我如何从 str[0] 中获取“t”?

最佳答案

只需按照以下步骤操作即可:

str[0][2]; // third character of first string

更多例子:

string str[4];
str[0]="1st";
str[1]="2nd";
str[2]="3rd";
str[3]="4th";
cout<<str[0][2]<<endl; // t
cout<<str[2][1]<<endl; // r
cout<<str[3][2]<<endl; // h

关于C++ 读取字符串数组,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35340650/

相关文章:

c++ - Operator() 作为下标 (C++)

c++ - 识别类成员的数据类型

c++ - 从基指针列表循环 (OOD) 调用派生类方法

c++ - 将 FindResource 与内存缓冲区一起使用

c++ - 是我还是Boost Track SVN(Boost Geometry Extension Dissolve)的一部分无法编译?

c++ - GCC 3.4 与 4.4 对于基于 C++ 的 MEX 文件?

c++ - OpenGl 2d 缩放(使用缩放和平移而不是 glOrtho)

c++ 2软件,相同模型不同的 View 和 Controller

c++ - 当入口和导出指向不同的节点时,如何使用 boost::graph 来(大约)解决旅行商问题?

c++ - 返回值 int&