c++ - 如何为以下容器赋值?

标签 c++ dictionary stl

谁能告诉我如何初始化下面的容器? key是固定的,下面两列信息应该根据key访问,并且应该可以在迭代中更新:

谁能告诉我如何访问以下 map 容器中的可变大小列?

map<unsigned int, vector<vector<unsigned int>> > polyMap;



0                0                   20
                 1                   30 
                 2                   40
                 .                    . 
              face(n)           some angles

2                0                   20
                 1                   30 
                 .                   .
              face(m)           some angles

.                .                      .
.                .                      .
.                .                      .
.                .                      .
(`k vertex) (curesponding faces) (and angles)`

大家好,看来我的问题不完整:事情是这样的,我有顶点编号(即 uniq=key)和围绕该顶点的面以及各自的角度:示例如下所示

你能告诉我是否可以为上述目的制作问题中所示的 map 容器吗?

最佳答案

你可能想要这样的东西:

#include <map>
#include <vector>
#include <assert.h>

using namespace std;

int main()
{
  // Declare initialized polyMap
  map<unsigned int, vector<vector<unsigned int>> > polyMap
  {
    { 11,
      {
        { 1,2 }, { 3,4 }
      }
    },

    { 22,
      {
        { 5,6 }, { 7,8 }
      }
    }
  };

  // add another element dynamically
  polyMap.insert(pair<int, vector<vector<unsigned int>>>(
    { 33,
      {
        { 9, 10 },{ 11, 12 }
      }
    }
    ));


  // check expected outcome for some values 
  assert(polyMap[11][0][0] == 1);
  assert(polyMap[11][0][1] == 2);
  assert(polyMap[22][1][1] == 8);
  assert(polyMap[33][1][1] == 12);
}

关于c++ - 如何为以下容器赋值?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48442718/

相关文章:

c++ - 从IP获取域名

c++ - 当类在 main() 中时,为什么这个 std::sort 谓词会失败?

c# - 如何获取 Windows 服务自定义状态? [C++/C#]

C++ 从另一个静态函数调用一个静态函数

python - 按数值排序字典

Python:循环字典,一次两个键

algorithm - 经典 Cracking the Coding 面试题的运行时间 a^3 + b^3 = c^3 + d^3?

c++ - 编写自己的 STL 容器

c++ - map 的内积

c++ - unsigned short 和 signed short 比较奇怪的行为