C++ template function returning "vector" ?
-
I am trying to get comfortable using "vector". I have a function "returning" hardware parameters I need to process further "upstream" - including "main". I can pass a vector by reference or have been told to define "local vector" and "return" if from the function. I am not sure how "return vector" really works and have not tried it yet. The function returning "vector " syntax is scary... Could I return pointer to locally defined vector ? ( Or is is even feasible ?) Is there a specific resource I could read ? Something similar to this example , but with "return * vector - string "?
// C++ program to demonstrate how vectors
// can be passed by reference.
#include
using namespace std;// The vect is passed by reference and changes
// made here reflect in main()
void func(vector &vect)
{
vect.push_back(30);
}int main()
{
vector vect;
vect.push_back(10);
vect.push_back(20);func(vect); for (int i=0; i
-
I am trying to get comfortable using "vector". I have a function "returning" hardware parameters I need to process further "upstream" - including "main". I can pass a vector by reference or have been told to define "local vector" and "return" if from the function. I am not sure how "return vector" really works and have not tried it yet. The function returning "vector " syntax is scary... Could I return pointer to locally defined vector ? ( Or is is even feasible ?) Is there a specific resource I could read ? Something similar to this example , but with "return * vector - string "?
// C++ program to demonstrate how vectors
// can be passed by reference.
#include
using namespace std;// The vect is passed by reference and changes
// made here reflect in main()
void func(vector &vect)
{
vect.push_back(30);
}int main()
{
vector vect;
vect.push_back(10);
vect.push_back(20);func(vect); for (int i=0; i
Your code looks good to me. If you want
func
to return a vector, it should allocate it on the heap, and returning it in aunique_ptr
would be better than just returning a raw pointer. But it looks like you want to update a vector that might already have entries, so passing it by reference makes sense. EDIT:func
isn't a function template; it simply takes a vector argument. The term function template refers to a function defined bytemplate func
...Robust Services Core | Software Techniques for Lemmings | Articles
The fox knows many things, but the hedgehog knows one big thing. -
I am trying to get comfortable using "vector". I have a function "returning" hardware parameters I need to process further "upstream" - including "main". I can pass a vector by reference or have been told to define "local vector" and "return" if from the function. I am not sure how "return vector" really works and have not tried it yet. The function returning "vector " syntax is scary... Could I return pointer to locally defined vector ? ( Or is is even feasible ?) Is there a specific resource I could read ? Something similar to this example , but with "return * vector - string "?
// C++ program to demonstrate how vectors
// can be passed by reference.
#include
using namespace std;// The vect is passed by reference and changes
// made here reflect in main()
void func(vector &vect)
{
vect.push_back(30);
}int main()
{
vector vect;
vect.push_back(10);
vect.push_back(20);func(vect); for (int i=0; i
You function is OK as it stands.
Quote:
can pass a vector by reference or have been told to define "local vector" and "return" if from the function.
That is fine whenever you have code similar to
vector get_rand_vect(size_t size)
{
vector v;
while (size--)
{
v.push_back(rand());
}
return v; // this is optimized by the compiler
}"In testa che avete, Signor di Ceprano?" -- Rigoletto
-
You function is OK as it stands.
Quote:
can pass a vector by reference or have been told to define "local vector" and "return" if from the function.
That is fine whenever you have code similar to
vector get_rand_vect(size_t size)
{
vector v;
while (size--)
{
v.push_back(rand());
}
return v; // this is optimized by the compiler
}"In testa che avete, Signor di Ceprano?" -- Rigoletto
That would not work in my function which retrieves unknown "size" of (string) data. As I said, I want to learn about vector , but more I look into it the more I like "old fashioned way " to pass a pointer to the function and let the calling code process the pointer afterwards. Seems much simpler.
-
That would not work in my function which retrieves unknown "size" of (string) data. As I said, I want to learn about vector , but more I look into it the more I like "old fashioned way " to pass a pointer to the function and let the calling code process the pointer afterwards. Seems much simpler.
-
That would not work in my function which retrieves unknown "size" of (string) data. As I said, I want to learn about vector , but more I look into it the more I like "old fashioned way " to pass a pointer to the function and let the calling code process the pointer afterwards. Seems much simpler.
Quote:
That would not work in my function which retrieves unknown "size" of (string) data.
That's not a problem. It would work as well. Try, for instance
#include #include using namespace std;
vector get_a_fresh_vector_with_unknown_size()
{
vector v;
int N = rand() % 128;for (int n = 0; n < N; ++n)
v.push_back( rand() );return v;
}int main()
{
auto v = get_a_fresh_vector_with_unknown_size();
for (auto x : v)
cout << x << " ";
cout << endl;
}"In testa che avete, Signor di Ceprano?" -- Rigoletto