在创建对象向量时,不为每个对象唯一调用默认对象构造函数
Call not default object constructor for each object uniquely when creating a vector of objects
我正在尝试创建一个对象的向量,并希望为向量中的每个对象唯一调用非默认构造函数。我在下面有一个简化的例子。
在我的示例中,我有一个具有两个构造函数的对象,默认(无参数(和非默认(1 参数(。当我使用默认构造函数 (v1( 初始化大小为 10 的向量时,构造函数被调用了 10 次(从随机数可以看出(。但是,当我尝试使用对象非默认构造函数 (v2( 初始化向量时,对象构造函数被调用一次,并且该对象被复制到向量中的其余元素(x 不再是许多不同的随机数(。
是否可以初始化 N 个对象的向量,以便为每个对象调用每个对象的非默认构造函数?
示例代码:
#include <vector>
#include <iostream>
#include <cstdlib>
struct Obj {
int x;
Obj() {
x = rand() % 5;
}
Obj(int max_x) {
x = rand() % max_x;
}
};
int main() {
std::vector<Obj> v1(10); // Default constructor
std::vector<Obj> v2(10, 5); // Non-Default Constructor
for(int i=0; i<10; ++i) {
std::cout << v1[i].x << ", " << v2[i].x << std::endl;
}
}
输出:
3, 2
1, 2
2, 2
0, 2
3, 2
0, 2
1, 2
2, 2
4, 2
1, 2
溶液
以下函数可用于返回一个对象向量,其中调用每个对象的非默认构造函数。
template <typename T, typename ... Args> std::vector<T> InitVector(const int n, Args ... args) {
std::vector<T> vec;
for(int i = 0; i < n; ++i) {
vec.emplace_back(args ...);
}
return vec;
}
这里有两个解决方法。
-
使用列表初始化。
std::vector<Obj> v2{5, 5, 5, ...};
-
稍后使用
emplace_back
插入元素。std::vector<Obj> v2; v2.reserve(10); for (int i=0; i<10; ++i) { v2.emplace_back(5); }
我不太确定这是否是一种解决方案,但您可以重载复制构造函数来执行预期的构造。
我尝试了重载复制构造函数的想法:
#include <vector>
#include <iostream>
#include <cstdlib>
struct Obj {
const int max_x;
int x;
Obj(): Obj(5) { }
Obj(int max_x): max_x(max_x), x(rand() % max_x) { }
Obj(const Obj &obj): Obj(obj.max_x) { }
};
int main() {
std::vector<Obj> v1(10); // Default constructor
std::vector<Obj> v2(10, 5); // Non-Default Constructor
for(int i=0; i<10; ++i) {
std::cout << v1[i].x << ", " << v2[i].x << std::endl;
}
}
输出:
3, 2
1, 0
2, 4
0, 3
3, 1
0, 0
1, 1
2, 2
4, 1
1, 1
科里鲁的现场演示
它更好,但缺点是struct Obj
中的附加成员。 :-(
您可以使用generate_n
:
#include <algorithm>
#include <iterator>
std::vector<Obj> v2;
std::generate_n(std::back_inserter(v2),10, [](){return 5;});
感谢您的所有快速回复。最后,解决我的特定问题的最佳解决方案是创建一个向量初始化函数(按照人们的建议使用emplace_back(,该函数将返回所需的结果。我需要初始化很多对象向量,并且希望它们是不可变的,所以这个通用解决方案可能最适合我的用例。
#include <vector>
#include <iostream>
#include <cstdlib>
template <typename T, typename ... Args>
std::vector<T> InitVector(const int n, Args ... args) {
std::vector<T> vec;
for(int i = 0; i < n; ++i) {
vec.emplace_back(args ...);
}
return vec;
}
struct Obj {
int x;
Obj() {
x = rand() % 5;
}
Obj(int max_x) {
x = rand() % max_x;
}
};
int main() {
const std::vector<Obj> v1(10);
const std::vector<Obj> v2 = InitVector<Obj>(10, 5);
for(int i=0; i<10; ++i) {
std::cout << v1[i].x << ", " << v2[i].x << std::endl;
}
}
结果:
3, 2
1, 2
2, 0
0, 4
3, 3
0, 1
1, 0
2, 1
4, 2
1, 1
我认为这些行可以帮助您
std::vector<Obj> v2;
for(int i = 0;i<10;i++)
{
Obj s(5); // Non-Default Constructor
v2.push_back(s);
}
相关文章:
- C++优先级队列,按对象的唯一指针的特定方法升序排列
- 如何使用Visual Studio 2017在C++中为参数化对象数组使用唯一指针
- 使用Unique_ptr确保工厂中的对象唯一
- 具有引用成员的结构是否具有唯一的对象表示形式
- 堆分配的对象是否存在永不为空的唯一所有者?
- 如何在C++中为增加但记住删除先前对象的对象分配唯一标识符
- 添加要映射的对象的唯一指针
- 唯一命名对象的表
- 在创建对象向量时,不为每个对象唯一调用默认对象构造函数
- 唯一 ptr 将所有权移动到包含对象的方法
- 如何打印出唯一指针对象向量中的值(基于范围的循环)?C++
- 当原始指针和唯一指针都引用同一对象时,如何避免双重释放
- 将对象数组转换为指向唯一对象的指针数组
- 我如何模拟将作为唯一_ptr存储在gmock中的对象
- 使用具有唯一随机数的结构数组创建多个对象
- 确定 std::shared_ptr 是否是对对象的唯一引用
- 如何在 C++ 中插入和排序具有唯一条目的对象向量
- 唯一的指针在移动后仍然容纳对象
- 将全局对象初始化为指针是唯一成功的方法
- 具有编译器强制对象唯一所有权语义的c++类