sorting through 2 vectors at the same time as one?

closed account (2EwbqMoL)
so right now im working on a class that has two vectors at it's heart, one that stores a list of objects (Classes), and the other stores how many of those classes it has acquired (instead of saying milk, milk, it would say milk (2), for example).

pretty simple as far as how it looks, and how it works, surprisingly, as long as you pop in the data at the same time, the quantity and class get put right next to each other, and can easily be sequentially output with a simple vec1name[i] << vec2name[i] with cout or whatever you'd like to use.

unfortunately, editing it later is another thing. vectors arent the funnest things to scan/iterate through, but I guess its more functional than an array for that... i like the effectiveness of my design, but because IM not sure I wanna sit and figure out this part, I may just list each class more than once instead of having an amount vector --

but I figured I'd ask anyway -- say I iterate through for the position of value N in vector 1, is there a way I can correlate the position I found for value N and either delete or subtract from vector 2? I havent really tried hard yet, but it may be possible to output the position in a variable or... something...

and how about adding to that vector -- If I use this design, If/switch statements are going to be a MUST -- it won't be a terrible nest, but it will be there. every time the class needs to store something else it will have to scan the vector through to see first IF the object is stored in Vec 1, and then How much of the item is stored in Vec 1, so that if something is being added, it will +1 the value and not add another entry to Vec 1, and so that way when it loses a value, it can -1 instead of removing the entry entirely.

thanks
closed account (2EwbqMoL)
I'm also working about a way to leave identical objects in a single vector without erasing them. so far the only code I could really think of

1
2
3
4
std::vector<object>::iterator position = std::find(inventory.begin(), inventory.end(), name);
				if (position != inventory.end()){
					inventory.erase(position);
                                }


erases all erases all instances of that object from the vector - its sort of the same problem I was having with two vectors -- which I thought would actually make this task simpler because if I could run a scan through both entries (same position in each vector), I could add and subtract to the value, and use erase if the value was < 2...

Last edited on
closed account (2EwbqMoL)
Edit: It would seem that adding a simple return; to the end of the function (probably good practice, even for void functions) stops it from going function or delete crazy and makes it usable.

so far I am continuing to use only one vector -- but if someone can offer me advice on scanning through multiple vectors in unison and using them as one so I could couple 2-datatypes it would be a lot nicer really...
Last edited on
Make a single vector of a pair:

std::vector< std::pair<type_one, type_two> > whatever;

You'll have to provide a custom comparison function for sort though.
spiroth10 wrote:
so right now im working on a class that has two vectors at it's heart, one that stores a list of objects (Classes), and the other stores how many of those classes it has acquired (instead of saying milk, milk, it would say milk (2), for example).
I think you are using the wrong design here. Could you elaborate on why you need this? There are much better ways of doing this than micromanaging two vectors.
When you find yourself using parallel arrays, then it is a sign that you need to be using an object that takes those types in the arrays and combines them in a single class. Or do what firedraco suggested.
Topic archived. No new replies allowed.