使用向量的二维数组
我想使用向量创建二维数组。但是,当我这样做时,我遇到了段错误。 谁能解释一下我做错了什么,以及这个问题的可能解决方案。
我将所有内容公开,因为我现在不想处理 getter 和 setter。 我想弄清楚二维数组的概念。
#include <iostream>
#include <vector>
using namespace std;
class point
{
public:
point():x(0),y(0){}
~point(){}
point(float xx,float yy):x(xx),y(yy){}
float x,y;
};
int main()
{
vector<vector<point> > a; // 2D array
point p(2,3);
a[0][0] = p; // error here
return 0;
}
I want to create 2D array using vector. But, when I do this, I get seg fault.
Can anyone please explain what I am doing wrong, and possible solution for this problem.
I made everything public since I dont want to deal with getters and setters now.
I want to get the concept of 2D array clear.
#include <iostream>
#include <vector>
using namespace std;
class point
{
public:
point():x(0),y(0){}
~point(){}
point(float xx,float yy):x(xx),y(yy){}
float x,y;
};
int main()
{
vector<vector<point> > a; // 2D array
point p(2,3);
a[0][0] = p; // error here
return 0;
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(8)
你的向量是空的。所以你不能使用
[0][0]
。声明方式如下:
如果您知道从一开始就有多少个项目,您可以这样做:
它是一个包含 10 个向量的向量,其中包含 10 个点。然后你可以使用
但是,我相信使用向量的向量是令人困惑的。如果您想要一个数组,请考虑使用 uBLAS http: //www.boost.org/doc/libs/1_41_0/libs/numeric/ublas/doc/index.htm
Your vector is empty. So you can't use
[0][0]
.Here is how you declare it:
If you know how many items you will have from the start, you can do :
It's a vector containing 10 vectors containing 10 point. Then you can use
However, I believe that using vector of vectors is confusing. If you want an array, consider using uBLAS http://www.boost.org/doc/libs/1_41_0/libs/numeric/ublas/doc/index.htm
这是另一个建议。您想要完成的任务之前已经完成,可以在 Boost 多阵列。
Here's another suggestion. What you're trying to accomplish has been done before and can be found within the Boost Multi-Array.
您构造了一个空的向量向量,并尝试取消引用第一个元素而不向其中添加任何元素。
向量不像(某些)关联数组那样工作,在关联数组中尝试访问缺少的值会将其添加到集合中。在尝试使用适当形式的向量构造函数或使用push_back 来访问向量之前,您需要确保向量具有适当数量的条目。
You have constructed a vector of vectors that is empty, and have tried to dereference the first element without adding any elements to it.
Vectors don't work like (some) associative arrays, where attempting to access a value that's missing will add it to the collection. You need to ensure the vectors have an appropriate number of entries before you try to access them by using the appropriate form of the vector constructor or by using push_back.
您正在创建二维数组,效果很好。问题是,当你创建它时,它是一个空数组——它根本不保存任何点。在实际创建点之前,您尝试使用 [0][0] 处的点。通常,要将新元素放入向量中,可以使用
resize()
设置向量的大小,或使用push_back()
一次添加一项。在这种情况下,后者可能会有点笨拙 - 因为你有一个点向量的向量,你需要创建一个点向量,将一个点推到该向量上,然后将该向量推到你的数组上。You're creating your 2D array just fine. The problem is that when you create it, it's an empty array -- it doesn't hold any points at all yet. You try to use the point at [0][0] before you've actually created a point there. Normally, to put a new element into a vector, you use
resize()
to set the size of the vector, orpush_back()
to add items one at a time. In this case, the latter will probably be a bit clumsy -- since you have a vector of vectors of point, you need to create a vector of point, push a point onto that vector, then push that vector onto your array.最简单的方法是使用
resize()
方法,如下所示:The simplest way would be to use
resize()
method as follow:设法让它工作。从其他地方学到了“typedef”的想法。尝试下面的代码,它有效:
Managed to get it working. Picked up the idea for the 'typedef' from somewhere else. Try the below code, it works:
您可以定义 vectorMatrix[][],它是向量矩阵,如下所示。
类:
定义构造函数:
添加条目的方法:
获取指向给定行和列中向量的指针:
清除所有矩阵:
You can define vectorMatrix[][], which is a matrix of vectors as follows.
Class:
Define constructor:
A method for adding an entry:
Getting a pointer to the vector in a given row and column:
Clearing all the matrix:
您可以使用
resize()
;例如,这里我将a
的大小调整为 100 x 200 数组:You can use
resize()
; e.g., here I resizea
to a 100 x 200 array: