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. Using vector over index?

Using vector over index?

Scheduled Pinned Locked Moved C / C++ / MFC
databasegraphicsquestion
7 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.
  • L Offline
    L Offline
    Lost User
    wrote on last edited by
    #1

    Hello, does std::vector have an index operator, i mean can i read data from vector directly over an index wthout search items in it!? like this:

    vector<CObject>VectorObjects;
    // push some Data to vector
    VectorObjects.push_back(myCObject);
    // then retrive Data from :
    myNexCObject = VectorObjects[0]; // is that posible, and safe!?

    thanks in advance termal

    C N S 3 Replies Last reply
    0
    • L Lost User

      Hello, does std::vector have an index operator, i mean can i read data from vector directly over an index wthout search items in it!? like this:

      vector<CObject>VectorObjects;
      // push some Data to vector
      VectorObjects.push_back(myCObject);
      // then retrive Data from :
      myNexCObject = VectorObjects[0]; // is that posible, and safe!?

      thanks in advance termal

      C Offline
      C Offline
      Cedric Moonen
      wrote on last edited by
      #2

      Yes, std::vector override the [] operator, so you can safely do it. Of course, if you try to access an index that is outside the bounds your vector, then'll get an exception.


      Cédric Moonen Software developer
      Charting control [v1.2]

      L 1 Reply Last reply
      0
      • L Lost User

        Hello, does std::vector have an index operator, i mean can i read data from vector directly over an index wthout search items in it!? like this:

        vector<CObject>VectorObjects;
        // push some Data to vector
        VectorObjects.push_back(myCObject);
        // then retrive Data from :
        myNexCObject = VectorObjects[0]; // is that posible, and safe!?

        thanks in advance termal

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

        use vector::at. Ex. myNexCObject = VectorObjects.at(0);

        J 1 Reply Last reply
        0
        • N nbugalia

          use vector::at. Ex. myNexCObject = VectorObjects.at(0);

          J Offline
          J Offline
          jhwurmbach
          wrote on last edited by
          #4

          std::vector.at() does validate the vector boundaries. The operator[] does not. It will happily access outside the vector. No problem when you know that, though.


          Though I speak with the tongues of men and of angels, and have not money, I am become as a sounding brass, or a tinkling cymbal.
          George Orwell, "Keep the Aspidistra Flying", Opening words

          L 1 Reply Last reply
          0
          • C Cedric Moonen

            Yes, std::vector override the [] operator, so you can safely do it. Of course, if you try to access an index that is outside the bounds your vector, then'll get an exception.


            Cédric Moonen Software developer
            Charting control [v1.2]

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

            Hello, thanks for answer and help! regards termal

            1 Reply Last reply
            0
            • J jhwurmbach

              std::vector.at() does validate the vector boundaries. The operator[] does not. It will happily access outside the vector. No problem when you know that, though.


              Though I speak with the tongues of men and of angels, and have not money, I am become as a sounding brass, or a tinkling cymbal.
              George Orwell, "Keep the Aspidistra Flying", Opening words

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

              Hello, thanks for input!! regards termal

              1 Reply Last reply
              0
              • L Lost User

                Hello, does std::vector have an index operator, i mean can i read data from vector directly over an index wthout search items in it!? like this:

                vector<CObject>VectorObjects;
                // push some Data to vector
                VectorObjects.push_back(myCObject);
                // then retrive Data from :
                myNexCObject = VectorObjects[0]; // is that posible, and safe!?

                thanks in advance termal

                S Offline
                S Offline
                Signal 9
                wrote on last edited by
                #7

                You mentioned seaching also. If this is something you are looking for, then I suggest using the std::map class instead of a vector.

                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