简体   繁体   English

在向量的相同位置插入多个元素

[英]Inserting multiple elements at same position in a vector

This is code for representing adjacency list using vector in C++(from geeksforgeeks). 这是使用C ++中的vector表示邻接列表的代码(来自geeksforgeeks)。

// A utility function to add an edge in an 
// undirected graph. 
void addEdge(vector<int> adj[], int u, int v) 
{ 
    adj[u].push_back(v); 
    adj[v].push_back(u); 
} 

// A utility function to print the adjacency list 
// representation of graph 
void printGraph(vector<int> adj[], int V) 
{ 
    for (int v = 0; v < V; ++v) 
    { 
        cout << "\n Adjacency list of vertex "
             << v << "\n head "; 
        for (auto x : adj[v]) 
           cout << "-> " << x; 
        printf("\n"); 
    } 
} 

// Driver code 
int main() 
{ 
    int V = 5; 
    vector<int> adj[V]; 
    addEdge(adj, 0, 1); 
    addEdge(adj, 0, 4); 
    addEdge(adj, 1, 2); 
    addEdge(adj, 1, 3); 
    addEdge(adj, 1, 4); 
    addEdge(adj, 2, 3); 
    addEdge(adj, 3, 4); 
    printGraph(adj, V); 
    return 0; 
}

addEdge(adj,0,1) and addEdge(adj,0,4) both push_back values 1 and 4 at position 0. So my question is can we insert two or more values at same position in vector? addEdge(adj,0,1)和addEdge(adj,0,4)都在位置0处push_back值1和4。所以我的问题是我们可以在向量的相同位置插入两个或多个值吗? Does it create some sort of linked list at a particular position of vector when more than one values are inserted into that position? 当在一个向量的特定位置插入多个值时,是否会在该位置创建某种链表? If not, can please someone explain the working of above code. 如果没有,请有人解释以上代码的工作原理。

So my question is can we insert two or more values at same position in vector 所以我的问题是我们可以在向量的相同位置插入两个或多个值吗

No, we can't. 不,我们不能。 A vector contains exactly one element in each index. 向量在每个索引中仅包含一个元素。 No more; 不再; no less. 不少。 Just like an array. 就像数组一样。

Does it create some sort of linked list 它是否创建某种链表

No. Vector is implemented using a dynamic array; 向量是使用动态数组实现的; not a linked list. 不是链表。

can please someone explain the working of above code. 可以请别人解释以上代码的工作原理。

vector<int> adj[V] is an array of vectors. vector<int> adj[V]是向量的数组。 Each vector represents the adjacency list of the vertex identified by the index where the vector is stored, and each of those vectors can contain multiple elements. 每个向量表示由存储向量的索引标识的顶点的邻接表,并且每个向量可以包含多个元素。

You can use nested vector to insert more than one values at a position. 您可以使用嵌套vector在一个位置插入多个值。

Declaration vector< vector<int> > adj[V]; 声明vector< vector<int> > adj[V];

Now to insert a value at position 0 you can use like this 现在要在位置0插入值,您可以像这样使用

void addEdge(vector<int> adj[], int u, int v, int val) 
{ 
    adj[u][v].push_back(val); 
    adj[v][u].push_back(val); 
}

To add element 添加元素

addEdge(adj, 0, 0, 1); // insert 1 at position (0,0)

Please keep in mind that before adding element you need to initialize vector at every index . 请记住,在添加元素之前,您需要在每个index处初始化vector

But you can't insert two or more values at same position in vector. 但是您不能在向量的相同位置插入两个或多个值。

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

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