-2

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.

manu
  • 85
  • 7
  • Hi. You seem confused. Do you know what a `std::map` is and how it works? What about `shared_ptr`? – jrok Apr 23 '14 at 10:58
  • 5
    @manu _u hv to spk englsh n ths sit_. Not because we can't understand things, but because it makes it look like you don't respect the people that you would like to be helping you. If you are too lazy to type a few words, you're simply not going to succeed and helping you is a waste of time. – sehe Apr 23 '14 at 14:31
  • sorry sir !! i did not do it purposefully.. sometimes in hurry.. – manu Apr 23 '14 at 14:48

1 Answers1

1

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).

Walter
  • 44,150
  • 20
  • 113
  • 196
  • no key in map.. we have to add key also.. any value we can give to key – manu Apr 23 '14 at 11:38
  • could please provide me an example.. and i'm trying at my end..tell you results – manu Apr 23 '14 at 11:39
  • 1
    @manu _Do_ read the documentation that was linked. `my_map[key]` _does_ add the key if it doesn't exist. If you don't know how to assign a value to `key` (?!) then you can stop doing this and [read a book first](http://stackoverflow.com/questions/388242/the-definitive-c-book-guide-and-list). – sehe Apr 23 '14 at 14:29
  • @sehe - sir really thanks for reference link and i have already started reading "Effective C++" and it will take sometime to complete the book reading and understanding.. – manu Apr 23 '14 at 14:55
  • @manu I gave you an example. To use a `map`, you must have sensible keys for your mapped objects. A typical example is a database of entries with unique names (such as "manu the great"), when the names can act as keys. – Walter Apr 23 '14 at 23:00