compilation error with const_reverse_iterator

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • subramanian100in@yahoo.com, India

    compilation error with const_reverse_iterator

    Consider the following program:

    #include <iostream>
    #include <map>
    #include <string>
    #include <utility>
    #include <algorithm>

    using namespace std;

    int main()
    {
    map<string, intsi;

    string word;

    while (cin >word)
    ++si[word];

    multimap<int, stringis;

    for (map<string, int>::const_ite rator i = si.begin(); i != si.end(); +
    +i)
    is.insert(make_ pair(i->second, i->first));

    for (multimap<int, string>::const_ reverse_iterato r r = is.rbegin(); r !
    = is.rend(); ++r)
    cout << r->second << " " << r->first << endl;

    return 0;
    }

    Under g++, I get compilation error for the line

    for (multimap<int, string>::const_ reverse_iterato r r = is.rbegin(); r !
    = is.rend(); ++r)

    The actual error message is

    error: no match for 'operator!=' in 'r != std::multimap<_ Key, _Tp,
    _Compare, _Alloc>::rend() [with _Key = int, _Tp = std::string,
    _Compare = std::less<int>, _Alloc = std::allocator< std::pair<const
    int, std::string]()'

    However this program compiles fine under VC++ 2005 Express Edition.

    I use the following compilation command under g++.

    g++ -std=c++98 -pedantic -Wall -Wextra word_count.cpp

    Kindly explain why I am getting error for the above mentioned line
    under g++ only.

    Thanks
    V.Subramanian

  • Barry

    #2
    Re: compilation error with const_reverse_i terator

    subramanian100i n@yahoo.com, India wrote:
    Consider the following program:
    >
    #include <iostream>
    #include <map>
    #include <string>
    #include <utility>
    #include <algorithm>
    >
    using namespace std;
    >
    int main()
    {
    map<string, intsi;
    >
    string word;
    >
    while (cin >word)
    ++si[word];
    >
    multimap<int, stringis;
    >
    for (map<string, int>::const_ite rator i = si.begin(); i != si.end(); +
    +i)
    is.insert(make_ pair(i->second, i->first));
    >
    for (multimap<int, string>::const_ reverse_iterato r r = is.rbegin(); r !
    = is.rend(); ++r)
    cout << r->second << " " << r->first << endl;
    >
    return 0;
    }
    >
    Under g++, I get compilation error for the line
    >
    for (multimap<int, string>::const_ reverse_iterato r r = is.rbegin(); r !
    = is.rend(); ++r)
    Ah,
    As you declare
    >multimap<int , stringis;
    then /is.rend()/ and /is.rbegin()/ are both reverse_iterato rs
    r = is.begin(), compiles as is convertible from reverse_iterato r to
    const_reverse_i terator.

    Threre are no /operator !=/ or /operator ==/ between the two types.

    so you can simply choose reverse_iterato r for r
    >
    The actual error message is
    >
    error: no match for 'operator!=' in 'r != std::multimap<_ Key, _Tp,
    _Compare, _Alloc>::rend() [with _Key = int, _Tp = std::string,
    _Compare = std::less<int>, _Alloc = std::allocator< std::pair<const
    int, std::string]()'
    >
    However this program compiles fine under VC++ 2005 Express Edition.
    >
    I think this is an extension from VC.

    Comment

    • Kai-Uwe Bux

      #3
      Re: compilation error with const_reverse_i terator

      subramanian100i n@yahoo.com, India wrote:
      Consider the following program:
      >
      #include <iostream>
      #include <map>
      #include <string>
      #include <utility>
      #include <algorithm>
      >
      using namespace std;
      >
      int main()
      {
      map<string, intsi;
      >
      string word;
      >
      while (cin >word)
      ++si[word];
      >
      multimap<int, stringis;
      Note that this is a non-const object.
      >
      for (map<string, int>::const_ite rator i = si.begin(); i != si.end(); +
      +i)
      is.insert(make_ pair(i->second, i->first));
      >
      for (multimap<int, string>::const_ reverse_iterato r r = is.rbegin(); r !
      = is.rend(); ++r)
      Note that is.rend() returns reverse_iterato r and not const_reverse_i terator
      since "is" is a non-const object. Thus, you compare

      const_reverse_i terate != reverse_iterato r

      cout << r->second << " " << r->first << endl;
      >
      return 0;
      }
      >
      Under g++, I get compilation error for the line
      >
      for (multimap<int, string>::const_ reverse_iterato r r = is.rbegin(); r !
      = is.rend(); ++r)
      >
      The actual error message is
      >
      error: no match for 'operator!=' in 'r != std::multimap<_ Key, _Tp,
      _Compare, _Alloc>::rend() [with _Key = int, _Tp = std::string,
      _Compare = std::less<int>, _Alloc = std::allocator< std::pair<const
      int, std::string]()'
      Yup. That's what you get.

      However this program compiles fine under VC++ 2005 Express Edition.
      >
      I use the following compilation command under g++.
      >
      g++ -std=c++98 -pedantic -Wall -Wextra word_count.cpp
      It's a matter which STL implementation you use.
      Kindly explain why I am getting error for the above mentioned line
      under g++ only.
      You hit upon a defect in the language.

      std::map<>::rev erse_iterator is defined to be

      std::reverse_it erator< std::map<>::ite rator >

      and std::map<>::con st_reverse_iter ator is defined to be

      std::reverse_it erator< std::map<>::con st_iterator >

      The standard only requires that std::reverse_it erator supports comparison
      for reverse_iterato rs with identical underlying iterator types. Thus, g++
      is formally correct.

      HOWEVER, this has been fixed in the draft for the next revision of the C++
      standard. It also has been fixed in g++. Your code compiles fine with
      g++-4.1.1.


      Best

      Kai-Uwe Bux

      Comment

      Working...