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. ATL / WTL / STL
  4. Vector

Vector

Scheduled Pinned Locked Moved ATL / WTL / STL
questiongraphicsperformance
8 Posts 5 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
    mcsherry
    wrote on last edited by
    #1

    Hi, I currently use a vector to store a set of records. However there is now a limit to the number of elements the vector contains and once it is filled we have to remove the first element and insert the new one at the back. But as an added complication we aren't allowed any dynamic memory allocation so what needs to be done is: 1) Remove first element from list but do not free it's memory 2) overwrite the removed elements data with the new data 3) insert the updated element to the end of the vector What is the best way to do this? I am not limited to only storing the data in a vector so if any other containers are more suitable then let me know! TIA,

    C N J 3 Replies Last reply
    0
    • M mcsherry

      Hi, I currently use a vector to store a set of records. However there is now a limit to the number of elements the vector contains and once it is filled we have to remove the first element and insert the new one at the back. But as an added complication we aren't allowed any dynamic memory allocation so what needs to be done is: 1) Remove first element from list but do not free it's memory 2) overwrite the removed elements data with the new data 3) insert the updated element to the end of the vector What is the best way to do this? I am not limited to only storing the data in a vector so if any other containers are more suitable then let me know! TIA,

      C Offline
      C Offline
      Chris Losinger
      wrote on last edited by
      #2

      i'd recommend a std::list, but if you can't allocate any memory, you can't use any STL collections (since they'll allocate memory themselves). so maybe a simple array: RecordType array[MAX_SIZE]; ?

      image processing toolkits | batch image processing

      M N 2 Replies Last reply
      0
      • C Chris Losinger

        i'd recommend a std::list, but if you can't allocate any memory, you can't use any STL collections (since they'll allocate memory themselves). so maybe a simple array: RecordType array[MAX_SIZE]; ?

        image processing toolkits | batch image processing

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

        Hi, thanks for your reply, the dynamic memory allocation can not happen in any code I write but it is allowed to happen in legacy or other code (i.e. STL, MFC, etc...). What happens at present is: 1) Start of program allocate enough space to the vector 2) Set the current position variable to 0 (i.e. we actually have no elements) ... x) read in a record (again this is into an already defined variable) x1) if not at the end of the vector copy the record into the next free position of the vector and increment the current position x2) clear the contents of the read in variable (so its empty next time it gets a record) The more I look at this code/mechanism the more clunky and bad it looks but I haven't got any other ideas how to do this, so if you have any suggestions that would be great. Regarding using a list what methods would allow me to get the behaviour of moving the first element to the end? cheers, Andy

        M 1 Reply Last reply
        0
        • M mcsherry

          Hi, thanks for your reply, the dynamic memory allocation can not happen in any code I write but it is allowed to happen in legacy or other code (i.e. STL, MFC, etc...). What happens at present is: 1) Start of program allocate enough space to the vector 2) Set the current position variable to 0 (i.e. we actually have no elements) ... x) read in a record (again this is into an already defined variable) x1) if not at the end of the vector copy the record into the next free position of the vector and increment the current position x2) clear the contents of the read in variable (so its empty next time it gets a record) The more I look at this code/mechanism the more clunky and bad it looks but I haven't got any other ideas how to do this, so if you have any suggestions that would be great. Regarding using a list what methods would allow me to get the behaviour of moving the first element to the end? cheers, Andy

          M Offline
          M Offline
          mcsherry
          wrote on last edited by
          #4

          Hi (again), I've just looked into the list container more and have figured out the following: myList.splice(myList.end(),myList, myList.begin()); This seems to do what I was, is this the correct way to do it (and is it safe!)? thanks,

          C 1 Reply Last reply
          0
          • M mcsherry

            Hi (again), I've just looked into the list container more and have figured out the following: myList.splice(myList.end(),myList, myList.begin()); This seems to do what I was, is this the correct way to do it (and is it safe!)? thanks,

            C Offline
            C Offline
            Chris Losinger
            wrote on last edited by
            #5

            mcsherry wrote:

            myList.end()

            you'd probably want myList.end()-1 there, since myList.end() isn't an element. otherwise, that should do what you want. or, you can use pop_back / push_front

            image processing toolkits | batch image processing

            1 Reply Last reply
            0
            • C Chris Losinger

              i'd recommend a std::list, but if you can't allocate any memory, you can't use any STL collections (since they'll allocate memory themselves). so maybe a simple array: RecordType array[MAX_SIZE]; ?

              image processing toolkits | batch image processing

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

              Chris Losinger wrote:

              i'd recommend a std::list

              Or a deque[^]


              Programming Blog utf8-cpp

              1 Reply Last reply
              0
              • M mcsherry

                Hi, I currently use a vector to store a set of records. However there is now a limit to the number of elements the vector contains and once it is filled we have to remove the first element and insert the new one at the back. But as an added complication we aren't allowed any dynamic memory allocation so what needs to be done is: 1) Remove first element from list but do not free it's memory 2) overwrite the removed elements data with the new data 3) insert the updated element to the end of the vector What is the best way to do this? I am not limited to only storing the data in a vector so if any other containers are more suitable then let me know! TIA,

                N Offline
                N Offline
                Nathan Holt at EMOM
                wrote on last edited by
                #7

                mcsherry wrote:

                I currently use a vector to store a set of records. However there is now a limit to the number of elements the vector contains and once it is filled we have to remove the first element and insert the new one at the back. But as an added complication we aren't allowed any dynamic memory allocation so what needs to be done is: 1) Remove first element from list but do not free it's memory 2) overwrite the removed elements data with the new data 3) insert the updated element to the end of the vector What is the best way to do this? I am not limited to only storing the data in a vector so if any other containers are more suitable then let me know!

                I don't think STL provides any class that specificly avoids dynamic memory allocation, but ther is the notion of an allocator class which could be designed to just manage members of a fixed sized array. It might be simpler to not use an STL container. Boost might have the kind of class your looking for. Failing that, you could implement your own class that does this. It would require an array, a first element index, and either a last element index of a content count. When the array gets full, you could update the first element index and assign a new value to the former first element. Nathan

                1 Reply Last reply
                0
                • M mcsherry

                  Hi, I currently use a vector to store a set of records. However there is now a limit to the number of elements the vector contains and once it is filled we have to remove the first element and insert the new one at the back. But as an added complication we aren't allowed any dynamic memory allocation so what needs to be done is: 1) Remove first element from list but do not free it's memory 2) overwrite the removed elements data with the new data 3) insert the updated element to the end of the vector What is the best way to do this? I am not limited to only storing the data in a vector so if any other containers are more suitable then let me know! TIA,

                  J Offline
                  J Offline
                  John R Shaw
                  wrote on last edited by
                  #8

                  Well first off you are using dynamic memory just by using the ‘vector’ class, or any other STL collection, to store your records. Whether or not is frees the memory used by the first element is another story, it may just move the other elements down by one address, making the previous last element space empty (break out the pencil and paper). If the implementation of the ‘vector’ you are using does that, then you can simply remove the first element and add the new element to the end, and you have met the requirement (no special coding required); provided your class does not allocate memory in its copy constructor. If you were dealing with a situation where you are supposed to write your own ‘vector’ type class, then I would say that instructor wants you to learn about ‘in place’ construction (look it up) using ‘new”. Sorry if that did not help, but it is something you need to examine in detail to understand.

                  INTP "Program testing can be used to show the presence of bugs, but never to show their absence."Edsger Dijkstra

                  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