如何将值分配给类型为 std::map<std::string,shared_ptr<A>>
how do i assign value to member variable of type std::map<std::string,shared_ptr<A>>
我第一次尝试boost lib,不知道如何分配字符串映射和共享指针。这是我的代码,我正在尝试赋值,但无法执行。
#include <boostshared_ptr.hpp>
#include <boostassign.hpp>
#include <boostmap.hpp>
struct X
{
public:
int p;
double q;
X();
~X();
};
struct Y
{
float m;
double n;
Y();
~Y();
};
struct Z
{
public:
std::map<std::string,boost::shared_ptr<X>> Xtype;
std::map<std::string,boost::shared_ptr<Y>> Ytype;
int i;
string name;
Z();
~Z();
};
struct X *x1;
struct Y *y1;
void setx()
{
x1->p=10;
x1->q=20.20;
}
void sety()
{
y1->m=10;
y1->n=20.20;
};
void initialize(Z *z1)
{
z1->i=30;
// how to i add x1 and y1 to Xtype and Ytype respectively of z1 struct
}
我的问题是:如何将x1和y1值分配给z1类型的Xtype和Ytype。真的不知道该做什么以及如何开始。如果我能够分配,那么我可以继续对它进行序列化。
只需使用[]
运算符:
std::map<std::string, some_type> my_map;
some_type object=get_object(); // an object that you want to insert into the map
std::string key=get_key(); // the key you want to associated with object
assert(my_map.empty()); // no keys in map yet! (only for demonstration)
my_map[key] = object;
如果映射已经包含所提供的键,则替换该键的对象否则,将生成映射中的新条目,并使用对象进行初始化。在这里阅读更多关于std::map
的信息(如果你还不知道shared_ptr<>
,并且不能百分之百确定它是否需要,请避免使用它)。
相关文章:
- 使用std::multimap迭代器创建std::list
- C++中std::resize(n)和std::shrink_to_fit之间的区别
- 来自 std::list 的迭代器 .end() 按预期返回"0xcdcdcdcdcdcdcdcd"但 .begin()
- 错误:调用"std::vector<:vector<int>>::p ush_back(std::vector<std::__cxx11::basic_string<
- C 建造者Clang STD :: Sill,找不到超载的操作员&lt;
- 为什么STD :: MAP需要操作员&lt;以及我如何写一个
- std::vector::reserve(未知m),我知道m<<;N(通常)并且知道N
- std::vector<;uint8_t>;当C++11/14启用时,手动复制而不是调用memcpy
- C++运算符<<调用::ostream而不是std::osttream
- 是std :: set&lt; std :: future&gt;不可能存在
- 在修改列表后,std :: list&lt; t&gt; :: end()的值是否会更改
- BOOST ::变体无法解决运算符&lt;&lt;对于STD :: Ostream
- C++重载<<具有typedef'd std::vector
- 以x的倍数填充前导零,使用std::cout<<std::十六进制
- 错误:没有匹配'运算符<<"在'std::cout
- std::pair的默认构造函数<>将基本类型(int等)设置为零
- std::ostream&operator< & lt; (std:: ostream&压力,压力& &;val)
- 将std::endl传递给std::operator<<
- std::映射<>或std::vector<>在处理大型标志集时
- 重载& lt; & lt;使用命名空间std