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
CODE PROJECT For Those Who Code
  • Home
  • Articles
  • FAQ
Community
  1. Home
  2. General Programming
  3. C / C++ / MFC
  4. iterator

iterator

Scheduled Pinned Locked Moved C / C++ / MFC
question
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.
  • N Offline
    N Offline
    nss
    wrote on last edited by
    #1

    I have an iterator that is pointing to a certain element. I want to overwrite this element with the next one following it. *I is the current element. But if I do ++I to get to the next element I lose my place.... I dont think *I = *(++I) is quite going to do it. (what would this do anyways? ) :confused: thanks, ns

    M J 2 Replies Last reply
    0
    • N nss

      I have an iterator that is pointing to a certain element. I want to overwrite this element with the next one following it. *I is the current element. But if I do ++I to get to the next element I lose my place.... I dont think *I = *(++I) is quite going to do it. (what would this do anyways? ) :confused: thanks, ns

      M Offline
      M Offline
      Michael Dunn
      wrote on last edited by
      #2

      Just copy it :) iterator i2 = i; *++i2 = *i; --Mike-- Personal stuff:: Ericahist | Homepage Shareware stuff:: 1ClickPicGrabber | RightClick-Encrypt CP stuff:: CP SearchBar v2.0.2 | C++ Forum FAQ ---- "Just because the box has 2 gigabytes of memory doesn't mean you get to use it all!"   -- Rico Mariani, CLR perf guy

      N 1 Reply Last reply
      0
      • M Michael Dunn

        Just copy it :) iterator i2 = i; *++i2 = *i; --Mike-- Personal stuff:: Ericahist | Homepage Shareware stuff:: 1ClickPicGrabber | RightClick-Encrypt CP stuff:: CP SearchBar v2.0.2 | C++ Forum FAQ ---- "Just because the box has 2 gigabytes of memory doesn't mean you get to use it all!"   -- Rico Mariani, CLR perf guy

        N Offline
        N Offline
        nss
        wrote on last edited by
        #3

        Thanks!!!

        1 Reply Last reply
        0
        • N nss

          I have an iterator that is pointing to a certain element. I want to overwrite this element with the next one following it. *I is the current element. But if I do ++I to get to the next element I lose my place.... I dont think *I = *(++I) is quite going to do it. (what would this do anyways? ) :confused: thanks, ns

          J Offline
          J Offline
          Jorgen Sigvardsson
          wrote on last edited by
          #4

          Or: *I = *(I + 1);. -- Unser Tanz ist so wild! Ein neuer böser Tanz. Alle gegen Alle!

          N 1 Reply Last reply
          0
          • J Jorgen Sigvardsson

            Or: *I = *(I + 1);. -- Unser Tanz ist so wild! Ein neuer böser Tanz. Alle gegen Alle!

            N Offline
            N Offline
            Nemanja Trifunovic
            wrote on last edited by
            #5

            Only if I is a random-access iterator. ;)

            J 1 Reply Last reply
            0
            • N Nemanja Trifunovic

              Only if I is a random-access iterator. ;)

              J Offline
              J Offline
              Jorgen Sigvardsson
              wrote on last edited by
              #6

              Yes, you are right. I thought forward iterator would be ok. I guess one will have to use std::advance instead then. But I'll have to copy the iterator, as std::advance mutates the iterator (passed by reference) :| But I guess one could always do

              template <typename Iterator, typename Distance>
              Iterator my_advance(const Iterator& i, Distance d) {
              Iterator tmp = i;
              std::advance(tmp, d);
              return tmp;
              }

              and then

              *I = *(my_advance(I, 1));

              If the compiler is smart and inlines the function, there's no overhead penalty. -- Unser Tanz ist so wild! Ein neuer böser Tanz. Alle gegen Alle!

              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