Passing and returning a vector

I'm trying to figure out how to pass vectors to be able to modify them in a subfunction. This is my code:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
#include <iostream>
#include <vector>
#include <string>

struct person {
    char name;
    bool status;
};
using namespace std;

vector<person> add();
vector<person> output(vector<person>);

int main(){
	vector<person> friends = add();
	cout << "In main: " << friends[2].name << endl;
	friends = output(friends);
	cout << "In main 2: " << friends[2].name << endl;
    return 0;
}

vector<person> add(){
    vector<person> friends;
    friends.push_back(person());
	friends[1].name = 'J';
	friends[1].status = true;
	friends[2].name = 'T';
	friends[2].status = true;
	return friends;
}

vector<person> output(vector<person> friends){
	cout << "in output: " << friends[2].name << endl;
	friends[2].name = 'X';
	cout << "in output 2: " << friends[2].name << endl;
	return friends;
}


It compiles without errors and runs fine, but I don't get the results I was hoping for.

I expected output to be:
In main: T
in output: T
in output 2: X
In main 2: X


But instead it was:
In main: T
in output: 
in output 2: X
In main 2: T


Thanks in advance.
pass them by reference, and dont return them from functions.

http://www.learncpp.com/cpp-tutorial/73-passing-arguments-by-reference/

1
2
3
4
5
void output(vector<person>& friends){
	cout << "in output: " << friends[2].name << endl;
	friends[2].name = 'X';
	cout << "in output 2: " << friends[2].name << endl;
}


edit: I am very surprised this runs...
You can't do this:
 
friends[1].name = 'J';

You are trying to assign values to variables that do not exist. i.e. you only have one person in your vector so why are you trying to access the 1st and the 2nd element? (you only have a 0th).
you also need to pass your friends vector by reference into your add function too.
Last edited on
Thanks, that did it! I didn't understand the difference between passing as reference and pass as values, but now it's clear.

Regarding your edit: would the proper way to do it have been to use push_back for each element?
There are a few ways to do it, but you should only be creating ONE friends vector.

e.g. create friends in main. the pass into your add(). Your add() should instantiate a person, then add this to your friends vector.
To be honest i'd change your struct to a class, and have your constructor take a name and a status so you could do something like this:

 
friends.push_back(person('J', true));


but then that begs the question why have a method with just one line in it? It could be as easily be done in main.

1
2
3
4
5
vector<person> friends;
friends.push_back(person('J', true));
friends.push_back(person('L', false));
friends.push_back(person('P', true));
// etc.. 


if you didn't want to change it to a class you need to find out where in the vector you've added the new person and assign to this element. e.g. :

1
2
3
4
5
6
7
8
9
void add(vector<person>& friends)
{
	friends.push_back(person());

	int size = friends.size();

	friends[size-1].name = 'J'; 
	friends[size-1].status = true;


but you'll need to pass a char and a bool into this method as well, unless you want all people to be 'J' and true :)
Last edited on
Thanks once again. Regarding the single line method; this code is very simplified from what I'm working with. I just included the pieces that seemed relevant to my current issue and rewrote pieces of it to illustrate my issue with passing the vector.
cool :)
Topic archived. No new replies allowed.