使用 std::vector 在类中分配索引

Assign Index inside a class using std::vector

本文关键字:分配 索引 std vector 使用      更新时间:2023-10-16

我有一个 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;
}

现场演示