(C++) 如何显示具有 Char 和 Int 值的二维数组?

(C++) How do you Display a 2D Array with both Char and Int Values?

#include<iostream>
using namespace std;

int main()

{

string display[2][3] = {{"Name","Geometry","English"},{"Larry",90,85}}; //problem is here
    
    for(int row=0;row<2;row++){
        for(int col=0;col<3;col++){
            cout<<display[row][col]<<" "; 
        }
        cout<<endl;
    }
}

//[错误] 从 'int' 到 'const char*' 的无效转换 [-fpermissive]

您只能拥有一种类型的数组。在你的例子中是字符串数组。所以如果你想把你的整数放入这个数组中,你可以使用:

std::to_string(some_int);

所以它将是:

string display[2][3] = {{"Name","Geometry","English"},{"Larry",std::to_string(90),std::to_string(85)}}; //problem is here

我在这里猜测,但我觉得你用错了工具。

您似乎想要像 CSV 文件一样的结构,第一行为 header,其余行为数据。这不能使用数组来完成,因为数组必须是同构的。您不能在其中存储多种类型。
您当然可以将 int 转换为 std::string,请参阅其他答案,但如果下一步是,例如找到分数的平均值,突然变得更加困难。

存储多种类型数据的更好解决方案是创建 class。

#include <iostream>
#include <string>

struct Student
{
    std::string name;
    int geometryScore;
    int englishScore;
};

int main()
{
    Student students[1] {{"Larry",90,85}};
    std::cout << "Name Geometry English\n";
    for(int i = 0; i < 1; i++)
    {
        std::cout << students[i].name << " " << students[i].geometryScore << " " 
                  << students[i].englishScore << "\n";
    }
}