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