Can list container contain an vector object, such as list< vector<string> >??

E

ehui928

hi, everybody
I am a newbie in STL. When I compile the following program under
gcc4.0, I got a the following errors.
I wonder whether the form of list< vector<string> > is correct in STL
?
// test.cpp
#include <iostream>
#include <fstream>
#include <vector>
#include <string>
#include <list>

using namespace std;

typedef vector<string> RECORD;
typedef list< RECORD > DataInstances;

DataInstances getRecords(const string& filename);
void print_data(const DataInstances& d);
void print_vector(const vector<string>& s);

const string filename = "mydata";

int main(void)
{
DataInstances data;

data = getRecords(filename);
print_data(data);

return 0;
}

DataInstances getRecords(const string& filename)
{
.......
return instances;

}
void print_vector(const vector<string>& s)
{
......
}

void print_data(const DataInstances& d)
{
DataInstances::iterator pos;
// the compiler seems to say "pos = d.begin()" is error? why
?
for (pos = d.begin(); pos != d.end(); ++pos) { //test.cpp:86
,complier say this line has error
print_vector(*pos);
}
}

Following is the compile errors:

In function 'void print_data(const DataInstances&)':
test.cpp:86: error: no match for 'operator=' in 'pos = (+
d)->std::list<_Tp, _Alloc>::begin [with _Tp =
std::vector<std::basic_string<char, std::char_traits<char>,
std::allocator<char> >, std::allocator<std::basic_string<char,
std::char_traits<char>, std::allocator<char> > > >, _Alloc =
std::allocator<std::vector<std::basic_string<char,
std::char_traits<char>, std::allocator<char> >,
std::allocator<std::basic_string<char, std::char_traits<char>,
std::allocator<char> > > > >]()'
/usr/lib/gcc/i386-redhat-linux/4.0.2/../../../../include/c++/4.0.2/bits/stl_list.h:112:
note: candidates are:
std::_List_iterator<std::vector<std::basic_string<char,
std::char_traits<char>, std::allocator<char> >,
std::allocator<std::basic_string<char, std::char_traits<char>,
std::allocator<char> > > > >&
std::_List_iterator<std::vector<std::basic_string<char,
std::char_traits<char>, std::allocator<char> >,
std::allocator<std::basic_string<char, std::char_traits<char>,
std::allocator<char> > > > >::eek:perator=(const
std::_List_iterator<std::vector<std::basic_string<char,
std::char_traits<char>, std::allocator<char> >,
std::allocator<std::basic_string<char, std::char_traits<char>,
std::allocator<char> > > > >&)


Any one give me some help ?
Thanks!
 
K

Kai-Uwe Bux

ehui928 said:
hi, everybody
I am a newbie in STL. When I compile the following program under
gcc4.0, I got a the following errors.
I wonder whether the form of list< vector<string> > is correct in STL
?

It is.
// test.cpp
#include <iostream> [snip]

void print_data(const DataInstances& d)
{
DataInstances::iterator pos;
// the compiler seems to say "pos = d.begin()" is error? why
?

You try to obtain an iterator to a const container. Try const_iterator.
for (pos = d.begin(); pos != d.end(); ++pos) { //test.cpp:86
,complier say this line has error
print_vector(*pos);
}
}
[snip]


Best

Kai-Uwe Bux
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,766
Messages
2,569,569
Members
45,045
Latest member
DRCM

Latest Threads

Top