Structure containing map on map? (C ++ / STL)

I was wondering if it is possible to create a structure containing a series of variables and a map on the map.

What I have at the moment:

typedef std::map<std::string,double> lawVariables;  

struct ObjectCustomData {   
  std::string objectType;    
  bool global_lock;   
  std::map<std::string, lawVariables> lawData;   
 };

This structure is then transferred to another function as a single data block for this object.

Setting up the structure is as follows: Each object has a data block that contains: its ObjectType, bool for blocking and a different number of "laws", which may look like this:

law1 -> var_a = 39.3;
     -> var_g = 8.1;
law8 -> var_r = 83.1;
     -> var_y = 913.3;
     -> var_a = 9.81;

Firstly, I’m not sure if I should use the Card within the Map, and secondly, even if it were really, I don’t know how to fill it with data and how to remind it. I looked at the cards, because then I can search (by name) if a certain object has a certain law, and if this law has certain variables.

( , , :))

+3
3

, ,

. .

, , :

typedef std::vector<std::pair<std::string,double> > lawVariables;  

struct ObjectCustomData {   
  std::string objectType;    
  bool global_lock;   
  std::map<std::string, lawVariables> lawData;   
 };

:   . , .

:

 typedef std::map<std::string,double> lawVariables;  

    struct ObjectCustomData {   
      std::string objectType;    
      bool global_lock;   
      std::map<std::string, lawVariables> lawData;   
     };

void test(ObjectCustomData& data)
{
  lawVariables& variable = data.lawData["law_1"];
  variable["var_a"] = 39.3;

}
+2

. :

  ObjectCustomData data;
  data.lawData["law_1"]["var_a"] = 39.3;
  data.lawData["law_1"]["var_g"] = 8.1;
  data.lawData["law_8"]["var_r"] = 83.1;
  .
  .
  .

:

  if (!data.lawData.count("law_x")) {
    cout<<"law_x not found"<<endl;
  }

  if (data.lawData.count("law_1")) {
    cout<<"law_1 was found"<<endl;
  }
+1

, - : ( , , , !))

.h:

typedef std::map<std::string,double> lawVariables;
typedef std::map<std::string,double>::iterator lawVars;
struct ObjectCustomData {
    std::string objectType;
    bool global_lock;
    std::map<std::string, lawVariables> lawData;
};


template <typename K, typename V, class C, class A>
std::ostream &operator<< (std::ostream &os, std::map<K,V,C,A> const& m)
{
    os << "{ ";
    typename std::map<K,V,C,A>::const_iterator p;
    for (p = m.begin(); p != m.end(); ++p) {
        os << p->first << ":" << p->second << ", ";
    }
    return os << "}";
}

: - , .

    void setCustomData();
    void showCustomData();
    bool checkLaw(std::string law);
    bool checkVar(std::string law,std::string var);
    double getLawVar(std::string law, std::string var);
    template<class T,class A>
    void showMap(const std::map<T, A>& v);
    ObjectCustomData ocd;

note: SetCustomData -- , showCustomData . checkLaw checkVar , , getLawVar . showMap .

.cpp:

setCustomData, ocd.lawData [law] [var] = 123.45;

    void showCustomData() {
        std::cout <<ocd.lawData<<std::endl; 
    }
    bool checkLaw(std::string law){
        if ((int)ocd.lawData[law].size() != 0) {
            return true;
        }
        else {
            return false;
        }   
    }
    bool checkVar(std::string law, std::string var){        
        lawVars lVars = ocd.lawData[law].find(var);
        if(lVars != ocd.lawData[law].end()){
            return true;
        }
        else {
            return false;
        }
    }
    double getLawVar(std::string law, std::string var){
        if (checkLaw(law) && checkVar(law, var)){
            return ocd.lawData[law].find(var)->second;
        }
        else {return 0.0;}
    }
    template<class T, class A>
    void showMap(const std::map<T, A>& v) {
        for (std::map<T, A>::const_iterator ci = v.begin(); ci != v.end(); ++ci) {
            std::cout << ci->first <<" -> ";
            lawVariables tmpLaw = ci->second;
            lawVars lVars;
            for (lVars = tmpLaw.begin(); lVars != tmpLaw.end(); lVars++){
                std::cout << lVars->first << " : " << lVars->second <<"\t";
            }
            std::cout<<std::endl;
        }
        std::cout<<std::endl;
    }

, -, , / .

0

Source: https://habr.com/ru/post/1748622/


All Articles