Click here to Skip to main content
15,886,137 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I have below code. I have inserted the data in the structures. I need to access the first value of mapOfTest using mapOfTest2.

C++
#include <iostream>
#include 
using namespace std; 

typedef struct
    {
      string name;
      int id;
    }Test;

typedef struct
    {
      int errCode;
      string errName;
      map<int, test=""> mapOfTest;
    }Test1;

typedef struct
    {
     map < string, Test1> mapOfTest1;
    }Test2;

int main()
{
    map< string, Test2> mapOfTest2;

   return 0;

}

What I have tried:

I have below code. I have inserted the data in the structures. I need to access the first value of mapOfTest using mapOfTest2.
Posted
Updated 24-Jan-21 9:46am
v2

The variable mapOfTest2 contains an entry that points to a Test2 object. That in turn has an entry that points to a Test1 object. The Test1 object contains the mapOfTest map. So following the links will get you to that map and give access to its contents. Quite why you have such a complicated set of structures is not clear.
 
Share this answer
 
Comments
Member 15025528 24-Jan-21 12:16pm    
Thanks for the answer. But I am not getting how to iterate.
Like i have created iterator of mapOfTest2
map< string, Test2>::iterator itr;
for(itr = mapOfTest2.begin(); itr = mapOfTest2.end();itr++)
{

}

then how to access mapOfTest1 and mapOfTest and Test member using mapOfTest2 iterator?
Richard MacCutchan 24-Jan-21 12:21pm    
You refer to each item in the map by the iterator, or use one of the map methods to find an entry based on the string field. see map::begin - C++ Reference[^].
Member 15025528 25-Jan-21 3:51am    
Thanks for the help Richard.
Irshad
Here you are, but...
Are you really sure you want this?
C++
#include <iostream>
#include <map>
using namespace std;

struct Test
{
  string name;
  int id;
};

struct Test1
{
  int errCode;
  string errName;
  map<int, Test > mapOfTest;
};

struct Test2
{
  map < string, Test1> mapOfTest1;
};

int main()
{
  Test test[] =
  {
    {"foo", 4},
    {"bar", 2}
  };


  Test1 test1[] =
  {
    5, "error five",
    {
      {5, test[0]} ,
      {25, test[1]}
    },
    6, "error six",
    {
      {6, test[1]} ,
      {36, test[1]}
    }
  };

  Test2 test2 = { {{ "goo", test1[0] }, { "boo", test1[0] }, {"woo", test1[1]}}};

  // access the 'name' field of the 'test[1]' variable ('Test' struct) 
  cout << test2.mapOfTest1["woo"].mapOfTest[6].name << endl;

}



[update]
As per OP request, here it is the code using iterators
C++
auto it1 = test2.mapOfTest1.find("woo");
if ( it1 != test2.mapOfTest1.end() )
{
  auto & mof = it1->second.mapOfTest; // reference to mapOfTest
  auto it = mof.find(6);
  if ( it != mof.end() )
    cout << it->second.name << endl;
}

[/update]
 
Share this answer
 
v2
Comments
Member 15025528 24-Jan-21 22:25pm    
Thanks CPallini,
Actually I need this, but using iterator.
CPallini 25-Jan-21 2:25am    
I've updated my solution. Now it shows how can you access the data using iterators.
Member 15025528 25-Jan-21 3:01am    
Thanks for the help CPallini.
Now I understood the way to access.
Thanks again
Irshad
CPallini 25-Jan-21 3:08am    
You are welcome.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900