vector<vector<XXXX>> values

I'd like to know how I can assign the value of a vector<string> to another vector.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
vector<vector<Client>> vvc;
vector<Client> vc;
vector<string> vs;

int j = 0;
for (int i = 0; i < vs.size() / 4; i++)
{
	vc[i].fullName = vs[j];
	vc[i].idcardNo = vs[j + 1];
	string s = vs[j + 2];
	vc[i].accountNumber = (float)atof(s.c_str());
	string d = vs[j + 3];
	vc[i].balance = (float)atof(d.c_str());
	vvc.push_back(vc);
	j = j + 4;
}


I am getting the error that a vector is out of range on the bold line. I checked via debugging that vs[j] has a value of "John Doe", so the error must be from the vc[i].fullName .... can anyone point me towards a solution please?

Thank you.
Does vc[i] exist?

If not, you should create Client, fill all its fiulds and push to the vc.
Or you should allocate memory in vc.
Last edited on
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
    vector<vector<Client>> vvc;
    vector<Client> vc;
    vector<string> vs;

    // ...

    // assert( vs.size()%4 == 0 ) ;
    unsigned j = 0 ;
    while ( j < vs.size() )
    {
        vc.resize(vc.size()+1) ;

        vc.back().fullname = vs[j++] ;
        vc.back().idcardNo = vs[j++] ;
        vc.back().accountNumber = static_cast<float>(atof(vs[j++].c_str())) ;
        vs.back().balance = static_cast<float(atof(vs[j++].c_str())) ;

        //  This doesn't make much sense to me, but what the hay?
        vvc.push_back(vc);
    }
Hello thanks for the answers.

cire .... I got your reasoning. The vvc makes sense, what doesn't make sense is the vector<client> ... hah that should not be a vector but an object of type client .... what the .. was i thinking !!

thanks
Topic archived. No new replies allowed.