list<string> insertion fails

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • banangroda

    list<string> insertion fails

    Compilation fails at "line.insert(li ne.end(), x.begin(), i);" and I
    can't figure out why. Here is the code:

    /*
    5-1. Design and implement a program to produce a permuted index. A
    permuted
    index is one in which each phrase is indexed by every word in the
    phrase.
    */

    #include <cctype>
    #include <iostream>
    #include <string>
    #include <list>

    using std::cin;
    using std::cout;
    using std::isspace;
    using std::string;
    using std::list;

    int main() {
    string x;
    list<list<strin g indexes;
    while (getline(cin, x)) {
    string::const_i terator i = x.begin();
    list<stringline ;
    while (i < x.end()) {
    while (i < x.end() && isspace(*i)) x.erase(*(i++)) ;
    while (i < x.end() && !isspace(*i)) ++i;
    line.insert(lin e.end(), x.begin(), i);
    }
    indexes.push_ba ck(line);
    }
    for (list<list<stri ng::const_itera tor i = indexes.begin() ;
    i != indexes.end(); ++i) {
    for (list<string>:: const_iterator j = i->begin(); j != i->end(); +
    +j)
    cout << *j << std::endl;
    }
    return 0;
    }

    ... and here is the compilation error:
    g++ 5-1.cpp -o 5-1 -Wall
    5-1.cpp: In function ‘int main()’:
    5-1.cpp:26: error: no matching function for call to
    ‘std::list<std: :basic_string<c har, std::char_trait s<char>,
    std::allocator< char, std::allocator< std::basic_stri ng<char,
    std::char_trait s<char>, std::allocator< char >
    >::insert(std:: _List_iterator< std::basic_stri ng<char,
    std::char_trait s<char>, std::allocator< char >,
    __gnu_cxx::__no rmal_iterator<c har*, std::basic_stri ng<char,
    std::char_trait s<char>, std::allocator< char >,
    __gnu_cxx::__no rmal_iterator<c onst char*, std::basic_stri ng<char,
    std::char_trait s<char>, std::allocator< char >&)’
    /usr/include/c++/4.2/bits/list.tcc:86: note: candidates are: typename
    std::list<_Tp, _Alloc>::iterat or std::list<_Tp,
    _Alloc>::insert (std::_List_ite rator<_Tp>, const _Tp&) [with _Tp =
    std::basic_stri ng<char, std::char_trait s<char>, std::allocator< char>
    >, _Alloc = std::allocator< std::basic_stri ng<char,
    std::char_trait s<char>, std::allocator< char >]
    /usr/include/c++/4.2/bits/stl_list.h:808: note: void
    std::list<_Tp, _Alloc>::insert (std::_List_ite rator<_Tp>, size_t, const
    _Tp&) [with _Tp = std::basic_stri ng<char, std::char_trait s<char>,
    std::allocator< char, _Alloc =
    std::allocator< std::basic_stri ng<char, std::char_trait s<char>,
    std::allocator< char >]
    make: *** [5-1] Error 1
  • Victor Bazarov

    #2
    Re: list&lt;string& gt; insertion fails

    banangroda wrote:
    Compilation fails at "line.insert(li ne.end(), x.begin(), i);" and I
    can't figure out why. Here is the code:
    >
    /*
    5-1. Design and implement a program to produce a permuted index. A
    permuted
    index is one in which each phrase is indexed by every word in the
    phrase.
    */
    >
    #include <cctype>
    #include <iostream>
    #include <string>
    #include <list>
    >
    using std::cin;
    using std::cout;
    using std::isspace;
    using std::string;
    using std::list;
    >
    int main() {
    string x;
    list<list<strin g indexes;
    while (getline(cin, x)) {
    string::const_i terator i = x.begin();
    list<stringline ;
    while (i < x.end()) {
    while (i < x.end() && isspace(*i)) x.erase(*(i++)) ;
    while (i < x.end() && !isspace(*i)) ++i;
    line.insert(lin e.end(), x.begin(), i);
    }
    indexes.push_ba ck(line);
    }
    for (list<list<stri ng::const_itera tor i = indexes.begin() ;
    i != indexes.end(); ++i) {
    for (list<string>:: const_iterator j = i->begin(); j != i->end(); +
    +j)
    cout << *j << std::endl;
    }
    return 0;
    }
    [..]
    Your 'line' is a list of strings. Your 'x.begin()' is an iterator in a
    string. If you intended to insert the entire 'x' string into your list
    of strings (append), then you probably wanted to do

    line.insert(lin e.end(), x);

    If not, describe to us *what it is you're trying to accomplish*.

    V
    --
    Please remove capital 'A's when replying by e-mail
    I do not respond to top-posted replies, please don't ask

    Comment

    • banangroda

      #3
      Re: list&lt;string& gt; insertion fails

      On Jul 29, 8:17 pm, Victor Bazarov <v.Abaza...@com Acast.netwrote:
      banangroda wrote:
      Compilation fails at "line.insert(li ne.end(), x.begin(), i);" and I
      can't figure out why. Here is the code:
      >
      /*
         5-1. Design and implement a program to produce a permuted index.A
      permuted
         index is one in which each phrase is indexed by every word in the
      phrase.
      */
      >
      #include <cctype>
      #include <iostream>
      #include <string>
      #include <list>
      >
      using std::cin;
      using std::cout;
      using std::isspace;
      using std::string;
      using std::list;
      >
      int main() {
         string x;
         list<list<strin g indexes;
         while (getline(cin, x)) {
                 string::const_i terator i = x.begin();
                 list<stringline ;
                 while (i < x.end()) {
                         while (i < x.end() && isspace(*i)) x.erase(*(i++)) ;
                         while (i < x.end() && !isspace(*i)) ++i;
                         line.insert(lin e.end(), x.begin(), i);
                 }
                 indexes.push_ba ck(line);
         }
         for (list<list<stri ng::const_itera tor i = indexes.begin() ;
         i != indexes.end(); ++i) {
                 for (list<string>:: const_iterator j = i->begin(); j != i->end(); +
      +j)
                         cout << *j << std::endl;
         }
         return 0;
      }
      [..]
      >
      Your 'line' is a list of strings.  Your 'x.begin()' is an iterator in a
      string.  If you intended to insert the entire 'x' string into your list
      of strings (append), then you probably wanted to do
      >
          line.insert(lin e.end(), x);
      >
      If not, describe to us *what it is you're trying to accomplish*.
      >
      V
      --
      Please remove capital 'A's when replying by e-mail
      I do not respond to top-posted replies, please don't ask
      I want to insert the contents of 'x' from its start through 'i' into
      'line'.

      Comment

      • Victor Bazarov

        #4
        Re: list&lt;string& gt; insertion fails

        banangroda wrote:
        [..]
        I want to insert the contents of 'x' from its start through 'i' into
        'line'.
        See 'substr' member of 'string'.

        V
        --
        Please remove capital 'A's when replying by e-mail
        I do not respond to top-posted replies, please don't ask

        Comment

        Working...