Why is it OK to return a 'vector' from a function?
Can we guarantee it will not die?
As long there is no reference returned, it's perfectly fine to do so. words
will be moved to the variable receiving the result.
The local variable will go out of scope. after it was moved (or copied).
Efficient way to return a std::vector in c++
In C++11, this is the preferred way:
std::vector<X> f();
That is, return by value.
With C++11, std::vector
has move-semantics, which means the local vector declared in your function will be moved on return and in some cases even the move can be elided by the compiler.
In C++, is it still bad practice to return a vector from a function?
Dave Abrahams has a pretty comprehensive analysis of the speed of passing/returning values.
Short answer, if you need to return a value then return a value. Don't use output references because the compiler does it anyway. Of course there are caveats, so you should read that article.
Return vector from function without it being destroyed
You can create anything on heap with new
. You shouldn't give out from the function the references to your stack objects, as they will be destroyed as soon as the function finishes.
If you prefer your function to return the vector by value, be sure that the objects inside the vector implement copy constructor (and perhaps assignment operator, too, not sure about that). Having that, please do not forget about the Rule of Three.
returning vector of arrays from function in c++
Try with Standard Library arrays:
#include <iostream>
#include <vector>
#include <array>
using namespace std;
void process(vector<array<int, 3>> &vec)
{
//vector<unsigned long long int*> vec;
array<int, 3> a{1, 2, 3};
array<int, 3> b{4, 5, 6};
vec.push_back(a);
vec.push_back(b);
for (int i = 0; i < vec.size(); i++)
{
for (int j = 0; j < 3; j++)
{
cout << vec[i][j] << " ";
}
cout << "\n";
}
cout << "\n";
//return vec;
}
int main()
{
// your code goes here
vector<array<int, 3>> vec;
array<int, 3> c{1, 7, 8};
vec.push_back(c);
process(vec);
for (int i = 0; i < vec.size(); i++)
{
for (int j = 0; j < 3; j++)
{
cout << vec[i][j] << " ";
}
cout << "\n";
}
cout << "\n";
return 0;
}
C++ edit function, return vector
The way your methods are written, you cannot chain addContact()
with another addContact()
(or any other CEFaceMask
method).
Either change the way you are calling addContact()
the second time:
test.addContact ( CContact ( CTimeStamp ( 2021, 1, 12, 12, 40, 10 ), 123456789, 111222333 ) );
test.addContact ( CContact ( CTimeStamp ( 2021, 2, 5, 15, 30, 28 ), 999888777, 555000222 ) );
or change the signature of addContact()
so that it can chain calls:
CEFaceMask& CEFaceMask::addContact(const CContact &c)
{
m_Db.push_back(c);
return *this;
}
return a vector vs use a parameter for the vector to return it
In theory, yes it's copied. In reality, no, most modern compilers take advantage of return value optimization.
So you can write code that acts semantically correct. If you want a function that modifies or inspects a value, you take it in by reference. Your code does not do that, it creates a new value not dependent upon anything else, so return by value.
Related Topics
C++: Argument Passing "Passed by Reference"
Are Memory Leaks "Undefined Behavior" Class Problem in C++
Boost C++ Serialization Overhead
Difference Between C++03 Throw() Specifier C++11 Noexcept
Performance of Qsort VS Std::Sort
Why/When Is _Declspec( Dllimport ) Not Needed
Convert Std::Bind to Function Pointer
Stdafx + Header File - Order of Inclusion in Mfc Application
Error Enabling Openmp - "Ld: Library Not Found for -Lgomp" and Clang Errors
What's This C++ Syntax That Puts a Brace-Surrounded Block Where an Expression Is Expected
How to Check If Enum Value Is Valid
Linker Error When Calling a C Function from C++ Code in Different VS2010 Project
Why Are Memcpy() and Memmove() Faster Than Pointer Increments
Is Using Double Faster Than Float
Difference in Performance Between Msvc and Gcc for Highly Optimized Matrix Multplication Code