Solved

how to convert iterator to an object pointer

Posted on 2013-11-30
2
431 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
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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

Suggested Solutions

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…
Container Orchestration platforms empower organizations to scale their apps at an exceptional rate. This is the reason numerous innovation-driven companies are moving apps to an appropriated datacenter wide platform that empowers them to scale at a …
The goal of the tutorial is to teach the user how to use functions in C++. The video will cover how to define functions, how to call functions and how to create functions prototypes. Microsoft Visual C++ 2010 Express will be used as a text editor an…
The viewer will learn how to clear a vector as well as how to detect empty vectors in C++.

920 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

Need Help in Real-Time?

Connect with top rated Experts

13 Experts available now in Live!

Get 1:1 Help Now