【问题标题】:Creating an iterator to a map in a class template在类模板中创建映射的迭代器
【发布时间】:2016-08-15 11:51:53
【问题描述】:

我有这个类模板,其中包含如下地图:

template<class K, class V>
class interval_map {

private:
    std::map<K,V> m_map;    
}

我想要一个函数来为映射添加值并检查键是否已经存在,所以我尝试使用迭代器来做到这一点:

void add_elements_test2 ( K const& key,V const& val)
{             
    std::make_pair<typename std::map<K,V>::iterator,bool>x;  
    x= m_map.insert(std::make_pair(key,val));
    if(x.second = false)
    {
        cout<<"Key alreads exists "<<endl;
    }
}

但是当我创建操作符时出现这个错误:

std::make_pair<typename std::map<K,V>::iterator,bool>x;

这是正确的方法吗?

【问题讨论】:

  • 如何,@NathanOliver 的语法是什么
  • @M.Cesar :你可能是 C++ 新手,if (x.second=false) 分配 falsex.second 然后什么都不做。

标签: c++ templates iterator


【解决方案1】:
std::make_pair<typename std::map<K,V>::iterator,bool>x;  

声明std::pair 的正确方法不正确。如果你想声明一个std::pair 来返回insert 那么你需要

std::pair<typename std::map<K,V>::iterator,bool> x;

现在x 具有正确的类型。 std::make_pair 是一个用于构造 std::pair 的函数,您将变量传递给它以生成对。

尽管您可以简单地使用auto like,但不必输入所有这些内容

auto x = m_map.insert(std::make_pair(key,val));
//...

现在x 具有正确的类型,您输入的内容也少了很多。


你也有错别字

if(x.second = false)

在上面你是在做分配,而不是比较。由于您将值设置为 false,因此 if 语句将永远不会运行,因为它将始终评估为 false。你需要

if(x.second == false)

【讨论】:

    【解决方案2】:

    只需使用auto:

    auto x = m_map.insert(std::make_pair(key, val));
    if (!x.second)
    {
        cout << "Key already exists" << endl;
    }
    

    注意:你要的类型是pair

    std::pair<typename std::map<K, V>::iterator, bool>
    

    std::make_pair 是创建std::pair 的实用函数。

    【讨论】:

      【解决方案3】:

      我写了这个答案是为了让你以后不会讨厌模板类。您需要考虑两种情况,我在下面的代码中列出了它们。如果您有任何问题,请告诉我,cmets 很详细。

      场景一,add_elements_test2 是在类里面定义的

      template<class K, class V>
      class interval_map {
      private:
          std::map<K,V> m_map;    
      
          // This is fine because the K and V types are in the same scope for the map, and add_elements_test2
          void add_elements_test2 ( K const& key,V const& val)
          {
              // Use auto here to simplify your life a little
              auto x = m_map.insert(std::make_pair(key,val)); // actual type is std::pair<K, bool>
              if(x.second == false)
              {
                  cout<<"Key already exists "<<endl;
              }
          }
      };
      

      场景二,add_elements_test2 定义在类外

      template<class K, class V>
      class interval_map {
      private:
          std::map<K,V> m_map;    
      
          void add_elements_test2 ( K const& key,V const& val);
      };
      
      // need another template
      template<class K, class V>
      // need to template interval_map, this could cause headaches if you did not realize this is a templated class
      void interval_map<K, V>::add_elements_test2 ( K const& key,V const& val)
      {
          // Use auto here to simplify your life a little
          auto x = m_map.insert(std::make_pair(key,val)); // actual type is std::pair<K, bool>
          if(x.second == false)
          {
              cout<<"Key already exists "<<endl;
          }
      }
      

      本质上,x 的错误在于类型定义。

      // pair is in the utility header
      std::pair<K, bool> x= m_map.insert(std::make_pair(key,val));
      

      【讨论】:

        猜你喜欢
        • 2012-06-28
        • 1970-01-01
        • 1970-01-01
        • 2012-08-03
        • 1970-01-01
        • 1970-01-01
        • 2012-05-10
        • 2021-04-15
        • 2011-07-31
        相关资源
        最近更新 更多