C++ 向量的向量

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/2717533/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-28 00:28:10  来源:igfitidea点击:

C++ Vector of vectors

c++vector

提问by xbonez

I have a class header file called Grid.h that contains the following 2 private data object:

我有一个名为 Grid.h 的类头文件,其中包含以下 2 个私有数据对象:

vector<int> column;
vector<vector<int>> row;

And a public method whose prototype in Grid.h is such:

还有一个公共方法,其原型在 Grid.h 中是这样的:

int getElement (unsigned int& col, unsigned int& row);

The definition of above mentioned function is defined as such in Grid.cpp:

上述函数的定义在 Grid.cpp 中是这样定义的:

int getElement (unsigned int& col, unsigned int& row)
{
    return row[row][col] ;
}

When I run the program, I get this error:

当我运行程序时,我收到此错误:

error C2109: subscript requires array or pointer type

Whats going wrong?

怎么了?

回答by Jonathan M Davis

In the line return row[row][col];the first rowis the int&, not the vector.

在该行中return row[row][col];,第一个rowint&,而不是vector

The variable declared in the inner scope is shadowing the variable in the outer scope, so the compiler is trying to index an intrather than a vector, which it obviously can't do.

内部作用域中声明的变量遮蔽了外部作用域中的变量,因此编译器试图索引 anint而不是 a vector,这显然无法做到。

You should fix your variable names so that they don't conflict.

您应该修复您的变量名称,以免它们发生冲突。

EDIT:Also, while the error that you're getting indicates that the compiler is finding the wrong rowvariable, as A. Levy points out, you also have a problem with the declaration of your vector, so even if you fix the variable names, if you have indeed declared the vectoras shown here, it won't compile. Nested templates need spaces between the >symbols, otherwise the compiler will read >>as a right-shift operator rather than part of a template declaration. It needs to be

编辑:另外,虽然你得到的错误表明编译器找到了错误的row变量,正如 A. Levy 指出的那样,你的 声明也有问题vector,所以即使你修复了变量名,如果你确实已经声明了vector这里显示的,它不会编译。嵌套模板在>符号之间需要空格,否则编译器将读取>>为右移运算符而不是模板声明的一部分。它需要是

std::vector<std::vector<int> > row;

or

或者

std::vector< std::vector<int> > row;

In addition, as you're doing this in a header file, you're going to need to tack the std::tag on the front of anything from the std namespace - such as vector. If it were in a cpp file, then you could use using namespace std;but that would be very bad to do in a header file (since it would pollute the global namespace). Without the std::tag or the usingstatement, the compiler won't recognize vector.

此外,当您在头文件中执行此操作时,您将需要std::在 std 命名空间中的任何内容(例如vector. 如果它在 cpp 文件中,那么您可以使用,using namespace std;但在头文件中这样做会非常糟糕(因为它会污染全局命名空间)。如果没有std::标记或using语句,编译器将无法识别vector.

回答by A. Levy

This is probably not the index problem, but you also need a space between the nested angle brackets in your vector of vectors type declaration. C++ compilers have a hard time telling the difference between nested template types and the right bit shift operator.

这可能不是索引问题,但您还需要在向量类型声明的向量中的嵌套尖括号之间留一个空格。C++ 编译器很难区分嵌套模板类型和正确的位移运算符之间的区别。

Example:

例子:

vector<vector<int> >  vec2d;        // Good.

vector<vector<int>>   anotherVec2d; // Bad!

vector< vector<int> > yetAgain;     // Best IMHO. 
                                    // Keeps the white space balanced.

回答by Jive Dadson

I think you want something like this... (although I cannot imagine why :-))

我想你想要这样的东西......(虽然我无法想象为什么:-))

#include <vector>
#include <iostream>

using namespace std;

typedef vector<int> row;
typedef vector<row> matrix;

matrix mat(2,2);

int getElement (unsigned int ri, unsigned int ci)
{
    return mat[ri][ci] ;
}

int main() {

    mat[1][0] = 1234;
    cout << getElement(1,0) << endl;

    return 0;
}

回答by Pedro Torres

This is what you need:

这是你需要的:

return Grid::row[row][col];

回答by KevenK

It appears to me (although you may need to verify this on your own, I don't feel like writing up a test application) that the problem is coming from the fact that your parameter contains a named rowand your class has an inner variable rowand there is a naming conflict.

在我看来(虽然您可能需要自己验证这一点,但我不想编写测试应用程序)问题出在您的参数包含一个命名row并且您的类具有一个内部变量row和存在命名冲突。

You may need to qualify which row you're using. Consider:

您可能需要限定您正在使用的行。考虑:

return Grid::row[row][col];