Solved

how to convert iterator to an object pointer

Posted on 2013-11-30
2
444 Views
Last Modified: 2013-12-01
class objectClass
{
    string name;
    int id;
};

objectClass object;

std::vector <object> objectList;

map <name, objectClass *> objectMap


for (auto iter = objectList.begin(); iter < objectList.end(); iter++)
{
       // ??? how do I assign the object name and object pointer
       // from the list to the objectMap

      objectMap [(*iter).name] = &(*iter);  // Is this right?
}
0
Comment
Question by:tommym121
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
2 Comments
 
LVL 86

Accepted Solution

by:
jkr earned 500 total points
ID: 39688242
objectMap [(*iter).name] = &(*iter);  // Is this right?

Open in new window


Yes, that would be correct way that keeps all 'misunderstandings' out and is on the safe side. Since the above snippet is full of typos, here's a complete one that you can use to verify that it compiles:

#include <vector>
#include <map>
#include <string>
using namespace std;

class objectClass
{
public:
    string name;
    int id;
};

void main ()
{
objectClass object;

std::vector <objectClass> objectList;

map <string, objectClass *> objectMap;


for (std::vector <objectClass>::iterator iter = objectList.begin(); iter < objectList.end(); iter++)
{
       // ??? how do I assign the object name and object pointer
       // from the list to the objectMap

      objectMap [(*iter).name] = &(*iter);  // Is this right?

} 

}

Open in new window

0
 

Author Closing Comment

by:tommym121
ID: 39688695
Thanks
0

Featured Post

Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

In days of old, returning something by value from a function in C++ was necessarily avoided because it would, invariably, involve one or even two copies of the object being created and potentially costly calls to a copy-constructor and destructor. A…
IntroductionThis article is the second in a three part article series on the Visual Studio 2008 Debugger.  It provides tips in setting and using breakpoints. If not familiar with this debugger, you can find a basic introduction in the EE article loc…
The goal of the video will be to teach the user the concept of local variables and scope. An example of a locally defined variable will be given as well as an explanation of what scope is in C++. The local variable and concept of scope will be relat…
The viewer will learn additional member functions of the vector class. Specifically, the capacity and swap member functions will be introduced.

728 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question