首页 > 解决方案 > 关于在 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

标签: c++

解决方案


字符串是一个字符数组。所以字符串数组是字符数组的数组。要访问j第 th 个字符串中的i第 th 个字符,请使用a[i][j].


推荐阅读