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: Assign vector to array

STL: Assign vector to array

Scheduled Pinned Locked Moved C / C++ / MFC
c++graphicsdata-structureshelpquestion
4 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.
  • M Offline
    M Offline
    Mr Brainley
    wrote on last edited by
    #1

    I need to convert a vector to an array. It's no problem really, only one thing bothers me. If i use an iterator to iterate through the vector, i have no counter for the array, unless i declare an extra variable for that. If i use a counter to go through the vector, i bypass the recommended STL-way for iterating - iterators. So, does anyone know any elegant way for that ? I know that it's not really a problem, i'm just trying to optimize my solution. wbr Mr.Brainley

    M 1 Reply Last reply
    0
    • M Mr Brainley

      I need to convert a vector to an array. It's no problem really, only one thing bothers me. If i use an iterator to iterate through the vector, i have no counter for the array, unless i declare an extra variable for that. If i use a counter to go through the vector, i bypass the recommended STL-way for iterating - iterators. So, does anyone know any elegant way for that ? I know that it's not really a problem, i'm just trying to optimize my solution. wbr Mr.Brainley

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

      A vector's storage is contiguous, so you can do &my_vector[0] to get an (array-like) pointer to the first element.

      --Mike-- Visual C++ MVP :cool: LINKS~! Ericahist | PimpFish | CP SearchBar v3.0 | C++ Forum FAQ

      S 1 Reply Last reply
      0
      • M Michael Dunn

        A vector's storage is contiguous, so you can do &my_vector[0] to get an (array-like) pointer to the first element.

        --Mike-- Visual C++ MVP :cool: LINKS~! Ericahist | PimpFish | CP SearchBar v3.0 | C++ Forum FAQ

        S Offline
        S Offline
        Sceptic Mole
        wrote on last edited by
        #3

        Michael Dunn wrote:

        A vector's storage is contiguous, so you can do &my_vector[0] to get an (array-like) pointer to the first element.

        very encapsulated :suss:

        S 1 Reply Last reply
        0
        • S Sceptic Mole

          Michael Dunn wrote:

          A vector's storage is contiguous, so you can do &my_vector[0] to get an (array-like) pointer to the first element.

          very encapsulated :suss:

          S Offline
          S Offline
          Stephen Hewitt
          wrote on last edited by
          #4

          The C++ Standard Library requires that the elements of a vector are in contiguous memory. There's no problem with code like Mike suggested and encapsulation has not been violated as the contiguous storage requirement is documented and required behaviour for a std::vector.

          Steve

          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