简体   繁体   English

emplace_back在向量中调用赋值运算符,但不在列表中

[英]emplace_back calls assignment operator in vectors but not in list

As per http://www.cplusplus.com/reference/vector/vector/emplace_back/ I understood that emplace_back would create objects in place without calling the assignment operator . 按照http://www.cplusplus.com/reference/vector/vector/emplace_back/的理解,emplace_back可以在不调用赋值运算符的情况下就地创建对象。 But in case of std::vector they call the assignment operator and they donot call the assignment operators in case of std::list. 但是在std :: vector的情况下,它们调用赋值运算符,而在std :: list的情况下,它们不调用赋值运算符。

My object is not copyable. 我的对象不可复制。 Is there any other way to get around the problem other then by using pointers. 除了使用指针之外,还有其他方法可以解决该问题。

Also erase in vector seems to call the assignment operator, Erase in list doesnot call the assignment operator. 向量中的擦除似乎也调用了赋值运算符,列表中的擦除不调用了赋值运算符。 this seemed wrong to me.. 这对我来说似乎是错误的。

Does std does not support objects which are not copyable? std不支持不可复制的对象吗?

vector requires the element type to be movable (not necessarily copyable) in order to maintain the elements in a contiguous array. vector要求元素类型是可移动的(不一定是可复制的),以便将元素保持在连续的数组中。

Insertion, at any point, might require all the elements to be moved to a new array, if the old capacity was too small. 如果旧容量太小,在任何时候插入都可能需要将所有元素都移到新阵列中。 Erasing, except at the end, requires elements after the erased one(s) to be moved forward. 除末尾外,擦除需要将擦除后的元素向前移动。

Other containers don't require the type to be movable, so perhaps deque (allowing insertion and removal at either end) or list (allowing insertion and removal anywhere) might be an option if you can't (or don't want to) make it movable. 其他容器不需要该类型的容器是可移动的,因此如果您无法(或不想),则可能使用deque (允许两端插入和移除)或list (允许在任何位置插入和移除)。使它移动。

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

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