[C++] How to return and call vectors from functions?

In summary, the get_string_input function will return a vector of strings, while averageFunc will return the average of a vector of floats.
  • #1
Yohanna99
3
0
Hi, beginner coder here. I have a somewhat solid understanding of both vectors and functions, and have used the two of them many times, but I'm have trouble coding functions that have vectors in their parameters and as their return values.

Another thing I'm having trouble with is calling the return value from a function when the parameter is a single vector. The c++ forum explains how to call the return when the parameters are variables and I get that but there's no example with vectors.

Thanks :)
 
Technology news on Phys.org
  • #2
Could you give some specific code that you've tried with exactly what you want it to do? There's really nothing special about a vector object that is returned from a function; the only thing possibly is whether it is a reference to the object, pointer to the object or an object itself.
 
  • #3
This is very rough code just to give you an idea. Ignore things that may make no sense I'll edit it again soon.
Code:
#include <iostream>
#include <string>
#include <vector>

using namespace std;

string get_string_input(vector<string> strings);

float averageFunc(vector<float> floating);int main()
{

	cout << "enter a word. When you're done, enter done." << endl;

	vector<string> strings;

	string input;

	while (input != "done")
	{
		cin >> input;

		if (input == "done")
			break;
		else
			strings.push_back(input);
	
	}	cout << "your sentence is: ";
	for (int i = 0; i < strings.size(); i++) {

	// how to output return val from get_string_input

	

	cout << "enter a num. When you're done, enter 10001." << endl;

	vector<float> floating;

	float num;	while (num != 10001)
	{
		cin >> num;

		if (num == 10001)
			break;
		else
			floating.push_back(num);

	}
	

	// how to output return val from averageFunc	system("pause");
	return 0;
}

/*
The first function will be get_string_inputs, which will return a vector of strings.
This function should continually loop, taking input from cin, and putting that into
the vector that will be returned. It should take one parameter, which is the string
to take in that will denote that input should stop being received. When this input 
is found, the function will return the vector.
*/

string get_string_input(vector<string> strings) {	string s;
	for (int i = 0; i < strings.size(); i++) {

		s = strings[i];
		cout << s << " ";
	}
	

	return s;
}

/*
The second function will be an average function. This function should
take a vector of floats as input, and return a single float value that
is the average of the vector passed in.
*/
float averageFunc(vector<float> floating) {

	float total = 0.0;

	for (int i = 0; i < floating.size(); i++)	{

		total += floating[i];
}
	

	float avrg = total / floating.size(); // average of vector

	return avrg;
}
 
  • #4
Yohanna99 said:
This is very rough code just to give you an idea. Ignore things that may make no sense I'll edit it again soon.

Hi Yohanna99! Welcome to MHB! (Smile)

It appears you're looking for:
Code:
// how to output return val from get_string_input
cout << get_string_input(strings) << endl;
...
// how to output return val from averageFunc
cout << averageFunc(floating) << endl;
Oh, and [m]num[/m] should really be initialized:
Code:
float num = 0;
 

FAQ: [C++] How to return and call vectors from functions?

How do I return a vector from a function in C++?

In order to return a vector from a function in C++, you can use the keyword "vector" followed by the data type of the elements in the vector, and then the function name. Inside the function, you can simply use the "return" keyword followed by the vector you want to return.

Can I modify a vector inside a function and still return it?

Yes, you can modify a vector inside a function and still return it. Vectors are passed by reference in C++, which means that any changes made to the vector inside the function will also affect the original vector outside the function.

How do I call a function that returns a vector in C++?

To call a function that returns a vector in C++, you can simply use the function name followed by parentheses, like you would with any other function. Assign the returned vector to a new vector variable if you want to use it later in your code.

What if I want to pass a vector as a parameter to a function in C++?

If you want to pass a vector as a parameter to a function in C++, you can declare the function parameter as a vector type, followed by the data type of the elements in the vector. Then, you can pass the vector as an argument when calling the function.

Can I use vectors in a recursive function in C++?

Yes, you can use vectors in a recursive function in C++. Vectors are like any other data type and can be used in recursive functions, as long as the function's base case is properly defined and the vector is being passed by reference.

Similar threads

Replies
10
Views
2K
Replies
2
Views
2K
Replies
23
Views
2K
Replies
36
Views
4K
Replies
6
Views
10K
Replies
39
Views
4K
Replies
1
Views
720
Replies
19
Views
2K
Replies
18
Views
1K
Back
Top