Solved

how to convert iterator to an object pointer

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

Efficient way to get backups off site to Azure

This user guide provides instructions on how to deploy and configure both a StoneFly Scale Out NAS Enterprise Cloud Drive virtual machine and Veeam Cloud Connect in the Microsoft Azure Cloud.

Question has a verified solution.

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

Introduction This article is the first in a series of articles about the C/C++ Visual Studio Express debugger.  It provides a quick start guide in using the debugger. Part 2 focuses on additional topics in breakpoints.  Lastly, Part 3 focuses on th…
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 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.
The viewer will learn how to clear a vector as well as how to detect empty vectors in C++.

809 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