处理二维向量 c++
我需要在 C++ 中设置和访问结构的二维向量。 我的结构定义为:
struct nodo{
int last_prod;
int last_slot;
float Z_L;
float Z_U;
float g;
bool fathomed;
};
我将向量定义为:
vector<vector<struct nodo> > n_2;
现在,我需要创建 n_2 的几个元素,然后它们再次成为向量,然后访问它们的单个成员。 我怎样才能做到这一点?这是我到目前为止的一段代码:
for(int i=1;i<112;i++){
n_2.push_back(vector<struct nodo>(111-i));
for(int j=1;j<112-i;j++){
n_2[i][j].last_prod=j;
}
}
它不起作用。
I need to set up and access a two dimensional vector of structure in C++.
My structure is defined as:
struct nodo{
int last_prod;
int last_slot;
float Z_L;
float Z_U;
float g;
bool fathomed;
};
I defined the vector as:
vector<vector<struct nodo> > n_2;
Now,I need to create several elements of n_2, which will then be vectors again, and then access the single members of them.
How can I achieve that? that is the piece of code I have so far:
for(int i=1;i<112;i++){
n_2.push_back(vector<struct nodo>(111-i));
for(int j=1;j<112-i;j++){
n_2[i][j].last_prod=j;
}
}
which doesn't work.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(7)
向量的大小为 0,直到您告诉它调整大小,或者除非您使用特定大小初始化它。创建向量时传递向量的大小:
此外,看起来您正在跳过第 0 个索引,这意味着将跳过数组中的第一个值。这可能是不希望的。
最后,如果您的数组是恒定大小,请考虑使用 std::array比 std::vector 。请注意,std::array 是 C++11 功能,可能不可用,具体取决于您的编译器。
如果我正在编写这段代码,我可能会这样写:
或者,如果我没有支持 C++11 的编译器:
更理想的是,您应该使用一维向量,并简单地对待它作为二维向量。这样,您可以一次性分配一次内存,而不是进行 112 次较小的分配。这变得相当挑剔,但显然 O(1) 解决方案比 O(n) 解决方案更好,而 O(n) 解决方案在分配方面比 O(n^2) 解决方案更好,因为分配很慢。
A vector is size 0 until you tell it to resize, or unless you initialize it with a specific size. Pass the size of your vector in when you create it:
Also, it looks like you are skipping over the 0th index, which means your first value in your array will be skipped. That is probably undesired.
Finally, if your array is a constant size, consider using std::array rather than std::vector. Note that std::array is a C++11 feature and may not be available depending on your compiler.
If I were writing this code, I'd probably write it like this:
Or alternatively, if I don't have a compiler that supports C++11:
Even more ideally, you should use a 1 dimensional vector, and simply treat it as a 2 dimensional vector. This way, you can do a single allocation of memory all at once rather than 112 smaller allocations. This is getting pretty nitpicky but obviously a O(1) solution is better than an O(n) solution which is better than a O(n^2) solution in terms of allocations since allocations are slow.
在外循环中,您正在创建一个空向量(更新:问题更改后,它不再是空的,但仍然不够大);你需要创建一个足够大的尺寸:
In the outer loop, you're creating an empty vector (UPDATE: after the question changed, it's no longer empty, but still isn't big enough); you need to create it with a large enough size:
首先,这不是一个很好的方法。如果您提前知道尺寸,我实际上建议您此时使用普通的二维数组。或者,为了保持“C++”,您可以将向量包装在某些东西中并提供更易于理解的界面。
其次,如果您打算在不使用迭代器的情况下直接访问向量,则实际上应该从 0 开始数组索引。
但是,如果您确实想这样做,您的问题是您需要在引用其元素之前填充向量。
因此,
您需要确保在访问该元素所在的位置之前已将元素添加到嵌套向量。所以,你的代码可以是:
First of all, this isn't a very good way to do it. I'd actually suggest you use a normal 2-d array by this point if you know the dimensions in advance. Or, to stay "C++", you can wrap the vectors in something and provide a more intelligible interface.
Secondly, you should start your array indices from 0 really if you plan on directly accessing your vectors without an iterator.
But, if you really want to do this, your problem is you need to populate the vector before referencing its element.
So,
You'll need to ensure you've added an element to the nested vector before you access it the position where that element would be. So, your code could be:
我建议您使用调整大小,至少对我来说它看起来更整洁:
I would recommend you to use resizes, for me at least it looks neater:
您正在尝试访问尚不存在的项目。
提前分配向量中的项目或根据需要创建。更好的选择是使用数组而不是向量,因为无论如何你都不会调整向量的大小。这样会有性能的提升。
You're trying to access items that don't exist yet.
Either allocate the items in the vector ahead of time or create then as needed. A better alternative is to use an array instead of a vector since you don't resize the vector anyway. This will have a performance improvement.
您必须注意的第一件事是起始索引(在两个循环中)必须从 0 而不是 1 开始。
另一件事是您必须将某些内容推入嵌套向量内才能对其进行寻址(使用运算符 []) 。
The first thing you must pay attention is the starting index that (in both cycles) must start from 0 instead of 1.
The other thing is that you must push something inside the nested vector in order to address it (with the operator []).