How do I avoid copying whole values of vector kws
into req_kws
(the vector of vectors) below, while keeping two containers kws
and req_kws
in my code?
vector<vector<string>> req_kws;
for (string& request : requests) {
vector<string>& kws = req_kws.back(); //<-- define &kws to empty req_kws
kws = split(kwsStr, ","); //split is a parsing function upon string kwsStr
req_kws.push_back(kws);
}
I used in line number 3
vector<string>& kws = req_kws.back();
In this way, instead of storing all values of kws
vector, only its reference is stored into req_kws
.
But, I am getting a segmentation fault during the runtime. Is there a way to fix it?
Thank you for your help.
Additional note;
Please note my constraint stated above that I need to keep both line number 4 (kws) and 5 (req_kws) intact, and want to use line number 3 of reference (&) or something similar to save the execution time from the same values being stored to both kws and req_kws. I have the other parts of the codes with the same problem, which are more complicate so that I can Not simply combine two lines of 4 and 5 into one.
Additional Question (Similar problem):
Thank you for your solution down below. However, how about the following case, which is similar as above but more complicated.
vector<unordered_map<string, string>> ads_kwsBids;
for (const auto& kwBid : kwsBids) {
…
unordered_map<string, string>& kwsBidsMap = ads_kwsBids.back(); //to prevent from being whole container copied
for (unsigned j = 0; j < nsize; j++) {
unsigned jdx = 2 * j;
kwsBidsMap[splitedStr[jdx]] = splitedStr[jdx + 1];// constructing map kwsBidsMap
}
ads_kwsBids.push_back(kwsBidsMap);
}
In line number 4, that is,
unordered_map<string, string>& kwsBidsMap = ads_kwsBids.back();
Because of the reference &
, it yields the segmentation fault. But, the reference is also the one that prevents kwsBidMap
from being copied into ads_kwsBids
. Is there a way still to use the reference &
on kwsBidMap
and not get the segmentation fault?