I have two <int> vectors: Customers and Wait_Times. The position of an entry in either vector will represent data about the same customer. So if I find Customer[4], for example, Wait_Times[4] will contain data pertaining to the same customer.
My objective is to find the Wait_Time for a given customer. I know the customer number and search for it in the Customers vector like this:
vector<int>::const_iterator it = find( customers.begin(), customers.end(), this_customer );
Then, I want to take this result and use it to search out the same position in the Wait_Times vector. I tried this, but it doesn't work:
my_wait = Wait_Times[it];
Can anyone please tell me how to use this iterator to get the appropriate entry from the Wait_Times vector?
Right, but what I am trying to do is return the position within the vector. In my example, say that I am looking for the position in the customers vector of customer number '1111'. I use the find function with '1111' as this_customer, and it finds this customer in position [4]. If I knew that it was in position [4], I would simply say my_wait = Wait_Times[4], but I don't have any idea what position in the customers vector the customer will be found in.
Can I use the iterator for this purpose? I've looked at all the reference information, and I know that if I dereference the iterator, I would return '1111' from my example, but I'm hoping to return [4] using the find function. The way that I have it doesn't compile.
So you want to convert the iterator to an index, basically.
You can do it this way:
1 2 3 4 5 6 7 8
myvector_t::iterator i = myvector.find( foo );
int index = i - myvector.begin();
// 'index' is now a 0 based index
// so you can do this:
myvector[index] = bar;
Of course... you could just use the iterator instead of the [] operator. Why do you need to use the [] operator?
I don't, necessarily. I'm just looking for the best way to accomplish this. Using it as an index is just what I know how to do (more or less). I'd love to just use the iterator, but I don't know how and can't figure it out. Can you tell me?