list<string> insertion fails

  • Follow


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 =3D 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 =3D indexes.begin();
	i !=3D indexes.end(); ++i) {
		for (list<string>::const_iterator j =3D i->begin(); j !=3D i->end(); +
+j)
			cout << *j << std::endl;
	}
	return 0;
}

=2E.. and here is the compilation error:
g++ 5-1.cpp -o 5-1 -Wall
5-1.cpp: In function =91int main()=92:
5-1.cpp:26: error: no matching function for call to
=91std::list<std::basic_string<char, std::char_traits<char>,
std::allocator<char> >, std::allocator<std::basic_string<char,
std::char_traits<char>, std::allocator<char> > >
>::insert(std::_List_iterator<std::basic_string<char,
std::char_traits<char>, std::allocator<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> > >&)=92
/usr/include/c++/4.2/bits/list.tcc:86: note: candidates are: typename
std::list<_Tp, _Alloc>::iterator std::list<_Tp,
_Alloc>::insert(std::_List_iterator<_Tp>, const _Tp&) [with _Tp =3D
std::basic_string<char, std::char_traits<char>, std::allocator<char>
>, _Alloc =3D std::allocator<std::basic_string<char,
std::char_traits<char>, std::allocator<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 =3D std::basic_string<char, std::char_traits<char>,
std::allocator<char> >, _Alloc =3D
std::allocator<std::basic_string<char, std::char_traits<char>,
std::allocator<char> > >]
make: *** [5-1] Error 1
0
Reply fredrikhcs (6) 7/29/2008 5:55:14 PM

banangroda wrote:
> 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;
> }
> [..]

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(line.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
0
Reply v.Abazarov (13255) 7/29/2008 6:17:06 PM


On Jul 29, 8:17=A0pm, Victor Bazarov <v.Abaza...@comAcast.net> wrote:
> banangroda wrote:
> > Compilation fails at "line.insert(line.end(), x.begin(), i);" and I
> > can't figure out why. Here is the code:
>
> > /*
> > =A0 =A05-1. Design and implement a program to produce a permuted index.=
 A
> > permuted
> > =A0 =A0index is one in which each phrase is indexed by every word in th=
e
> > 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() {
> > =A0 =A0string x;
> > =A0 =A0list<list<string> > indexes;
> > =A0 =A0while (getline(cin, x)) {
> > =A0 =A0 =A0 =A0 =A0 =A0string::const_iterator i =3D x.begin();
> > =A0 =A0 =A0 =A0 =A0 =A0list<string> line;
> > =A0 =A0 =A0 =A0 =A0 =A0while (i < x.end()) {
> > =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0while (i < x.end() && isspace(*i=
)) x.erase(*(i++));
> > =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0while (i < x.end() && !isspace(*=
i)) ++i;
> > =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0line.insert(line.end(), x.begin(=
), i);
> > =A0 =A0 =A0 =A0 =A0 =A0}
> > =A0 =A0 =A0 =A0 =A0 =A0indexes.push_back(line);
> > =A0 =A0}
> > =A0 =A0for (list<list<string> >::const_iterator i =3D indexes.begin();
> > =A0 =A0i !=3D indexes.end(); ++i) {
> > =A0 =A0 =A0 =A0 =A0 =A0for (list<string>::const_iterator j =3D i->begin=
(); j !=3D i->end(); +
> > +j)
> > =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0cout << *j << std::endl;
> > =A0 =A0}
> > =A0 =A0return 0;
> > }
> > [..]
>
> Your 'line' is a list of strings. =A0Your 'x.begin()' is an iterator in a
> string. =A0If you intended to insert the entire 'x' string into your list
> of strings (append), then you probably wanted to do
>
> =A0 =A0 line.insert(line.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'.
0
Reply fredrikhcs (6) 7/29/2008 6:42:43 PM

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
0
Reply v.Abazarov (13255) 7/29/2008 6:45:09 PM

3 Replies
13 Views

(page loaded in 0.668 seconds)


Reply: