【发布时间】:2019-04-16 10:07:59
【问题描述】:
我阅读了文章Initializing a vector of vectors having a fixed size with boost assign,它应该完全符合我的要求:初始化一个可以在两个方向上任意扩展的向量的类矩阵向量(我想用它来从更大的列表)。
但是,前2个答案中给出的解决方案
vector<vector<int>> v(10, vector<int>(10,1));
在我的 CDT_eclipse 中提示语法错误,并在我的编译器中提示以下错误:
error: expected identifier before numeric constant
vector <vector <int> > v(10, vector <int>(10,1));
--
vector of vector - specific syntax 中找到的版本适用于我的 Eclipse:
vector<vector<int>> v = vector<vector<int>>(n, vector<int>(n, 0));
它提示我的编译器发出警告:
vector warning: non-static data member initializers only available with -std=c++11 or -std=gnu++11 [...]
在应该使用我的代码的宏伟方案中,无法更改编译器版本(Ubuntu 5.4.0-6ubuntu1~16.04.10 的 gcc 5.4.0 20160609)。所以我需要上面提到的命令的兼容公式。非常感谢!
编辑:我的两个主要尝试如下所示:
vector <vector <int> > v(10, vector <int>(10,1)); --> syntax error
vector <vector<int> > v = vector <vector<int> >(1, vector<int>(1, 0)); --> compiler error
【问题讨论】:
-
在我的情况下编译得很好。你试过
std::vector<std::vector<int>> v(10, {1, 1, 1, 1, 1, 1, 1, 1, 1, 1 });吗? -
1) 由于您收到有关使用 -std=c++11 标志编译的警告,您是否尝试设置它? 2)如果我没记错的话,pre C++-11
vector<vector<int>> v(10, vector<int>(10,1));必须是vector<vector<int> > v(10, vector<int>(10,1));。 -
@Ayxan 不幸的是,这给我留下了另一个语法错误,我的编译器也抱怨:错误:数字常量之前的预期标识符
-
@AlgirdasPreidžius:1)编译是由一个单独的脚本完成的,我无法更改(我们的机构结构相当复杂......)。所以我没有尝试设置标志,但不幸的是也不能:/ 2)我在 >> 之间添加了额外的空格,但这不是问题。
-
@BlueFire "我在 >> 之间添加了额外的空格,但这不是问题。" 在这种情况下,您不是在编译代码,而是在向我们展示。 It compiles fine even with C++03.
标签: c++ vector g++ compiler-version