Solved

how to convert iterator to an object pointer

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

Better Security Awareness With Threat Intelligence

See how one of the leading financial services organizations uses Recorded Future as part of a holistic threat intelligence program to promote security awareness and proactively and efficiently identify threats.

Join & Write a Comment

Errors will happen. It is a fact of life for the programmer. How and when errors are detected have a great impact on quality and cost of a product. It is better to detect errors at compile time, when possible and practical. Errors that make their wa…
Written by John Humphreys C++ Threading and the POSIX Library This article will cover the basic information that you need to know in order to make use of the POSIX threading library available for C and C++ on UNIX and most Linux systems.   [s…
The goal of the video will be to teach the user the difference and consequence of passing data by value vs passing data by reference in C++. An example of passing data by value as well as an example of passing data by reference will be be given. Bot…
The viewer will learn how to pass data into a function in C++. This is one step further in using functions. Instead of only printing text onto the console, the function will be able to perform calculations with argumentents given by the user.

708 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