簡體   English   中英

將迭代器添加到泛型類中的泛型容器:如何

[英]getting an iterator to a generic container inside a generic class: HOW TO

這來自更大的上下文,但是我剝離了很多代碼以簡化問題。 如果您覺得我遺漏了任何東西,請告訴我。

假設您有一個定義為的模板類:

#include <map>
#include <vector>
#include <string>
#include <fstream>
#include <iostream>
#include <iterator>
#include <algorithm>
template <class key, class container, class container_type>
class file_to_map
{
public:
  file_to_map()
  {
    m_file = "";
  }

  file_to_map(std::string file)
  {
    m_file = file;
  }

  ~file_to_map()
  {
  }

  std::map<key, container>& get_map()
  {
    return m_map;
  }

  void set_file(const std::string file)
  { 
    m_file = file;
  }

  void insert_into_map(key insert, container_type value)
  {
    m_map[insert].insert(value);
  }

  friend std::ostream& operator<< (std::ostream &out, file_to_map<key, container, container_type> &obj)
  {
    typedef typename std::map<key, container>::const_iterator mapItr;
    mapItr mbi = obj.m_map.begin();
    mapItr emi = obj.m_map.end();
    while (mbi != emi) {
      out << " -- " << mbi->first << " -- " << std::endl;
      container::iterator cbi;
      ++mbi;
    }
    return out;
  }

  friend std::istream& operator>> (std::ifstream &in, file_to_map<key, container, container_type> &obj)
  {
    if (in.is_open())
      in.close();

    if (obj.m_file == "")
      return in;

    in.open(obj.m_file.c_str(), std::ios::in);

    if (in.fail() || in.bad()) {
      in.close();
      return in;
    }

    std::vector<key> tmp;
    typedef std::istream_iterator<key> string_input;
    copy(string_input(in), string_input(), back_inserter(tmp));
    typename std::vector<key>::iterator bvi = tmp.begin();
    typename std::vector<key>::iterator evi = tmp.end();
    while (bvi != evi) {
      obj.m_map[*(bvi)] = container();
      ++bvi;
    }

    in.close();
    return in;
  }  
  private:
    std::map<key, container> m_map;
    std::string m_file;
};

並在朋友方法“ operator<< ”中

您要打印地圖和通用container怎么辦呢? 如何獲得適當的迭代器來遍歷通用container的內容。

我正在嘗試:

container::iterator cbi;

代碼更新

帶有:

friend std::ostream& operator<< (std::ostream &out, file_to_map<key, container, container_type> &obj)
  {
    typedef typename std::map<key, container>::const_iterator mapItr;
    mapItr mbi = obj.m_map.begin();
    mapItr emi = obj.m_map.end();
    while (mbi != emi) {
      out << " -- " << mbi->first << " -- " << std::endl;
      typename container::const_iterator cbi = mbi->second.begin();
      typename container::const_iterator ebi = mbi->second.end();
      std::copy(cbi, mbi, std::ostream_iterator<container_type>(out, "\t\n"));
      ++mbi;
    }
    return out;
  }

我收到以下編譯器錯誤:

g++ -o file_to_map -Wall ./file_to_map.h ./main.cpp ./code_finder.cpp \
        -L/usr/local/boost_1_48_0/stage/lib -lboost_filesystem -lboost_system -I /usr/local/boost_1_48_0/
In file included from ./code_finder.h:4,
                 from ./code_finder.cpp:1:
./file_to_map.h: In function ‘std::ostream& operator<<(std::ostream&, file_to_map<std::basic_string<char, std::char_traits<char>, std::allocator<char> >, std::set<std::basic_string<char, std::char_traits<char>, std::allocator<char> >, std::less<std::basic_string<char, std::char_traits<char>, std::allocator<char> > >, std::allocator<std::basic_string<char, std::char_traits<char>, std::allocator<char> > > >, std::basic_string<char, std::char_traits<char>, std::allocator<char> > >&)’:
./code_finder.cpp:36:   instantiated from here
./file_to_map.h:62: error: no matching function for call to ‘copy(std::_Rb_tree_const_iterator<std::basic_string<char, std::char_traits<char>, std::allocator<char> > >&, operator<<(std::ostream&, file_to_map<std::basic_string<char, std::char_traits<char>, std::allocator<char> >, std::set<std::basic_string<char, std::char_traits<char>, std::allocator<char> >, std::less<std::basic_string<char, std::char_traits<char>, std::allocator<char> > >, std::allocator<std::basic_string<char, std::char_traits<char>, std::allocator<char> > > >, std::basic_string<char, std::char_traits<char>, std::allocator<char> > >&)::mapItr&, std::ostream_iterator<std::basic_string<char, std::char_traits<char>, std::allocator<char> >, char, std::char_traits<char> >)’

我想念什么?

使用typename

typename container::iterator cbi;

因為iterator是一個從屬名稱,所以它取決於類模板的類型參數container 有趣的是,如果您已在其他地方更正了使用的typename名,例如在operator<<

typedef typename std::map<key, container>::const_iterator mapItr;

由於您正在為地圖使用const_iterator ,這意味着您將使用地圖的const迭代器獲取的容器對象將是const對象,這又意味着您也需要對容器使用const_iterator ,因為它們是的值地圖。 所以我認為您需要使用:

typename container::const_iterator cbi; //use this instead!

回復您的編輯:

我在這里看到一個錯字:

typename container::const_iterator cbi = mbi->second.begin();
typename container::const_iterator ebi = mbi->second.end();
std::copy(cbi, mbi, std::ostream_iterator<container_type>(out, "\t\n"));
            // ^^^ typo

std::copy的第二個參數應該是ebi ,而不是mbi 這就是為什么我通常將這樣的變量命名為beginend ,而不是cbiebi

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM