C++ 在C+中使用引用包装器而不是标准指针访问类成员+;11
我正在尝试构建一个C++ 在C+中使用引用包装器而不是标准指针访问类成员+;11,c++,c++11,pointers,vector,reference-wrapper,C++,C++11,Pointers,Vector,Reference Wrapper,我正在尝试构建一个无序映射到向量变量,这些变量是我的类的成员。我可以使用标准指针*来实现这一点,但这需要使用(*x)来访问向量。我想知道std::reference\u wrapper是否会更干净,但无法让它工作 #include <unordered_map> #include <iostream> #include <vector> #include <functional> class model_class { private:
无序映射
到向量
变量,这些变量是我的类的成员。我可以使用标准指针*
来实现这一点,但这需要使用(*x)
来访问向量。我想知道std::reference\u wrapper
是否会更干净,但无法让它工作
#include <unordered_map>
#include <iostream>
#include <vector>
#include <functional>
class model_class {
private:
static constexpr auto MaxHerds = 1 ;
static constexpr int MaxInitValues = 5 ;
static constexpr int MaxAnimals = 2 ;
std::vector< double > MamCellsF ;
std::vector< std::vector< double > > InitCond ;
public:
std::unordered_map< std::string , std::reference_wrapper< std::vector< double > > > vector;
std::unordered_map< std::string , std::vector< std::vector< double > >* > array;
model_class ( ) :
// initialise vectors
MamCellsF( MaxHerds , 0.07 ) ,
InitCond( MaxAnimals , std::vector< double > ( MaxInitValues , 0.7 ) )
{
// point to variables from model
vector.insert({"MamCellsF", std::ref(MamCellsF)});
array["InitCond"] = &InitCond;
// assign to vectors
MamCellsF = { 0.001 , 0.002 } ; // warning: automatic resizing
InitCond = { { 1.0 , 550 , 3.5 , 1 , 4 } ,
{ 2.0 , 625 , 3.5 , 5 , 4 } } ;
}
void print_values(){
// access the vectors
double a = vector["MamCellsF"].get()[1]; // error: "no matching function call to `std::reference_wrapper<std::vector<double>>::reference_wrapper()"
double b = (*array["InitCond"])[0][1];
std::cout << a << std::endl;
std::cout << b << std::endl;
}
};
void test()
{
model_class model;
model.print_values();
}
#包括
#包括
#包括
#包括
类模型{
私人:
静态constexpr auto maxsurds=1;
静态constexpr int MaxInitValues=5;
静态constexpr int max=2;
std::vectorMamCellsF;
std::vector>InitCond;
公众:
std::无序映射>vector;
std::无序_映射>*>数组;
模型类():
//初始化向量
MamCellsF(最大牧群,0.07),
InitCond(MaxAnimals,标准::vector(MaxInitValue,0.7))
{
//指向模型中的变量
insert({“MamCellsF”,std::ref(MamCellsF)});
数组[“InitCond”]=&InitCond;
//分配给向量
MamCellsF={0.001,0.002};//警告:自动调整大小
InitCond={{1.0550,3.5,1,4},
{ 2.0 , 625 , 3.5 , 5 , 4 } } ;
}
无效打印_值(){
//访问向量
double a=vector[“MamCellsF”].get()[1];//错误:“没有对`std::reference_wrapper::reference_wrapper()的匹配函数调用”
双b=(*数组[“InitCond”])[0][1];
std::coutvector[“MamCellsF”]
返回对映射中的值的引用。因此,如果没有,则必须首先构造该值。该函数使用默认构造函数,但std::reference\u wrapper
不是默认可构造的,因此出现错误
我认为STL容器使用不可默认构造的T是安全的,但它们的操作可能会受到限制。因此我不建议这样做。vector[“MamCellsF”]
返回对映射中的值的引用。因此,如果没有,则必须首先构造它。这使用默认构造函数,但std::reference\u wrapper
不是默认可构造的,因此出现错误
我认为STL容器使用不可默认构造的T是安全的,但它们的操作可能会受到限制。因此我不建议这样做。我以为我已经将引用插入到映射中了。@SimonWoodward这无关紧要。这是编译错误,因为在该运算符中是执行添加和默认构造的代码你是说std::reference_wrapper
不是我想要做的事情的合适工具吗?那么使用标准指针可以吗?@SimonWoodward它有你想要的语义,但代价是它不是默认的可构造的,STL容器不能很好地与之配合。所以我坚持可能是指向指针。我以为我已经将引用插入到映射中了。@SimonWoodward这无关紧要。这是编译错误,因为在该运算符中是执行添加和默认构造的代码,无法编译。你是说std::reference\u wrapper
不是适合我尝试的工具吗怎么办?那么使用标准指针可以吗?@SimonWoodward它有你想要的语义,但代价是它不是默认可构造的,STL容器不能很好地使用它。所以我可能会坚持使用指针。