Question

i'm 1st time trying boost lib and don't know how to assign map of string and share pointer. here is my code and i'm trying to assign value but not able to do it.

#include <boost\shared_ptr.hpp>
#include <boost\assign.hpp>
#include <boost\map.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
}

my question :- how do i assign x1 and y1 value to Xtype and Ytype of z1 type. really do not know what to do and how to start. If i'm able to assign then i can move ahead to do serialization of it.

Was it helpful?

Solution

just use the [] operator:

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;

If the map already contains the key provided, than the object for that key is replaced. Otherwise, a new entry into the map is generated and initialised with the object. Read more about std::map here (and avoid using shared_ptr<> if you don't know it yet and aren't 200% sure it's needed).

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top