c++ map clear inside 地图

c++ map clear inside a map

我有以下类型定义

typedef map<string, IPAddressPolicyRulesInfo> SecondMap;

typedef map<string, SecondMap>   FirstMap;

FirstMap            CPCRF::m_mIMSI2PCRFInfo;

在 c++ 函数内:

在函数中,我想清除第二张地图。哪种方法最好?如有任何意见或建议,我们将不胜感激。

谢谢 PDK

希望这可以让您朝着正确的方向开始:

#include <map>
#include <iostream>

// In general you shouldn't use _t after your own types (some people don't like it)
// But for a post here it doesn't matter too much.

typedef std::map<int, double> map1_t;
typedef std::map<int, map1_t> map2_t;

typedef map2_t::iterator it_t;

int main(int argc, char** argv)
{
  // Create the map
  map2_t m;

  // Add some entries
  { map1_t h; h[0] = 1.1; h[1] = 2.2; m[5] = h; }
  { map1_t h; h[0] = 5.2; h[8] = 7.2; m[1] = h; }

  // Output some information
  std::cout << m.size() << std::endl;
  std::cout << m[5].size() << std::endl;

  // For each element in the outer map m
  for (it_t it = m.begin(); it != m.end(); ++it)
  {
    // Assign a friendly name to the inner map
    map1_t& inner = it->second;

    // Clear the inner map
    inner.clear();
  }

  // Output some information (to show we have done something)
  std::cout << m.size() << std::endl;
  std::cout << m[5].size() << std::endl;

  return 0;
}