【问题标题】:Can I create map <string,map<string, int[2]> >?我可以创建地图 <string,map<string, int[2]> > 吗?
【发布时间】:2013-10-16 16:43:50
【问题描述】:

我的问题是我需要创建这个映射结构,这样我就可以存储两个普通字符串和一个由两个管道 fd 组成的 int 数组。

我的代码是:

map<string, map <string, int[2]> > fillEndPipes(Automata a){

map <string, map<string, int[2]> > tempMap;

vector <string> tempVector = a.getStates();
vector <string>::const_iterator it;
string name = a.getName();

for(it = tempVector.begin(); it != tempVector.end(); it++){
    int tuberia[2];
    pipe (tuberia);


    map<string, int[2]> innerMap;
    innerMap.insert(pair<string,int>((*it),(tuberia)));

    tempMap.insert(pair<string, map <string, int[2]> >(name,innerMap));


}

    return tempMap;
}

我还没有找到将 int[2] 插入到对或映射中的方法。

【问题讨论】:

  • 这行不通。但是您可以使用 std::array&lt;int, 2&gt; 代替普通数组。
  • 这会让你的事情变得复杂。你为什么不创建一个包含你想要的一切的类并将指针存储在 map/vector 中?
  • 谢谢大家,你是对的!
  • 使用 typedef 避免代码重复。如果需要,它还可以帮助您以后更轻松地修改数据结构。

标签: c++ dictionary insert pipe


【解决方案1】:

您可以定义一个结构并使用它来代替int[2]

struct pipefd {
    int fdin;
    int fdout;
};

【讨论】:

    【解决方案2】:

    你可以使用 std::pair

     map <string, map<string, pair<int,int> > > tempMap;
    

    【讨论】:

      猜你喜欢
      • 2017-01-06
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2020-03-10
      • 1970-01-01
      相关资源
      最近更新 更多