In order to code a vector in C++, the vector library has to be included in the program. The vector library has the vector class from which vector objects can be instantiated (created).
The program in which all the code samples of this article are, begins with:
#include <vector>
#include <string>
using namespace std;
A vector of strings is used.
Article Content
– Returning Vector by Normal Vector Name
– Returning a Vector Literal
– Returning a Vector Reference
– Returning a Vector Pointer
Returning Vector by Normal Vector Name
Let the vector of interest be:
The vector is a list of items in a small grocery store. The name, store of this vector, is to be sent as an argument to a function, whose parameter is a vector, but with the name, vtr. The function of interest can be:
return vtr;
}
Notice the return type of the function definition. The name of the vector is store. This is the argument for the function call. The parameter for the function corresponding to the vector is:
Note that the argument for the function and the parameter name are different (they can still be the same). As soon as the function starts executing, the following statement is made:
This statement is equivalent to the following two statements:
vector<string> vtr = store;
And so, vtr is a copy of the vector, store. At this point, there are two vectors with the same content in memory for the program. An appropriate C++ main function for the code can be:
vector<string> v = fn(store);
for (int i=0; i<v.size(); i++)
cout << v[i] << ", ";
cout << endl;
return 0;
}
Notice that the word store, is the argument of the function call. When the function is called, two copies of the same vector content occur in memory. The function (call) returns a vector, which is received by another vector, v. By the time the program finishes, there are three vectors of the same copy in memory. These three copies of the same content can be reduced to one copy by using a reference vector, or pointer vector.
Here is the full code for this program:
#include <vector>
#include <string>
using namespace std;
vector<string> store = {"bread", "meat", "rice", "Tomato sauce", "Cheese"};
vector<string> fn(vector<string> vtr) {
return vtr;
}
int main() {
vector<string> v = fn(store);
for (int i=0; i<v.size(); i++)
cout << v[i] << ", ";
cout << endl;
return 0;
}
The output for the above program is:
Returning a Vector Literal
Today (in 2022), a vector literal is the same as an array literal. This literal is called an initializer_list, today in C++. So, returning a vector literal by a function, is the same as returning an initializer_list. Let the initlializer_list be:
Let the function definition to return the initializer_list be,
return {"bread", "meat", "rice", "tomato sauce", "Cheese"};
}
The initializer_list is composed on the spot in the return statement, and returned. The function definition has no parameter, but has the same return type as its counterpart in the previous section. Let the main C++ function be:
vector<string> v = fn();
for (int i=0; i<v.size(); i++)
cout << v[i] << ", ";
cout << endl;
return 0;
}
The function call, this time, has no argument, but the return value is received by the same vector and type of the previous section.
By the time the program is completing, would there be two copies of the same vector in memory? No. There would be only one copy, which is v. The initializer_list is a kind of expression, called a rvalue. When this kind of expression is no longer needed in memory, can it be erased by C++ in order to have more memory space? It is not important whether it remains in memory after it has been used while the program continues to run. It would be erased if its space is needed. The program output is:
The full code for the program is here:
#include <vector>
#include <string>
using namespace std;
vector<string> fn() {
return {"bread", "meat", "rice", "tomato sauce", "Cheese"};
}
int main() {
vector<string> v = fn();
for (int i=0; i<v.size(); i++)
cout << v[i] << ", ";
cout << endl;
return 0;
}
The output for the above program is:
Returning a Vector Reference
The program here will do what the first program above has done, but only with one copy of the same vector. There will be three different names for the same vector, though. Let the vector of interest be:
The variable, store here, is an ordinary name. Let the function of interest be:
return vtr;
}
Note the presence and position of & in the parameter. It means vtr is a referenced (synonym) vector, and not a copy of the argument to be sent. Note the presence and position of & in the return type. It means that the reference (synonym) of a vector will be returned by the function. Note that the inside statement, “return vtr;” does not have &. Let the C++ main function be:
vector<string> *v = &fn(store);
for (int i=0; i<v->size(); i++)
cout << (*v)[i] << ", ";
cout << endl;
return 0;
}
The signature of the function definition, and the function call statement, are:
and
respectively. Note again, the presence and position of &, in the return type of the function definition. Note the presence and position of & in the function call statement. The argument of the function call is the ordinary name of the vector, store. The function returns a reference, and it is received by a pointer, v.
And so, there are three different variables in the program, all referring to the same vector memory location (the function returned &vtr, which is a synonym for store).
The full code of the program is:
#include <vector>
#include <string>
using namespace std;
vector<string> store = {"bread", "meat", "rice", "tomato sauce", "Cheese"};
vector<string> & fn(vector<string> &vtr) {
return vtr;
}
int main() {
vector<string> *v = &fn(store);
for (int i=0; i<v->size(); i++)
cout << (*v)[i] << ", ";
cout << endl;
return 0;
}
The output is:
Returning a Vector Pointer
The program here will do what the first program above has done, but only with one copy of the same vector. There will be three different names for the same vector. Let the vector of interest be:
The variable, store here, is an ordinary name. Let the function of interest be:
return vtr;
}
Note the presence and position of * in the parameter. It means vtr is a pointer vector, and not a copy of any vector argument to be sent. Note the presence and position of * in the return type. Again, note that the inside statement, “return vtr;” does not have & or *. Let the C++ main function be:
vector<string> *v = fn(&store);
for (int i=0; i<v->size(); i++)
cout << (*v)[i] << ", ";
cout << endl;
return 0;
}
The signature of the function definition, and the function call statement, are:
and
respectively. Note the presence and position of * in the return type of the function definition. Note the presence and position of & in the function call statement; it is in front of the argument, store, and not in front of fn(), which does not have & or *. The function returns a reference, and it is received by a pointer, v.
And so, there are three different variables in the program, all referring to the same vector memory location.
The full code of the program is here:
#include <vector>
#include <string>
using namespace std;
vector<string> store = {"bread", "meat", "rice", "tomato sauce", "Cheese"};
vector<string> * fn(vector<string> *vtr) {
return vtr;
}
int main() {
vector<string> *v = fn(&store);
for (int i=0; i<v->size(); i++)
cout << (*v)[i] << ", ";
cout << endl;
return 0;
}
The output is:
Conclusion
A function can return a vector by its normal name. A function can return a vector literal (initializer_list), to be received by a normal vector (name). A vector can return a vector reference, to be received by a vector pointer. A vector can return a vector pointer, still to be received by another vector pointer.