我需要存储ThirdPartyElm类型的元素序列,我使用的是std::vector (如果需要固定大小的序列,则使用std::array )。
我在想该怎么初始化这个序列。第一个版本创建一个新元素,当元素插入序列时(如果我是对的)创建元素的副本:
for (int i = 0; i < N; i++)
{
auto elm = ThirdPartyElm();
// init elm..
my_vector.push_back(elm); // my_array[i] = elm;
}
第二个版本存储一系列指针(或者使用c++11存储更好的智能指针):
for (i
vector<vector<int> > res;
res.emplace_back({1,2}); // change to res.push_back({1,2}); would work
这给了我错误
main.cpp:61:25: error: no matching function for call to ‘std::vector<std::vector<int> >::emplace_back(<brace-enclosed initializer list>)’
main.cpp:61:25: note: ca
我有下面的类,并尝试添加复制和移动构造函数和赋值操作符。我的目标是拥有最少的拷贝,并尽可能优化。
我预计向量将被填充到位,即在创建向量时不调用复制构造函数。我做错了什么,如何强迫它使用移动构造函数或赋值?
#include <iostream>
#include <concepts>
#include <vector>
template<typename T>
requires std::is_arithmetic_v<T>
class Data {
private :
T mA = 0;
T mB = 0;