C++ 将元素插入二维向量
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/27241177/
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
Inserting elements into 2D vector
提问by Kam
so I'm creating a class that implements an adjacency list. Currently in my class definition I initialized two vectors:
所以我正在创建一个实现邻接列表的类。目前在我的类定义中,我初始化了两个向量:
vector<vector<int>> adjList;
vector<int> neighbors;
and I declared two functions that I plan to use to make it:
我声明了两个我计划用来制作它的函数:
bool constructAdjList();
bool insertIntoAdjList(int, int);
It's getting difficult wrapping my head around 2D vectors. I understand that it is essentially a vector of vectors, but I'm confused about how to insert a new value into one of the "subvectors". For example, I am able to create an adjacency list in createAdjList that is empty with the following loop:
将我的头缠绕在 2D 向量上变得越来越困难。我知道它本质上是一个向量向量,但我对如何将新值插入“子向量”之一感到困惑。例如,我可以使用以下循环在 createAdjList 中创建一个空的邻接列表:
for (int i = 0; i < numOfValues; i++){
neighbors.push_back(0);
adjList.push_back(neighbors);
neighbors.clear();
}
But how can I say, push_back the value 5 to the 4th vector in adjList, which would be represented in my insertIntoAdjList function as
但是我怎么能说,将值 5 推回到 adjList 中的第 4 个向量,这将在我的 insertIntoAdjList 函数中表示为
insertIntoAdjList(4, 5);
I know I can access a specific value in a 2D vector by saying adjList[4][1], but how can I push one onto it?
我知道我可以通过说 adjList[4][1] 来访问 2D 向量中的特定值,但是我怎样才能将一个值推到它上面呢?
Thanks!
谢谢!
回答by Kam
To push on the vector that is an element of another vector, you simply do this
要推动作为另一个向量元素的向量,您只需执行以下操作
adjList[x].push_back();
回答by JHumphrey
A couple of notes here.
这里有几个注意事项。
Your loop can be significantly shortened just be using the constructors of your two members:
只需使用两个成员的构造函数,就可以显着缩短循环:
vector<int> neighbors(1, 0); // set to length 1, value is zero
vector<vector<int>> adjList(numOfValues,neighbors); // "outer" vector is numOfValues long
. // each row is a *COPY* of neighbor
If you can't do this at construction time (maybe numOfValues isn't known yet), then there's still a better loop phrasing we can use:
如果您不能在构建时执行此操作(可能 numOfValues 尚不清楚),那么我们仍然可以使用更好的循环语句:
// neighbors object can be reused
neighbors.clear(0);
neighbors.push_back(0);
adjList.reserve(numOfValues); // reserving memory ahead of time will prevent allocations
for (int i = 0; i < numOfValues; i++){
adjList.push_back(neighbors); // push_back is by *COPY*
}
In your example, by using clear and push_back to essentially build the same vector every loop iteration, you are riskingan allocation and deallocation each iteration. In practice, most implementations won't do this, but if we can both shorten and potentially make things more efficient, we may as well.
在您的示例中,通过使用 clear 和 push_back 在每次循环迭代中基本上构建相同的向量,您在每次迭代时都冒着分配和解除分配的风险。在实践中,大多数实现不会这样做,但如果我们既能缩短时间又能提高效率,我们也可以。
Lastly, if the number of neighbors is relatively small and similar row to row (for instance a finite elements code with tetrahedral elements, where each element connects to ~5 others), then as others have suggested you may be better off with a different structure than vector-of-vector. For instance, a single vector that is logically organized such that a new "row" begins every N elements.
最后,如果邻居的数量相对较小且行与行相似(例如,具有四面体元素的有限元代码,其中每个元素连接到另外约 5 个元素),那么正如其他人所建议的那样,您可能会更好地使用不同的结构比向量的向量。例如,逻辑上组织的单个向量使得每 N 个元素开始一个新的“行”。
回答by Hanu Tyagi
If initially you do not have any values in the vector - You can push values into one vector and then push this vector into the 2D vector. For example:
如果最初您在向量中没有任何值 - 您可以将值推入一个向量,然后将该向量推入二维向量。例如:
vector< vector<int> > vt1;
vector<int> vt2;
vt2.push_back(value);
vt1.push_back(vt2);
If your vector is already populated then -
如果您的向量已经填充,则 -
vt1[index].push_back(value);