使用 std::vector 在类中分配索引
Assign Index inside a class using std::vector
我有一个 30 个实例的 std::vector 初始化如下:
#include <vector>
class Foo {
public:
int x;
int Index; // I want this to be initialized when std::vector start the
// Instances.
Foo(int& _x) : x(_x) {}
int Function(){
// Do something cool ...
}
};
int main(){
int a = 5;
std::vector<Foo> Instances (30, a);
return 0;
}
所以我希望当我调用std::vector<Foo> Instances (30, SomeVariablePassedByReference);
每个实例的每个成员变量Index
时得到相应的数字 (0-30(。
我怎样才能做到这一点?也许使用运算符重载?我不想使用 C 时尚[ ] 运算符。
你想让Foo的成员变量成为向量中的索引吗? 只需为每个成员调用 ctor 并将其推送到向量中即可。
#include <vector>
class Foo {
public:
int a;
int Index; // I want this to be initialized when std::vector start the
// Instances.
Foo(int _index, int& _x) : Index(_index), a(_x) {}
void func(){
// Do something cool ...
}
};
int main(){
int a = 5;
std::vector<Foo> Instances;
Instances.reserve(30);
for(size_t i=0;i<30;i++)
Instances.emplace_back(i,a);
return 0;
}
您在vector
构造函数的第二个参数中传递的值将按原样传递给创建的每个元素的复制构造函数。 由于您正在创建Foo
对象的vector
,因此第二个参数本身将是Foo
对象,因此您将创建一个临时Foo
,将int
作为输入,然后将该临时参数传递给vector
中每个Foo
实例的复制构造函数。
因此,您最接近的语法是如下所示:
class Foo
{
public:
...
int a;
int Index;
Foo(int _x) : a(_x), Index(-1) {}
Foo(const Foo &_f) : a(_f.a) { Index = ++(const_cast<Foo&>(_f).Index); }
...
};
int main()
{
int a = 5;
// this (implicitly) creates a temp Foo object using the 'Foo(int)'
// constructor, and then copy-constructs the Foo objects in the
// vector passing that temp to the 'Foo(const Foo&)' constructor...
std::vector<Foo> Instances (30, a);
return 0;
}
现场演示
但是,这是对C++语言的公然不安全的滥用,因此不要使用它。
更安全的选择是在填充vector
后简单地初始化索引,例如:
class Foo
{
public:
...
int a;
int Index;
Foo(int _x) : a(_x), Index(-1) {}
...
};
int main()
{
int a = 5;
std::vector<Foo> Instances (30, a);
// yes, I know, you don't want to use the operator[], but it
// is a safer option...
for(size_t index = 0; index < Instances.size(); ++index)
Instances[index].Index = index;
// unless you use iterators instead...
/*
size_t index = 0;
for (std::vector<Foo>::iterator iter = Instances.begin(); iter != Instances.end(); ++iter)
iter->Index = index++;
*/
/*
size_t index = 0;
for (auto &f : Instances)
f.Index = index++;
*/
return 0;
}
现场演示
更新:感谢弗朗索瓦·安德里厄(François Andrieux(在评论中,我有另一个想法,涉及std::generate_n()
以您想要的方式为您创建索引。 虽然它不是您要查找的语法:
#include <vector>
#include <algorithm>
#include <utility>
struct MyIndexGenerator
{
int value;
int index;
MyIndexGenerator(int _value, int _first = 0) : value(_value), index(_first) {}
std::pair<int, int> operator()()
{
return std::make_pair(value, index++);
}
};
class Foo
{
public:
int x;
int Index;
Foo(const std::pair<int, int> &_x) : x(_x.first), Index(_x.second) {}
...
};
int main()
{
int a = 5;
std::vector<Foo> Instances;
Instances.reserve(30);
std::generate_n(std::back_inserter(Instances), 30, MyIndexGenerator(a));
return 0;
}
现场演示
相关文章:
- 使用 std::vector 在类中分配索引
- 通过在编译时折叠带有索引的指针来分配 C 数组
- 在xarray中通过索引赋值会分配整个数组
- 在 c++ 中将数组的值分配给另一个数组的索引
- cpp 数组 - 分配常量索引有效,而非常量索引不起作用
- 如何在C++中为数组分配不同的索引值
- 是否可以将结构分配给向量的索引?
- 为字符串中的每个字符(不是字符位置!)分配唯一的索引
- 如何将文本文件的部分分配给数组的索引
- C++ 在类外部分配 char 数组的索引
- 将指针分配给字符串对象的第一个也是最后一个索引
- 如何将要分配给具有不同索引的单独变量分配给C 的单独变量
- 如何根据输入在数组中分配多个索引
- C 索引中的字符串映射而无需分配
- 提升多索引有序迭代器分配
- 具有相同索引的数组已更改,但未重新分配
- 如何在opencv中创建cv::matrix的向量,并将图像数据的子矩阵分配给向量的所有索引上的矩阵
- 将常量字符串分配给大小恒定的 char 数组,在未使用的数组索引中会发生什么
- 是否可以使用运算符重载按索引分配用户定义的数组?-C++
- 删除模板分配的数组,该数组可能已分配索引