Results 1 to 9 of 9

Thread: convert iterator

  1. #1
    Join Date
    Jan 2006
    Posts
    976
    Thanks
    53
    Qt products
    Qt3
    Platforms
    Windows

    Lightbulb convert iterator

    Hi,
    Qt Code:
    1. vector<Some>::iterator ii;
    To copy to clipboard, switch view to plain text mode 
    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
    Regards

  2. #2
    Join Date
    Jan 2006
    Location
    Munich, Germany
    Posts
    4,714
    Thanks
    21
    Thanked 418 Times in 411 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows

    Default Re: convert iterator

    I am not sure what you mean, but if you want to know how many elemets the vrctor holds use ii.size().

  3. #3
    Join Date
    Jan 2006
    Posts
    976
    Thanks
    53
    Qt products
    Qt3
    Platforms
    Windows

    Default Re: convert iterator

    no that!
    If I have:
    Qt Code:
    1. vector <Job> job (10);
    2. vector <Job>::iterator ii;
    3. for (ii=job.begin(); ii != job.end(); ++ii)
    4. if ((*ii).beautiful == true)
    5. (*ii).myFunction(ii);
    6.  
    7. myFunction(vector <Job>::iterator ii) {
    8. vector <Some> some (10);
    9. vector <Some>::iterator cu;
    10. cu = ii +5; //here ii may be 1,2,3 .....
    11. (*cu).printfNumber_of_elemet_of_Some_cu_Is_refering;
    12. }
    To copy to clipboard, switch view to plain text mode 
    Are you understand? thanks
    Regards

  4. #4
    Join Date
    Jan 2006
    Location
    Athens - Greece
    Posts
    219
    Thanks
    3
    Thanked 1 Time in 1 Post
    Qt products
    Qt3 Qt4
    Platforms
    Unix/X11 Windows

    Default Re: convert iterator

    Why don't you use a QMap instead if you wnat to assign an "index" value to every element in your container?

  5. #5
    Join Date
    Jan 2006
    Posts
    976
    Thanks
    53
    Qt products
    Qt3
    Platforms
    Windows

    Default Re: convert iterator

    I don't want assign an index; I'd like only to know it! Do I use <map>?
    Regards

  6. #6
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    5,372
    Thanks
    28
    Thanked 976 Times in 912 Posts
    Qt products
    Qt3 Qt4
    Platforms
    Unix/X11 Windows

    Default Re: convert iterator

    Try:
    Qt Code:
    1. std::cerr << cu - job.begin() << std::endl;
    To copy to clipboard, switch view to plain text mode 

  7. #7
    Join Date
    Jan 2006
    Posts
    976
    Thanks
    53
    Qt products
    Qt3
    Platforms
    Windows

    Default Re: convert iterator

    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
    Regards

  8. #8
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    5,372
    Thanks
    28
    Thanked 976 Times in 912 Posts
    Qt products
    Qt3 Qt4
    Platforms
    Unix/X11 Windows

    Default Re: convert iterator

    Quote Originally Posted by mickey
    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.

  9. The following user says thank you to jacek for this useful post:

    mickey (20th March 2006)

  10. #9
    Join Date
    Feb 2006
    Location
    US
    Posts
    173
    Thanks
    16
    Thanked 5 Times in 5 Posts
    Qt products
    Qt4 Qt5
    Platforms
    Unix/X11 Windows Android

    Default Re: convert iterator

    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

Similar Threads

  1. convert from QString to char[sizeof(...)]
    By adamatic in forum Qt Programming
    Replies: 4
    Last Post: 3rd September 2011, 09:05
  2. convert ebcdic to ascii
    By jaca in forum Qt Programming
    Replies: 1
    Last Post: 17th June 2008, 20:49
  3. Read binary file and convert to QString
    By jaca in forum Qt Programming
    Replies: 12
    Last Post: 13th June 2008, 23:05
  4. convert QString to int
    By mattia in forum Newbie
    Replies: 2
    Last Post: 4th January 2008, 09:10
  5. iterator
    By mickey in forum General Programming
    Replies: 6
    Last Post: 23rd May 2006, 10:28

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Digia, Qt and their respective logos are trademarks of Digia Plc in Finland and/or other countries worldwide.