c ++在地图中清除地图

时间:2015-01-13 11:38:28

标签: c++ stl

我有以下typedef

typedef map<string, IPAddressPolicyRulesInfo> SecondMap;

typedef map<string, SecondMap>   FirstMap;

FirstMap            CPCRF::m_mIMSI2PCRFInfo;

在c ++函数中:

在函数内,我想清除第二张地图。这是最好的方法吗?任何意见或建议表示赞赏。

感谢 PDK

1 个答案:

答案 0 :(得分:0)

希望这可能会让你开始朝着正确的方向前进:

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