I often do something like this to concatenate multiple vectors returned from functions (possibly class functions):
#include <vector>
#include <iostream>
using namespace std;
vector<int> v1;
const vector<int>& F1() {
cout << "F1 was called" << endl;
return v1;
}
int main() {
vector<int> Concat;
Concat.insert(Concat.end(), F1().begin(), F1().end());
return 0;
}
As I expected, it F1()is called twice, which may be undesirable if this is an expensive function call. An alternative is to copy the return value F1()to a temporary vector, which will require only one function call, but will perform a copy operation, which may be undesirable if the vector is large. The only alternative I can imagine is to create a pointer to a temporary vector and assign it a return value F1()as follows:
int main() {
vector<int> Concat;
const vector<int>* temp = &F1();
Concat.insert(Concat.end(), temp->begin(), temp->end());
return 0;
}
? , . , , . ?