How to Return a Vector From a Function in C++?
Last Updated :
25 Nov, 2024
In C++, by returning vectors, users to return multiple values at once from a function as the elements of vector. In this article, we will learn how to return a vector from a function in C++.
The recommended way to return a vector from a function is by using return vector by value method. Let’s take a look at a simple example:
C++
#include <bits/stdc++.h>
using namespace std;
// Function that returns a vector
vector<int> f() {
vector<int> v = {1, 2, 3, 4, 5};
// Return the vector
return v;
}
int main() {
// Store the returned vector
vector<int> v = f();
for (int i : v)
cout << i << " ";
return 0;
}
Explanation: We simply have to specify the vector of the given type as the return type of the function return it using return statement.
Note: In this method, the copy of the vector that is returned is created and passed to the caller function.
There are also some other methods in C++ to return a vector from a function. Some of them are as follows:
By Reference
In addition to returning a vector by value, we can also return a vector by reference. The return type of the function is defined as the reference to the vector of given type.
C++
#include <bits/stdc++.h>
using namespace std;
// Function that returns a reference to a vector
vector<int>& f() {
// Creating a static vector
static vector<int> v = { 1, 2, 3, 4, 5};
// Returning a vector
return v;
}
int main() {
vector<int> v = f();
for (int i : v)
cout << i << " ";
return 0;
}
In this method, the reference of the original vector is returned, so it must be ensure that the vector being returned is not destroyed when the function ends. This can be done by either dynamically creating vector using new or declaring vector as static.
By Pointer
Returning a vector as a pointer is similar to returning vector as reference. The only difference will be that the syntax of pointers will be used instead of reference.
C++
#include <bits/stdc++.h>
using namespace std;
// Function that returns a pointer to a vector
vector<int>* f() {
// Creating the dynamic vector
vector<int>* v = new vector<int>
({1, 2, 3, 4, 5});
// Returning pointer to the vector
return v;
}
int main() {
// Create a vector pointer
vector<int>* v = f();
for (int i : *v)
cout << i << " ";
return 0;
}
Again, it must be ensured that the vector is not destroyed after it is returned from the function.
By r-value Reference
In this method, we return the r-value reference of the vector which transfers ownership of the vector's resources to the caller without creating a temporary copy. The move() function is used to provide the r-value reference to the vector.
C++
#include <bits/stdc++.h>
using namespace std;
// Class to test the construction/destruction
class A {
public:
A() {
cout << "Default Constructor" << endl;
}
A(const A& obj) {
cout << "Copy Constructor" << endl;
}
~A() {
cout << "Destructor Called" << endl;
}
};
// Function that returns the r-value reference
vector<A>&& f() {
// Creating the dynamic vector
vector<A>* v = new vector<A>(1);
return move(*v);
}
int main() {
vector<A> v = f();
return 0;
}
OutputDefault Constructor
Destructor Called
Explanation: As we can see, the vector elements are created and destroyed only once and no copy is created.
Similar Reads
How to Return a Pointer from a Function in C++? In C++, we can return a pointer from a function which is useful when we want to return large data structures that cannot be returned by value. However, it must be done carefully to avoid memory leaks, dangling pointers, and other issues related to dynamic memory management. In this article, we will
2 min read
How to Create a Pointer to a Function in C++? In C++, a function pointer is a variable that stores the address of a function that can later be called through that function pointer. It is useful for passing functions as parameters to other functions(callback functions) or storing them in data structures. In this article, we will learn how to use
2 min read
How to Extract a Subvector from a Vector in C++? A subvector is a continuous part from the original vector. In this article, we will learn how to extract the subvector from a vector in C++.The simplest method to extract the subvector form a vector is by using vector's range constructor. Letâs take a look at an example:C++#include <bits/stdc++.h
3 min read
How to Capture std::vector in Lambda Function? In C++, lambda functions allow users to define an inline function anywhere in the program. They are also able to capture the objects from outside its definition. In this article, we will look at how to capture a std::vector object in lambda functions in C++. Capture std::vector in Lambda FunctionTo
2 min read
How to convert a Vector to Set in C++ This article shows how to convert a vector to a set in C++. Example: Input: vector = {1, 2, 3, 1, 1} Output: set = {1, 2, 3} Input: vector = {1, 2, 3, 3, 5}Output: set = {1, 2, 3, 5} Below are the various ways to do the required conversion: Method 1: Naive SolutionGet the vector to be converted.Crea
4 min read