Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. C / C++ / MFC
  4. STL iterator

STL iterator

Scheduled Pinned Locked Moved C / C++ / MFC
c++databasedata-structures
6 Posts 4 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • A Offline
    A Offline
    act_x
    wrote on last edited by
    #1

    Need to know if I can directly access elements in an STL array based on the Iterator .. As for eg List contains { 1,2,3,4,5,6,7,8 } if List::Iterator iter = List.begin() Now to access nth element can the routine be like int GetElementAt( int index) { // range check iter = List.begin(); iter+=index; return *iter ; }

    Engineering is the effort !

    C M L 3 Replies Last reply
    0
    • A act_x

      Need to know if I can directly access elements in an STL array based on the Iterator .. As for eg List contains { 1,2,3,4,5,6,7,8 } if List::Iterator iter = List.begin() Now to access nth element can the routine be like int GetElementAt( int index) { // range check iter = List.begin(); iter+=index; return *iter ; }

      Engineering is the effort !

      C Offline
      C Offline
      cje
      wrote on last edited by
      #2

      act_x wrote:

      Engineering is the effort !

      what happened when you tried it?

      cje

      1 Reply Last reply
      0
      • A act_x

        Need to know if I can directly access elements in an STL array based on the Iterator .. As for eg List contains { 1,2,3,4,5,6,7,8 } if List::Iterator iter = List.begin() Now to access nth element can the routine be like int GetElementAt( int index) { // range check iter = List.begin(); iter+=index; return *iter ; }

        Engineering is the effort !

        M Offline
        M Offline
        markkuk
        wrote on last edited by
        #3

        A std::list isn't an "array" and list iterators don't support random access. Vector and deque iterators support the += operator.

        1 Reply Last reply
        0
        • A act_x

          Need to know if I can directly access elements in an STL array based on the Iterator .. As for eg List contains { 1,2,3,4,5,6,7,8 } if List::Iterator iter = List.begin() Now to access nth element can the routine be like int GetElementAt( int index) { // range check iter = List.begin(); iter+=index; return *iter ; }

          Engineering is the effort !

          L Offline
          L Offline
          Lost User
          wrote on last edited by
          #4

          act_x wrote:

          iter+=index;

          try std::advance(iter, index); Might work

          A 1 Reply Last reply
          0
          • L Lost User

            act_x wrote:

            iter+=index;

            try std::advance(iter, index); Might work

            A Offline
            A Offline
            act_x
            wrote on last edited by
            #5

            will for(int i=0;iEngineering is the effort !

            L 1 Reply Last reply
            0
            • A act_x

              will for(int i=0;iEngineering is the effort !

              L Offline
              L Offline
              Lost User
              wrote on last edited by
              #6

              yes the ++ operator should be supported for the iterator I sugest you look at the doco

              1 Reply Last reply
              0
              Reply
              • Reply as topic
              Log in to reply
              • Oldest to Newest
              • Newest to Oldest
              • Most Votes


              • Login

              • Don't have an account? Register

              • Login or register to search.
              • First post
                Last post
              0
              • Categories
              • Recent
              • Tags
              • Popular
              • World
              • Users
              • Groups