[英]map structure member to key
我想从后端获取某些键的值。
在后端,定义结构并初始化值。 将此视为后端中定义的结构:
struct person{
string nme;
string adrs;
int id;
};
person p1 = {"steve","ABC street",23};
corresponds to value of in back-end. 关键对应于后端中值。
is to be mapped to (p1 & adrs) in external file and should get the value "ABC street". 现在,关键将映射到外部文件中的(p1和adrs),并且应获取值“ ABC street”。
我的问题是如何对密钥及其外部文件中的特定结构成员进行映射,以及如何获取该密钥的值。
在这里我可以使用std :: map概念吗?
我有要求的解决方案。 但是为了进一步改进,映射将在单独的文件中声明(此处在.h文件中声明)
myfile.h
#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
#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()){} else {cout<< item<<":"<<cha2.at(item)<<endl;} }
else {cout<< item<<":"<<cha1.at(item)<<endl;}
}
main.cpp
#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);
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.