简体   繁体   English

使用赋值运算符复制 std::vector

[英]copy std::vector using assignment operator

std::vector<MyStruct*> v1;
std::vector<void*> v2;

I have stl vector of pointer of structure.我有结构指针的 stl 向量。

I want to copy std::vector<MyStruct*> to std::vector<void*> .我想将std::vector<MyStruct*>复制到std::vector<void*>

If I use v2 = v1 , I am getting below error:如果我使用v2 = v1 ,则会出现以下错误:

error C2679: binary '=' : no operator found which takes a right-hand operand of
             type 'std::vector<_Ty>' (or there is no acceptable conversion).

How to resove this?如何解决这个问题?

Actually, the OP left essential details out.实际上,OP 忽略了基本细节。

However, I dare to write an answer as it's actually quite clear.但是,我敢写一个答案,因为它实际上很清楚。

Copy assignment of std::vector is possible when复制分配std::vector是可能的,当

  • source and destination vector have equal element type源向量和目标向量具有相同的元素类型
  • element type provides copy assignment.元素类型提供复制分配。

Example:例子:

#include <iostream>
#include <vector>

struct T {
  int value;
};

struct U {
  int value;
  U& operator=(const U&) = delete;
};

int main ()
{
#if 1 // OK:
  { std::vector<T> v1(10), v2;
    v2 = v1;
    std::cout << "After v2 = v1; v2 has size " << v2.size() << '\n';
  }
#else // Wrong: (U has no assignment!)
  { std::vector<U> v1(10), v2;
    v2 = v1;
    std::cout << "After v2 = v1; v2 has size " << v2.size() << '\n';
  }
#endif // 1
  return 0;
}

Output:输出:

After v2 = v1; v2 has size 10

Live demo on coliru 在coliru上进行现场演示

struct T has (default) copy assigment but in struct U I have explicitly deleted it. struct T具有(默认)复制分配,但在struct U我已明确删除它。

Changing #if 1 to #if 0 , the code doesn't compile anymore.#if 1更改为#if 0 ,代码不再编译。


After OP provided the missing info, an update of my answer:在 OP 提供丢失的信息后,更新我的答案:

std::vector::assign() is an alternative when std::vector::assign()是另一种选择

  • element type of source vector may be assigned to element type of destination vector.源向量的元素类型可以分配给目标向量的元素类型。

This is true for assigning MyStruct* to void* in the specific case of OP.在 OP 的特定情况下,将MyStruct*分配给void*是正确的。

Example:例子:

#include <iostream>
#include <vector>

struct T {
  int value;
};

struct U {
  int value;
  U& operator=(const T &t) { value = t.value; return *this; }
};

int main ()
{
  { std::vector<T> v1(10);
    std::vector<U> v2;
    v2.assign(std::begin(v1), std::end(v1));
    std::cout << "After v2.assign(std::begin(v1), std::end(v1)) v2 has size " << v2.size() << '\n';
  }
#if 1 // OK:
  { T t[] = { { 1 }, { 2 }, { 3 } };
    std::vector<T*> v1{ t + 0, t + 1, t + 2 };
    std::vector<void*> v2;
    v2.assign(std::begin(v1), std::end(v1));
    std::cout << "After v2.assign(std::begin(v1), std::end(v1)) v2 has size " << v2.size() << '\n';
  }
#else // Wrong: (Assignment from void* to T* not permitted!)
  { std::vector<void*> v1(10, nullptr);
    std::vector<T*> v2;
    v2.assign(std::begin(v1), std::end(v1));
    std::cout << "After v2.assign(std::begin(v1), std::end(v1)) v2 has size " << v2.size() << '\n';
  }
#endif // 1
  return 0;
}

Output:输出:

After v2.assign(std::begin(v1), std::end(v1)) v2 has size 10
After v2.assign(std::begin(v1), std::end(v1)) v2 has size 3

Live demo on coliru 在coliru上进行现场演示

You are almost certainly trying to copy vectors containing elements of different types.您几乎肯定会尝试复制包含不同类型元素的向量。 Even if the types of the elements in the vectors are convertible by the compiler (eg double and int ), collections of them are not.即使向量中元素的类型可以被编译器转换(例如doubleint ),它们的集合也不是。 You can manage it using the std::copy function, however:您可以使用std::copy函数管理它,但是:

#include <algorithm>
#include <iterator>
// ...
std::vector v1<int>;
// ...
std::vector v2<double>;
std::copy(v1.begin(), v1.end(), std::back_inserter(v2));

You mentioned that in your case, one of your vectors contains pointers.您提到在您的情况下,您的向量之一包含指针。 If the pointer types are convertible by the compiler, say, copying derived-class pointers to base-class pointers, std::copy will work as above.如果指针类型可由编译器转换,例如将派生类指针复制到基类指针,则std::copy将按上述方式工作。

If the pointers are not convertible, then you must use std::transform instead of std::copy , and supply the appropriate cast.如果指针不可转换,则必须使用std::transform而不是std::copy ,并提供适当的强制转换。 For example, copying base-class to derived-class pointers:例如,将基类复制到派生类指针:

class Y : public class X { ... };
std::vector v3<X*>;
// ...
std::vector v4<Y*>;
std::transform(v3.begin(), v3.end(), std::back_inserter(v4),
               [](X* arg) { return dynamic_cast<Y*>(arg); });

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

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