桌子的stl容器

Dan*_*ani 3 c++ stl

表格有数据结构吗?像这样存储:

    Width    Height
1     5        10
2     3        20
3     10       2
Run Code Online (Sandbox Code Playgroud)

我需要的是按行号和标题寻址的值,例如(2,“ Height”)将给出20。
我知道我可以做一个映射数组或2d数组,并将映射作为列名转换为number,但是在那里为此准备好的数据结构?

Xeo*_*Xeo 5

STL没有直接准备,但是STL很棒,您可以随时在其中结合使用:

#include <map>
#include <vector>
#include <string>
#include <iostream>

typedef std::map<std::string, int> col_val_map;
typedef std::vector<col_val_map> table;

int main(){
  table t(3);

  std::string col = "Width";
  t[0][col] = 5;
  t[1][col] = 3;
  t[2][col] = 10;

  col = "Height";
  t[0][col] = 10;
  t[1][col] = 20;
  t[2][col] = 2;

  col = "Random";
  t[0][col] = 42;
  t[1][col] = 1337;
  t[2][col] = 0;

  std::cout << "\t\tWidth\t\tHeigth\t\tRandom\n";
  for(int i=1; i <= 3; ++i){
    std::cout << i << "\t\t" << t[i-1]["Width"]
                   << "\t\t" << t[i-1]["Height"]
                   << "\t\t" << t[i-1]["Random"]
                   << "\n";
  }
}
Run Code Online (Sandbox Code Playgroud)

输出显示在Ideone上

或者,就像@DeadMG所说的那样:

#include <map>
#include <vector>
#include <string>
#include <iostream>

typedef std::vector<int> row_val_array;
typedef std::map<std::string,row_val_array> table;

int main(){
  table t;
  t["Width"].reserve(3);
  t["Width"][0] = 5;
  t["Width"][1] = 3;
  t["Width"][2] = 10;

  t["Height"].reserve(3);
  t["Height"][0] = 10;
  t["Height"][1] = 20;
  t["Height"][2] = 2;

  t["Random"].reserve(3);
  t["Random"][0] = 42;
  t["Random"][1] = 1337;
  t["Random"][2] = 0;      

  std::cout << "\t\tWidth\t\tHeigth\t\tRandom\n";
  for(int i=1; i <= 3; ++i){
    std::cout << i << "\t\t" << t["Width"][i-1]
                   << "\t\t" << t["Height"][i-1]
                   << "\t\t" << t["Random"][i-1]
                   << "\n";
  }
}
Run Code Online (Sandbox Code Playgroud)

再次显示在Ideone上