Click here to Skip to main content
15,889,595 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I have implemented string to structure mapping as follows.
Here I have mapped the required key(string) to struct instance(object).
But I want to map the key(string) to struct type name and then get the value from the instance of that struct.

Main.cpp

C++
#include <iostream>
    #include <string>
    using namespace std;
    #include "myfile.h"
    
	int main()
	{
		string item="IndicatorLED";
		func (item);
		
		string item1="AssetTag";
		func(item1);
		
		string item2="ManagerType";
		func(item2);
		
		string item3="Count";
		func(item3);
	}


myfile.h

C++
#ifndef MYFILE_H
    #define MYFILE_H
    #include <iostream>
    #include <algorithm>
    #include<vector>
    #include <string>
    #include <map>
    
    using namespace std;
    
    struct Chassis
    {
        string InLED;
        string AstTg;
    };
    
    struct Manager
    {
        string MngrType;
        int count;
    };
    
    const Chassis chassis1 = {"On", "null"}; 
	const Manager manager1 = {"BMC", 23};    
	
	const map<string, const string> cha1 =
	{
		{ "IndicatorLED",	chassis1.InLED		},
		{ "AssetTag"	,	chassis1.AstTg		},
		{ "ManagerType"	,	manager1.MngrType	}
	};
	
	const map<string, int> cha2 =
	{
		{ "Count", manager1.count }
	};
    
    void func(string);
    #endif

myfile.cpp

C++
#include <iostream>
    #include <string>
    #include "myfile.h"
    #include <map>
    
    using namespace std;
    
    void func(string item)
	{
		if (cha1.find(item) == cha1.end())
		{
			if (cha2.find(item) != cha2.end())
			{
				cout << item << ":" << cha2.at(item) << endl;
			}
		}
		else
		{
			cout << item << ":" << cha1.at(item) << endl;
		}
	}


Now here I have mapped `string` to `chassis1.InLED`.
My question is if there is any possibility to map string to `Chassis` (struct type) and now get the value key from the instance of that struct.

What I have tried:

I dint have any idea of implementing map between string and structure type (name) and get the corresponding instance value from that structure instance.
Posted
Updated 27-Mar-18 0:43am

The template class map has some interesting functions which will help you. Take a look at the sample code in this link to find function for better understand the real usage. You also asked for the key lookup which is done with some iterator.

Why havent you used some search engine to find that information?
 
Share this answer
 
Of course it is possible. However you have to use a std::map<string, Chassis>, in other word the same map cannot hold at the same time objects of unrelated types like Chassis and Manager.

[update]
Code sample:
C++
#include <iostream>
#include <string>
#include <map>

using namespace std;

struct Chassis
{
  string inLED;
  string astTg;
};


int main()
{
  map <string, Chassis> mc;

  mc["Chassis1"] = { "On", "null"};
  mc["Chassis2"] = { "Off", "null"};
  mc["Chassis3"] = { "On", "null"};

  cout << "list of chassis: " << endl;
  for (const auto & c : mc) // iterate on map items 
    cout << c.first << " {" << c.second.inLED << ", " << c.second.astTg << "}" << endl;

  string s = "Chassis1";
  cout << endl << "searching for item '" << s << "'" << endl;
  auto it = mc.find(s); // search for the 'Chassis1' item
  if ( it != mc.end())
    cout << "found " << it->first << " {"<< it->second.inLED << ", " << it->second.astTg << "}" << endl;
}

[/update]
 
Share this answer
 
v2
Comments
gvsk 28-Mar-18 1:28am    
After implementing std::map<string, Chassis>, Can I get values of instances for struct type "Chassis". How can I get chassis1.InLED from this mapping?
CPallini 28-Mar-18 3:45am    
See my updated solution.

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