c++ - 关于在 C++ 中访问字符串的字母
问题描述
#include <iostream>
#include <string>
using namespace std;
int main() {
int n;cin>>n;//entering number of string to be inputed
string a[n];//declaring an array of type string
for(int i=0;i<n;i++){
cin>>a[i];
}
for(int i=0;i<n;i++){
cout<<a[i]<<endl;
}
//for manipulating letters of strings
cout<<a[0][1];
return 0;
}
要访问字符串的元素,我们应该将结果输出为多维数组。这似乎有点违反直觉。有人可以解释这是正确的方法。
Input
2
asfdsf
asfdsafd
Output
asfdsf
asfdsafd
s
解决方案
字符串是一个字符数组。所以字符串数组是字符数组的数组。要访问j
第 th 个字符串中的i
第 th 个字符,请使用a[i][j]
.
推荐阅读
- laravel - 如果给定,laravel 视图中的 textarea 应显示链接
- sap-cloud-sdk - 使用 sap-cloud-sdk 时出现 JSON 解析器错误
- javascript - 单击按钮时运行命令提示符命令
- node.js - 节点,续集,结语 - 发送到客户端后无法设置标题
- protocol-buffers - 如何描述 gRPC .proto 模式返回的可能错误代码
- java - 等待用户输入 Java Swing
- c# - 为什么这个 get 方法调用 (c#) 给我一个错误?
- node.js - 使用带有 s3 的电子更新器的电子应用程序导致访问被拒绝错误
- python - 带有条件的 Python 列表到数据框
- javascript - Object.create 与 .prototype