?
Solved

how to convert iterator to an object pointer

Posted on 2013-11-30
2
Medium Priority
?
452 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 2000 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

Enroll in August's Course of the Month

August's CompTIA IT Fundamentals course includes 19 hours of basic computer principle modules and prepares you for the certification exam. It's free for Premium Members, Team Accounts, and Qualified Experts!

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…
This article shows you how to optimize memory allocations in C++ using placement new. Applicable especially to usecases dealing with creation of large number of objects. A brief on problem: Lets take example problem for simplicity: - I have a G…
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 user default arguments when defining functions. This method of defining functions will be contrasted with the non-default-argument of defining functions.

800 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