我有这个 vector 数组, vector 对:
vector<pair<int, int> > adj[V];
如何将其复制到另一个变量?
vector<pair<int, int> > adjCopy[V] = adj; //not working;
我还尝试使用 std:copy
但显示 vector (可变大小)位于数组内部,因此无法复制。
最佳答案
vector<pair<int, int> > adjCopy[V];
std::copy_n(&adj[0], V, &adjCopy[0]); // or std::copy(&adj[0], &adj[0] + V, &adjCopy[0]);
更惯用的:
vector<pair<int, int> > adjCopy[V];
std::copy_n(begin(adj), V, begin(adjCopy)); // or std::copy(begin(adj), end(adj), begin(adjCopy));
一般情况下,不要使用 C 数组,而使用 std::array
,
std::array<std::vector<std::pair<int, int> >, V> adj;
auto adjCopy = adj;
关于c++ - 如何复制由 vector 组成的数组,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/67514441/