简体   繁体   English

声明向量 <vector<pair<int, int> &gt;具有特定大小和插入元素?

[英]Declaring a vector<vector<pair<int, int>> with a specific size and inserting elements?

I want to represent a graph data structure and I am using vector of vector of pairs in c++ stl. 我想表示一个图形数据结构,并且在c ++ stl中使用成对向量的向量。 Example: 例:

vector<vector<pair<int, int>>> G;

Now I can do, G[u].push_back(make_pair(v, w)); 现在我可以了, G[u].push_back(make_pair(v, w));

The problem: I need to specify this data structure a size. 问题:我需要指定此数据结构的大小。 If I don't I get a segmentation fault when I try to push elements to this data structure. 如果不这样做,当我尝试将元素推送到此数据结构时会遇到分段错误。 If I do give a size like: 如果我的尺码如下:

vector< vector<ii> > adj(10, std::vector<ii>(10));

The problem now is the first 10 pairs of vector are initialized to zero. 现在的问题是前10对向量初始化为零。 If I push back an element now, it gets pushed to the 11th position. 如果我现在向后推一个元素,它将被推到第11个位置。 The first 10 being 0s. 前10个为0。 I don't want to do this. 我不想这么做 I want to insert elements in the order I need. 我想按需要的顺序插入元素。 A snippet to give you an idea about what I am trying to do: 简要介绍一下我要做什么:

 for(int i=0;i<E-1;i++)
        {
            cin >> p >> q >> l;
            adj[p].push_back(ii(q, l));
            adj[q].push_back(ii(p, l));
        } 

The output for the above would be 10 zeros followed by the values pushed. 上面的输出将是10个零,后跟推入的值。 Any way to get around this? 有什么办法解决这个问题?

Somehow you are contradicting yourself: When creating a vector you can either pass the number of elements in the constructor or you start with an empty vector and push the elements you want to have in the vector. 您以某种方式自相矛盾:创建向量时,可以传递构造函数中的元素数量,也可以从空向量开始,然后将想要包含的元素压入向量中。 If you start with size 10 and then push 10 more there are 20. They way around is to either use 如果您从10号开始,然后再推10号,则有20条。

std::vector<T> vect(10);
for (size_t i=0;i<10;i++){
    vect[i] = whatever;
}

or 要么

std::vector<T> vect;
for (size_t i=0;i<10;i++){
     vect.push_back(whatever);
}

Maybe you are confusing the size of the vector with its capacity. 也许您将向量的大小与其容量混淆了。 This you can set via: 您可以通过以下方式设置:

std::vector<T> vect;
vect.reserve(10);
for (size_t i=0;i<10;i++){
     vect.push_back(whatever);
}

For your vector of vectors you have to make sure, that there is a vector at that index before you start pushing elements into it: 对于向量的向量,必须确保在开始将元素推入索引之前该索引处有一个向量:

std::vector<std::vector<T>> mat;
for (size_t i=0;i<10;i++){
    mat.push_back(std::vector<T>());
    for (size_t j=0;j<10;j++){
        mat[i].push_back(whatever);
    }
}

If you know in advance your data structure size, you can declare it using the proper constructor: 如果您事先知道数据结构的大小,则可以使用适当的构造函数对其进行声明:

vector<vector<pair<int,int>>> G(10);

While the program is reading data to fill that structure, it can either check if the inputted indeces are out of bounds or resize the vector accordingly. 在程序读取数据以填充该结构时,它可以检查输入的索引是否超出范围,也可以相应地调整向量的大小。 For example this test program: 例如此测试程序:

#include <iostream>
#include <vector>

using std::vector;
using std::pair;
using std::cout;
using std::cin;

int main() {

    vector<vector<pair<int,int>>> G(5);
    //  insufficient initial size  ^^^

    int p, q, l;
    while ( cin >> p >> q >> l )
    {
        // resize the vector to avoid index out of bound
        int m = std::max(p, q) + 1;
        if ( m > G.size() )
            G.resize(m);

        G[p].push_back(std::make_pair(q, l));
        G[q].push_back(std::make_pair(p, l));
    } 

    for( int r = 0; r < G.size(); ++r )
    {
        for ( int c = 0; c < G[r].size(); ++c ) {
            cout << r << ", " << c << ": " << G[r][c].first << ' ' << G[r][c].second << '\n';
        }
    } 

    return 0;
}

With an input like: 输入如下:

1 2 3
4 5 6
7 8 9
8 7 6
5 4 3
2 1 0

Gives this output: 给出以下输出:

1, 0: 2 3
1, 1: 2 0
2, 0: 1 3
2, 1: 1 0
4, 0: 5 6
4, 1: 5 3
5, 0: 4 6
5, 1: 4 3
7, 0: 8 9
7, 1: 8 6
8, 0: 7 9
8, 1: 7 6

segmentation fault is because of that u >= G.size() and is not by the push_back(). 分段错误是因为u >= G.size()而不是push_back()造成的。

You need first resize you G to the number of the vertexes of the graph, after that, you can do your push back as long as u < G.size() 您需要先将G的大小调整为图形的u < G.size()然后,只要u < G.size() ,就可以进行后退

The corrected code should like this: 更正后的代码应如下所示:

vector< vector<ii> > adj(10 /* number of vertexes in the graph */);
for (int i=0; i < E-1; i++)
{
    cin >> p >> q >> l;
    // make sure both p < adj.size() and q < adj.size()
    adj[p].push_back(ii(q, l));
    adj[q].push_back(ii(p, l));
} 

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM