Return Vector in C++

Return vector in C++

In this post, we will see how to return vector in C++.

vectors can be returned from a function in C++ using two methods: return by value and return by reference. In this article, we will discuss efficient ways to return a vector from a function in C++.

Return by Value

It is a preferred method to return vector from function in C++. It can reduce the execution time and space because in this method, an object is not copied for returning a vector. It only points the pointer to the vector which will be returned.

Code:

Output:

Even elements are: 20 88 44 14

Return by Reference

This method is better for returning large classes and structures. We will not return the reference of the local variable which is declared in the function because that can lead to dangling reference. In this method, we pass the vector by reference(& myFunction) and we will return as reference only.

Code:

Output:

Squares of the elements are: 400 2601 121 7744 1936 5929 196 9801

Conclusion

In this article we discussed two efficient methods to return vector in C++. We can use return by value for smaller classes or structures. Return by reference method is only useful in the case of large structures and classes.
Happy Learning!!

Related Posts

  • C++ random number between 0 and 1
    11 June

    C++ random number between 0 and 1

    Table of ContentsUsing the rand() function to generate random number between 0 and 1 in C++Using the std::uniform_real_distribution() function to generate random number between 0 and 1 in C++ Random numbers are frequently used in programming for test case generation, cryptography, and other related purposes. In this tutorial, we will generate random number between 0 […]

  • String contains C++
    07 June

    Check if string contains substring in C++

    Table of ContentsUsing find() function to check if string contains substring in C++.Using strstr() method to check if string contains substring in C++Using boost library to check if string contains substring in C++Using custom functions to check if string contains substring in C++Conclusion There are different ways to check if string contains substring in C++. […]

  • Convert float to String in C++
    24 May

    Convert float to String in C++

    Learn about how to convert float to String in C++ in different wayds.

  • Convert int to char array in C++
    18 May

    Convert int to Char Array in C++

    Table of ContentsUsing to_string and c_str methods to convert int to Char Array in C++Using stringstream to convert int to Char Array in C++Using to_chars method to convert int to Char Array in C++Using sprintf function to convert int to Char Array in C++Using Boost:: lexical_cast method to convert int to Char Array in C++Conclusion […]

  • Input Validation in C++
    15 May

    Input Validation in C++

    Learn about how to implement input validation in C++.

  • Iterate through map in C++
    11 May

    Iterate through map in C++

    Learn about how to iterate through map in C++ in different ways.

Leave a Reply

Your email address will not be published. Required fields are marked *

Subscribe to our newletter

Get quality tutorials to your inbox. Subscribe now.