Which one is good option? Object or reference or pointer into STL | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
+ 1

Which one is good option? Object or reference or pointer into STL

Suppose I have a class called T. I would like to store objects into vector... Assume that I have done enough of reserve to avoid relocation of data on every push_back vector<T> or vector<T&> which is choice? I would prefer vector<T> and would do emplace_back instead of push_back... This emplace would not create object and then copy... It simply place object in memory. Would you have same opinion on this? I mean above is correct , right? Does vector<T> and doing emplace is simillar to vector<T&> ? Or do we have special case of vector<T&>? Any chance of opting vector<T*> over earlirr mentioned two?

16th Jun 2020, 10:05 AM
Ketan Lalcheta
Ketan Lalcheta - avatar
7 Answers
+ 1
in principle, emplacement functions should sometimes be more efficient than their insertion counterparts(i.e, insert, push_front, push_back, insert_after).and what makes it possible for emplacement functions to outperform insertion functions is their more flexible interface. insertion functions take objects to be inserted, while emplacement functions take constructor arguments for objects to be inserted. this difference permits emplacement functions to avoid the creation and destruction of temporary objects that insertion functions can necessitate. correct with shared_ptr you can have many consumers sharing responsibility for a dynamic object.
16th Jun 2020, 2:26 PM
MO ELomari
+ 1
you can't have vector of references (the component type of vector must be assignable. references are not assignable, you can only initialize them once when they are declared, and you cannot make them reference something else later). pointers to references and references to references are prohibited in C++ (Standard §8.3.2/4). you can make the vector to hold pointers, but you need to be sure that these pointers will remain valid. i mean if someone deletes an object, pointed by a pointer in vector, the pointer becomes invalid. you need to be sure that this will not happen, because you can't check for NULL, a pointer will not become NULL, if someone deletes the pointed object. the best solution here would be to use some smart pointers (using smart pointers leaves the ownership and destruction semantics up to them).
16th Jun 2020, 12:14 PM
MO ELomari
+ 1
supporting code for vector<T> and vector<T&> https://code.sololearn.com/cHycwWKk6H87 okay....smart_pointer seems a good option, but there also we have to use shared_pointer right? again with shared pointer, it is two copy of pointer referring to same object?is this correct? emplace_back is best in my view....What is the thoughts on this?
16th Jun 2020, 12:57 PM
Ketan Lalcheta
Ketan Lalcheta - avatar
+ 1
the downside of std::reference_wrapper is that you can’t use the regular "." operator to get at the members of T. You need to call .get() or bind it to a real T&. maybe someday we can overload "operator." too.
16th Jun 2020, 2:48 PM
MO ELomari
+ 1
what i mean is that you can't do something like: vecPerson[0].setName((char*)"A_1"); // this won't compile
16th Jun 2020, 3:26 PM
MO ELomari
+ 1
okay...Updated code for this learning... Thanks a lot...Something new I learnt about get as I was totally reliable on emplace till now
16th Jun 2020, 4:00 PM
Ketan Lalcheta
Ketan Lalcheta - avatar
0
oh I see.... I never know this and hence I tried this just now on Sololearn mobile app. After doing the reference_wrapper on vector, I tried in the code as below: Person& temp = vecPerson[0]; temp.setName((char*)"A_1"); it updates A to A_1 and no issue...No extra destructor call as it's just reference. Now just remove reference in updated code i.e. something like below Person temp = vecPerson[0]; temp.setName((char*)"A_1"); It still compiles without the need of something you mentioned like get.... Obviously this is not a reference and hence you will observe one extra destructor call of A_1. Is my understanding correct or .get is something else?
16th Jun 2020, 3:04 PM
Ketan Lalcheta
Ketan Lalcheta - avatar