STL reverse_iterator错误

时间:2019-03-18 07:10:50

标签: c++ gcc iterator

我正在尝试使用该算法构建霍夫曼树,但是我已经陷入了某种困境。我首先将字符串作为用户输入,然后找到输入的实际位表示形式以与压缩位序列进行比较。然后,我使用std :: map将字母映射到其频率。现在我想做的是,获得一个反向迭代器,并将键值作为一个节点插入到霍夫曼树中。但是我在迭代器部分有很多错误。

#include <iostream>
#include <stdio.h>
#include <string>
#include <string.h>
#include <map>
#include <bitset>
#include <iterator>
#include <algorithm>
#include "htree.cpp"

// utility function to calculate the occurence of 'ith' letter //
const int countChar(const std::string& istring, char key) {
  int n = istring.length(); int iCount = 0;
  for(int i = 0; i < n; ++i) if(istring[i] == key) iCount++; 
  return iCount;
}

// calculates and stores the frequency of each letter in string //
void calcFreq(std::map<char, int>& fMap, const std::string& istring) {
  int n = istring.length();
  for(int i = 0; i < n; ++i) {
    fMap.insert(std::pair <char, int> (istring[i], 
countChar(istring,istring[i])));
  }
}

//prints the binary representation of the input string //
const void bitRep(const std::string& istring) {
  for(std::size_t i = 0; i < istring.size(); ++i) 
    std::cout<<std::bitset<8>(istring[i]);
}

int main() {

  std::string istring;
  getline(std::cin, istring);

  std::cout<<"\n Bit Representation: \n";
  bitRep(istring);
  std::cout<<std::endl;

  std::map <char, int> fMap;
  std::map<char, int>::iterator it;
  calcFreq(fMap, istring);

  std::cout<<"\tCHAR\tVALUE\n";
  for(it = fMap.begin(); it != fMap.end(); ++it) 
    std::cout<<'\t'<<it->first<<'\t'<<it->second<<'\n';
 // auto i = fMap.rbegin();
  std::map<char, int>::const_reverse_iterator i = fMap.rbegin();
  Node* node = (Node*)malloc(sizeof(Node)); 
  while(i != fMap.rend()) {
    for(std::map<char, int>::const_reverse_iterator j = i; j != i + 2; j++) 
      node = newNode(j->first, j->second);
    std::advance(i,2);
  }

  return 0;

}

我得到了大量无法理解的错误,如下所示。我真的不知道我在做什么错,而且我已经差不多一天了。帮助将不胜感激。

λ g++ HAL.cpp
In file included from c:\mingw\lib\gcc\mingw32\6.3.0\include\c++\bits\stl_algobase.h:67:0,
                 from c:\mingw\lib\gcc\mingw32\6.3.0\include\c++\bits\char_traits.h:39,
                 from c:\mingw\lib\gcc\mingw32\6.3.0\include\c++\ios:40,
                 from c:\mingw\lib\gcc\mingw32\6.3.0\include\c++\ostream:38,
                 from c:\mingw\lib\gcc\mingw32\6.3.0\include\c++\iostream:39,
                 from HAL.cpp:1:
c:\mingw\lib\gcc\mingw32\6.3.0\include\c++\bits\stl_iterator.h: In instantiation of 'std::reverse_iterator<_Iterator> std::reverse_iterator<_Iterator>::operator+(std::reverse_iterator<_Iterator>::difference_type) const [with _Iterator = std::_Rb_tree_const_iterator<std::pair<const char, int> >; std::reverse_iterator<_Iterator>::difference_type = int]':
HAL.cpp:52:69:   required from here
c:\mingw\lib\gcc\mingw32\6.3.0\include\c++\bits\stl_iterator.h:233:41: error: no match for 'operator-' (operand types are 'const std::_Rb_tree_const_iterator<std::pair<const char, int> >' and 'std::reverse_iterator<std::_Rb_tree_const_iterator<std::pair<const char, int> > >::difference_type {aka int}')
       { return reverse_iterator(current - __n); }
                                 ~~~~~~~~^~~~~
c:\mingw\lib\gcc\mingw32\6.3.0\include\c++\bits\stl_iterator.h:333:5: note: candidate: template<class _Iterator> decltype ((__x.base() - __y.base())) std::operator-(const std::reverse_iterator<_Iterator>&, const std::reverse_iterator<_Iterator>&)
     operator-(const reverse_iterator<_Iterator>& __x,
     ^~~~~~~~
c:\mingw\lib\gcc\mingw32\6.3.0\include\c++\bits\stl_iterator.h:333:5: note:   template argument deduction/substitution failed:
c:\mingw\lib\gcc\mingw32\6.3.0\include\c++\bits\stl_iterator.h:233:41: note:   'const std::_Rb_tree_const_iterator<std::pair<const char, int> >' is not derived from 'const std::reverse_iterator<_Iterator>'
       { return reverse_iterator(current - __n); }
                                 ~~~~~~~~^~~~~
c:\mingw\lib\gcc\mingw32\6.3.0\include\c++\bits\stl_iterator.h:387:5: note: candidate: template<class _IteratorL, class _IteratorR> decltype ((__y.base() - __x.base())) std::operator-(const std::reverse_iterator<_Iterator>&, const std::reverse_iterator<_IteratorR>&)
     operator-(const reverse_iterator<_IteratorL>& __x,
     ^~~~~~~~
c:\mingw\lib\gcc\mingw32\6.3.0\include\c++\bits\stl_iterator.h:387:5: note:   template argument deduction/substitution failed:
c:\mingw\lib\gcc\mingw32\6.3.0\include\c++\bits\stl_iterator.h:233:41: note:   'const std::_Rb_tree_const_iterator<std::pair<const char, int> >' is not derived from 'const std::reverse_iterator<_Iterator>'
       { return reverse_iterator(current - __n); }
                                 ~~~~~~~~^~~~~
c:\mingw\lib\gcc\mingw32\6.3.0\include\c++\bits\stl_iterator.h:1186:5: note: candidate: template<class _IteratorL, class _IteratorR> decltype ((__x.base() - __y.base())) std::operator-(const std::move_iterator<_IteratorL>&, const std::move_iterator<_IteratorR>&)
     operator-(const move_iterator<_IteratorL>& __x,
     ^~~~~~~~
c:\mingw\lib\gcc\mingw32\6.3.0\include\c++\bits\stl_iterator.h:1186:5: note:   template argument deduction/substitution failed:
c:\mingw\lib\gcc\mingw32\6.3.0\include\c++\bits\stl_iterator.h:233:41: note:   'const std::_Rb_tree_const_iterator<std::pair<const char, int> >' is not derived from 'const std::move_iterator<_IteratorL>'
       { return reverse_iterator(current - __n); }
                                 ~~~~~~~~^~~~~
c:\mingw\lib\gcc\mingw32\6.3.0\include\c++\bits\stl_iterator.h:1193:5: note: candidate: template<class _Iterator> decltype ((__x.base() - __y.base())) std::operator-(const std::move_iterator<_IteratorL>&, const std::move_iterator<_IteratorL>&)
     operator-(const move_iterator<_Iterator>& __x,
     ^~~~~~~~
c:\mingw\lib\gcc\mingw32\6.3.0\include\c++\bits\stl_iterator.h:1193:5: note:   template argument deduction/substitution failed:
c:\mingw\lib\gcc\mingw32\6.3.0\include\c++\bits\stl_iterator.h:233:41: note:   'const std::_Rb_tree_const_iterator<std::pair<const char, int> >' is not derived from 'const std::move_iterator<_IteratorL>'
       { return reverse_iterator(current - __n); }

和顺便说一句,newNode的函数声明在另一个文件中:

Node* newNode(char data, int freq);

1 个答案:

答案 0 :(得分:2)

以下是导致错误的代码:

for(std::map<char, int>::const_reverse_iterator j = i; j != i + 2; j++) 
    node = newNode(j->first, j->second);

您不能使用i + 2,因为迭代器没有运算符+,您可以像这样更改它:

std::map<char, int>::const_reverse_iterator y = i;
std::advance(y,2);
for(std::map<char, int>::const_reverse_iterator j = i; j != y; j++)