c++ - 如何创建不同类型的多维 vector/数组?

标签 c++

我知道要创建一个多维 vector 你需要这样写

std::vector< std::vector <int> > name;
std::vector<int> firstVector;
firstVector.push_back(10);
numbers.push_back(thisVector);
std::cout << numbers[0][0]

输出将为 10。

但是我正在尝试创建三种不同类型的表格。第一列是字符串,第二列是整数,第三列是 double 。

这个表的输出看起来像这样

One     200    5.1%
Three    10    1.4%
Nine   5000   10.8%

最佳答案

我不确定我是否听懂了您的解释,但听起来您真正想要的是一个结构 vector :

struct whatever { 
    std::string first; // The first column will be a string
    int second;        // ...the second would be ints
    double third;      // ...and the third would be doubles.
};

std::vector<whatever> data;

就您的输出而言,您将定义一个 operator<<处理:

std::ostream &operator<<(std::ostream &os, whatever const &w) { 
     os << std::setw(10) << w.first 
        << std::setw(5) << w.second 
        << std::setw(9) << w.third;
     return os;
}

关于c++ - 如何创建不同类型的多维 vector/数组?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16473346/

相关文章:

c++ - lambda 函数的返回类型推导不起作用

c++ - 尽管有打印功能,但我的 Vector 没有打印

c++ - 关于 C++ char* 的错误代码。任何 body View ?

c++ - 定义友元运算符的问题

c++ - 如何通过单击按钮将 QMenu 文本从英语更改为俄语

c++ - 浮点格式和 isinf()

c++ - OpenGL 中的第三个三角形顶点在窗口中渲染时显示为 0.0

python - 在python中实现协程

c# - 如何在视频控件上方添加透明控件,如 mediaelement 上的 wpf 标签

c++ - Windows 上有复制文件夹的界面吗?