B
banangroda
Compilation fails at "line.insert(line.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<string> > indexes;
while (getline(cin, x)) {
string::const_iterator i = x.begin();
list<string> line;
while (i < x.end()) {
while (i < x.end() && isspace(*i)) x.erase(*(i++));
while (i < x.end() && !isspace(*i)) ++i;
line.insert(line.end(), x.begin(), i);
}
indexes.push_back(line);
}
for (list<list<string> >::const_iterator 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<char, std::char_traits<char>,
__gnu_cxx::__normal_iterator<char*, std::basic_string<char,
std::char_traits<char>, std::allocator<char> > >,
__gnu_cxx::__normal_iterator<const char*, std::basic_string<char,
std::char_traits<char>, std::allocator<char> > >&)’
/usr/include/c++/4.2/bits/list.tcc:86: note: candidates are: typename
std::list<_Tp, _Alloc>::iterator std::list<_Tp,
/usr/include/c++/4.2/bits/stl_list.h:808: note: void
std::list<_Tp, _Alloc>::insert(std::_List_iterator<_Tp>, size_t, const
_Tp&) [with _Tp = std::basic_string<char, std::char_traits<char>,
std::allocator<char> >, _Alloc =
std::allocator<std::basic_string<char, std::char_traits<char>,
std::allocator<char> > >]
make: *** [5-1] Error 1
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<string> > indexes;
while (getline(cin, x)) {
string::const_iterator i = x.begin();
list<string> line;
while (i < x.end()) {
while (i < x.end() && isspace(*i)) x.erase(*(i++));
while (i < x.end() && !isspace(*i)) ++i;
line.insert(line.end(), x.begin(), i);
}
indexes.push_back(line);
}
for (list<list<string> >::const_iterator 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<char, std::char_traits<char>,
std::char_traits<char>, std::allocator<char> > >,std::allocator<std::basic_string<char said:::insert(std::_List_iterator<std::basic_string<char,
__gnu_cxx::__normal_iterator<char*, std::basic_string<char,
std::char_traits<char>, std::allocator<char> > >,
__gnu_cxx::__normal_iterator<const char*, std::basic_string<char,
std::char_traits<char>, std::allocator<char> > >&)’
/usr/include/c++/4.2/bits/list.tcc:86: note: candidates are: typename
std::list<_Tp, _Alloc>::iterator std::list<_Tp,
std::char_traits<char>, std::allocator<char> > >]const _Tp&) [with _Tp = said:, _Alloc = std::allocator<std::basic_string<char,
/usr/include/c++/4.2/bits/stl_list.h:808: note: void
std::list<_Tp, _Alloc>::insert(std::_List_iterator<_Tp>, size_t, const
_Tp&) [with _Tp = std::basic_string<char, std::char_traits<char>,
std::allocator<char> >, _Alloc =
std::allocator<std::basic_string<char, std::char_traits<char>,
std::allocator<char> > >]
make: *** [5-1] Error 1