Maxxumas Maxxumas - 1 month ago 5
C++ Question

c++ Best Way to access properties of a vector element

So this is a question more so for my own knowledge.
Is there any kind of best practice for accessing multiple properties of a single element in a vector? Sorry if that question doesn't make much sense. Let me give a scenario:

I was working on some code and got to a point where I created a vector of a struct. To make sure we aren't duplicating the data in the vector, we check multiple pieces of the incoming data against already existing data in the vector via a for loop. The data is being added from multiple, independent sources, sometimes at rapid succession, so it's a bit difficult to do a master ID for each entry before this point. The code ends up looking something like this:

vector<testStruct> test;
void addDataToVector(int _iData1, string _sData2, unsigned int _uiData3){
testStruct tmp;
tmp.iData1 = _iData1;
tmp.sData2 = _sData2;
tmp.uiData3 = _uiData3;

if(test.size() > 0){
for(size_t i = 0;; i < test.size(); i++){
if(test[i].iData1 == tmp.iData1 &&
test[i].sData2 == tmp.sData2 &&
test[i].uiData3 == tmp.uiData3){
break;
}else{
test.push_back(tmp);
}
}
}else{
test.push_back(tmp);
}
}


In some other programming languages, I have learned that doing multiple calls on the same element of an array like this is a bit wasteful. In Action Script 3, I have learned that it is better to assign the element's content to a temporary variable then use that variable for the multiple calls instead of constantly accessing the vector over and over.
I am wondering if there is any such thing to be wary of in c++? I understand that a vector is a pointer... I think. But I'm not sure if that alleviates a lot of this or if I need to still be on the look out for such a thing. I have tried looking around for similar data/answers but wasn't exactly sure how to word my search.

Any help or pointers (hehe get it?) would be appreciated.

Answer

Not only a vector is a pointer (sort of, more like a wrapper around an array), but it's also a template, meaning that its whole implementation is typically accessible to the compiler at the time you use it. Meaning that the compiler knows exactly what test[i] is, and may optimize away this stuff for you. You need not to concern yourself with such things. Element access is very fast for vectors.

You may wish to overload operator== for your structure, though, so you can write test[i] == tmp instead. That will improve readability for your code, but you should only do that if that fields being equal actually means the objects are being equal, that is, if that makes sense semantically.

Or, if at some point you just wish to access an element just once for some other reason rather than performance (say, the access expression is too long to repeat comfortably), you can easily do this:

const testStruct &element = test[i];

Note the &—you avoid the unnecessary cost of copying this way, and access the vector element directly. Remove const if you need to modify the element.

Comments