vector.erase()

how would i erase elements from my vector without using a constant integer?

my vector is sorted like:
username(1),password(1),name(1),age(1),hours(1),late(1),username(2),password(2),name(2),age(2),hours(2),late(2),username(3),password(3),name(3),age(3),hours(3),late(3) and so on.....

i want to erase elements username(z),password(z)......
but i do not know the value of z before i run my program.

from what i have read so far, erase() has to have a const int but as i do not know which bunch of 6 i will be deleting, how would i go about this?
Last edited on
i have another vector which contains:

username(z),password(z),name(z),age(z),hours(z),late(z)

would this be useful to help erase the elements from the main vector
is this a vector of strings?

it looks like you just want
vec.erase(index_of_username, index_of_username+6);
say you want to delete the 4th one.
0-5 is the first 'record' (0th)
6-11 is the second record (1st) ... and so on.
or, 6*record number from 0 is the one you want.

so the 4th record, then:
vec.erase(vec.begin()+6*recordnumber, vec.begin()+6*recordnumber + 5);
where recordnumber is 4.
that erases [24] to [29]
^^ this should be what you want, or close to it?
Last edited on
but i do not know the value of z before i run my program.

from what i have read so far, erase() has to have a const int


For .erase(), you don't have to know the value at compile time. A value obtained at run-time is perfectly OK

However, storing data like this in a vector isn't the best way. It would be more usual to have a struct to define the data to be stored and then have a vector of that struct. Something like:

1
2
3
4
5
6
7
8
9
10
struct User {
    std::string user;
    std::string pass;
    std::string name;
    unsigned age {};
    unsigned hours {};
    unsigned late {};
};

std::vector<User> users;


Then once you know the vector position (say z), then you can simply do:

 
users.erase(users.begin() + z);

Topic archived. No new replies allowed.