PDA

View Full Version : convert iterator



mickey
18th March 2006, 02:06
Hi,

vector<Some>::iterator ii;
is possbile convert an iterator to int? I'd like retrieve, let an ii, the number (int) of element indexed from ii in the <vector>
Thanks

high_flyer
18th March 2006, 10:09
I am not sure what you mean, but if you want to know how many elemets the vrctor holds use ii.size().

mickey
18th March 2006, 18:31
no that!
If I have:


vector <Job> job (10);
vector <Job>::iterator ii;
for (ii=job.begin(); ii != job.end(); ++ii)
if ((*ii).beautiful == true)
(*ii).myFunction(ii);

myFunction(vector <Job>::iterator ii) {
vector <Some> some (10);
vector <Some>::iterator cu;
cu = ii +5; //here ii may be 1,2,3 .....
(*cu).printfNumber_of_elemet_of_Some_cu_Is_referin g;
}

Are you understand? thanks

yop
18th March 2006, 19:59
Why don't you use a QMap instead if you wnat to assign an "index" value to every element in your container?

mickey
18th March 2006, 23:58
I don't want assign an index; I'd like only to know it! Do I use <map>?

jacek
19th March 2006, 02:29
Try:
std::cerr << cu - job.begin() << std::endl;

mickey
19th March 2006, 14:21
Hi, I'm sorry but I need to put it in a variabile (don't print). But Can I covert iterator to int?
I need to do something like this
Thanks

jacek
19th March 2006, 14:29
Hi, I'm sorry but I need to put it in a variabile (don't print).
Just think. There's enough information in this thread to solve the problem.

brcain
20th March 2006, 22:59
The iterator doesn't know about the contents of the container. There is a clear separation between containers, iterators, and algorithms. This is intentional. You want the element index (position) in the container. But, what if one of the elements from the container is removed. Now, the index is no longer valid ... as well as the iterator (unless it's a list iterator).

If you state what you are trying to accomplish (rather than implementation question), I'd be glad to make a suggestion.

Cheers,
Ben