-1

I have a vector pointer that points to a vector<object> so const std::vector<object> vecPtr* = &vec;

Now i'd like to fill in this manner std::multimap<std::string, object*> dataMap; where the key is object.name and value is pointer to an object.

I tried

for(std::vector<object>::const_iterator it = data->cbegin(); it != data->cend(); ++it){
        dataMap.insert(std::pair<std::string, object*>(it->name, &it));
}

but i get an error.

error: no matching function for call to 'std::pair<std::basic_string<char>, object*>::pair(const string&, std::vector<object>::const_iterator*)'
         dataMap.insert(std::pair<std::string, object*>(it->name, &it));
                                                                          ^

What am i doing wrong?

I know i complicating my life with pointers, but I want to avoid copying objects

jabk
  • 1,388
  • 4
  • 25
  • 43
  • You should mention exactly what the error you get is. – heksesang Oct 23 '14 at 13:33
  • This looks fishy. You pass a pointer to an iterator. 'dataMap.insert(std::pair(it->name, &it))* – Oncaphillis Oct 23 '14 at 13:35
  • You actually add the address of the iterator to the map, not the pointer to the object. – fast Oct 23 '14 at 13:36
  • @Oncaphillis so i should add a * at the end? – jabk Oct 23 '14 at 13:38
  • `vecPtr` is not declared as a pointer, and anyway it doesn't seem to have anything to do with the rest of the question. – interjay Oct 23 '14 at 13:40
  • @interjay ups, forgot to add a *, but that is not the reason for the error – jabk Oct 23 '14 at 13:41
  • @user2202368 No I assume your class CountryStat which you do not describe has a field value which is a pointer to object. Then it should be it->value. If value is a member of CountryStat it should be &it->value. But beware of manipulating the vector in the latter case since your pointer to value would be a moving target. – Oncaphillis Oct 23 '14 at 13:43
  • @Oncaphillis CountryStat(now changed to "object") are stored inside a `vec`, each object has a .name variable. I have a problem when i want to copy a pointer to an object – jabk Oct 23 '14 at 13:51

2 Answers2

2

In order to avoid copying objects consider to use reference of objects. Moreover, consider to use shared pointer such as std::shared_ptr (for C++11), or boost::shared_ptr. A good style is to avoid allocating memory manually. Let's do it in an automatic way provided by the STL.

class Object{};
typedef boost::shared_ptr < Object > ObjectPtr;

then

std::multimap < std::string, ObjectPtr > map; 

Creating instances of Object just use:

ObjectPtr obj = boost::make_shared < Object > ();
sfrehse
  • 1,062
  • 9
  • 22
1

&it is a pointer to iterator, not to an object. If you want to get a pointer to an object, write &*it.

After that you'll see an error saying that you can't convert from const object* to object* - this is because you are using const_iterator. So, depending on what you need, you can do two things.

Either declare dataMap as std::multimap<std::string, const object*> dataMap;, if you are not planning to change the objects in it.

Or use iterator:

for (std::vector<object>::iterator it = data->begin(); it != data->end(); ++it) {
    dataMap.insert(std::pair<std::string, object*>(it->name, &*it));
}

By the way, this loop can be rewritten as:

for (auto& a : *data) {
    dataMap.insert({a.name, &a});
}
Anton Savin
  • 40,838
  • 8
  • 54
  • 90