将结构成员映射到键 [英] Map structure member to key

查看:103
本文介绍了将结构成员映射到键的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想从后端获取某个键的值。

在后端定义结构并初始化值。将此视为后端定义的结构:



I want to fetch value of certain key from back-end.
In back-end the structures are defined and values are initialized. Consider this as structure defined in back-end:

struct person{
    string nme;
    string adrs;
    int id;
};




person p1 = {"steve","ABC street",23};



密钥地址对应后端p1.adrs的值。

现在密钥地址将被映射到外部文件中的(p1& adrs),并且应该获得值ABC street。

我的问题是如何为密钥进行映射和它在外部文件中的特定结构成员以及如何获取该键的值。



我尝试过:



我用JSON实现了这个映射。




The key address corresponds to value of p1.adrs in back-end.
Now the key address is to be mapped to (p1 & adrs) in external file and should get the value "ABC street".
My question is how mapping is to be done for key and its particular structure member in external file and how to fetch value for that key.

What I have tried:

I have implemented that mapping using JSON.

{
 "address":"p1.adrs",
 "name":"p1.nme",
  ........
}



现在p1.adrs将是一个字符串,因为CPP不支持运行时反射,我不能这样做。


Now p1.adrs will be a string and as CPP do not support Run time reflection, I cannot do this.

推荐答案

你的 struct 可以直接使用 std :: map 和<$来提供映射c $ c> std :: string 作为键和 union (或 std :: variant ,如果你可以使用它)作为价值。





一个简单的例子(你需要 C ++ 17 编译器)。

[update]

Your struct may directly provide the mapping using, for instance, a std::map with std::string as key and a union (or std::variant, if you can use it) as value.


A simple example (you need a C++17 compiler).
[update]
#include <iostream>
#include <unordered_map>
#include <variant>
using namespace std;


class person
{
  unordered_map<string, variant<string, int> > msv; // this is the storage for the variables
public:
  person(string nme, string adrs, int id)
  {
    msv["nme"] = nme;
    msv["adrs"] = adrs;
    msv["id"] = id;
  }
  variant <string, int> operator[](const string & key)
  {
    auto it = msv.find(key);
    if ( it != msv.end())
      return it->second;
    throw( out_of_range("key not found")); // an alternative could be returning an empty variant
  }
};


int main()
{
  person p("steve","ABC street",23);

  cout << get<string>(p["nme"]) << ", " << get<int>(p["id"]) << endl; // OK, accessing existing fields

  cout << "'" << get<string>(p["foo"]) << "'" << endl; // Error, accessing missing field
}



[/ update]


myfile.cpp



myfile.cpp

#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



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





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);
}





这是要求。但是如果我在单独的文件中声明映射(这里在.h文件中声明)会更好。



This is requirement. But it will be more better if I declare mappings in separate file (here declared in .h file).


这篇关于将结构成员映射到键的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆