Vector向量的结构
我想做一个'表'来存储游戏的统计数据或技能值。 
 我最初试图使用带结构的多维数组,但我希望能够随时添加数据行。我做了一些研究:link,发现载体可能是我需要的。 
 我现在试图做一个向量的向量结构,无济于事。我不擅长与误差d:
 这里是我的代码:Vector向量的结构
#include <iostream> #include <vector> 
using namespace std; 
int main() 
{ 
    struct Skills 
    { 
     string type; 
     int value; 
    }; 
    vector< vector<Skills> > statTable; 
    statTable[0][0].type = "test"; 
    cout << statTable[0][0].type << endl; 
    return 0; 
} 
这里是错误:https://i.imgur.com/lNqbgyW.png
提前感谢!
回答:
我编译这个代码,但它给了我没有错误,它已成功编译但因为你必须在数据正确填写,因此,这里是我的解决方案
#include <bits/stdc++.h> using namespace std; 
int main() 
{ 
    struct Skills 
    { 
     string type; 
     int value; 
    }; 
    vector< vector<Skills> > statTable; 
    vector<Skills> v; 
    Skills x; 
    x.type = "test"; 
    v.push_back(x); 
    statTable.push_back(v); 
    cout << statTable[0][0].type << endl; 
    return 0; 
} 
希望给了我分割错误这可以帮助我使用C++ 98 btw。
回答:
看来你正在使用一个旧的编译器。在这种情况下,主要将外部结构放置在全局空间中。
struct Skills { 
    string type; 
    int value; 
}; 
int main() 
{ 
    //... 
此声明
vector< vector<Skills> > statTable; 载体statTable之后为空。所以你可能不会使用下标操作符。
相反申报像
vector< vector<Skills> > statTable(1, std::vector<Skills>(1)); 的向量后,你可以写
statTable[0][0].type = "test"; 可以通过下面的代码片段
vector< vector<Skills> > statTable; statTable.push_back(vector<Skills>()); 
statTable[0].push_back(Skills()); 
statTable[0][0].type = "test"; 
cout << statTable[0][0].type << endl; 
以上是 Vector向量的结构 的全部内容, 来源链接: utcz.com/qa/262605.html

