Aditya
Aditya

Reputation: 35

How to copy a array consisting of vectors

I have this array of vectors, vectors of pair: vector<pair<int, int> > adj[V];

How can i copy this to another variable ?

vector<pair<int, int> > adjCopy[V] = adj; //not working;

I also tried using std:copy but is shows that vector(variable size) is inside the array so, cannot copy.

Upvotes: 2

Views: 59

Answers (1)

alfC
alfC

Reputation: 16242

vector<pair<int, int> > adjCopy[V];
std::copy_n(&adj[0], V, &adjCopy[0]); // or std::copy(&adj[0], &adj[0] + V, &adjCopy[0]);

More idiomatic:

vector<pair<int, int> > adjCopy[V];
std::copy_n(begin(adj), V, begin(adjCopy)); // or std::copy(begin(adj), end(adj), begin(adjCopy));

In general, don't use C-arrays, use std::array,

std::array<std::vector<std::pair<int, int> >, V> adj;
auto adjCopy = adj;

Upvotes: 2

Related Questions