vector

how would i return an vector of variables from this statement

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
void YourFireType()
{
Monster YourFireType;
YourFireType.SetType("fire");
YourFireType.SetAttack(8);
YourFireType.SetDefense(1);
YourFireType.SetHealth(40);

Type = YourFireType.GetType();//string
Attack = YourFireType.GetAttack();//int
Defense = YourFireType.GetDefense();//int
Health = YourFireType.GetHealth();//int
return ???
}
Last edited on
Make the return type of the function a vector of the type you want(if not sure, you can template the function), and return it.
Also, make sure the vector is not a local one.

Thanks,
Aceix.
Were you wanting to stick Type, Attack, Defence, and Health all in the same vector, and return that?

If so, why not return the Monster itself?

The problem is that a vector typically contains only one type. As Type is a string, you have a problem. Unless you adjust your code to use an integer ID to identify type as opposed to a string.

If you make Type an int, then you can uses vector<int> as the return type.

Andy

PS There are ways to solve different types in a vector, but if you can avoid it, life will be simpler and happier!
Last edited on
i dont know how to use a vector and i tried but couldnt figure it out, i need to return an ARRAY of variables OR VECTOR of variables, which i dont know how to do, i believe arrays have to be the same type, but vector can be different (something like this...
 
return vector (string Type, int Attack, int Defense, int Health)

i'm trying to figure how to do something like this, because i have to do it in more than just this example (all i know is pair<?><?>,return make_pair which only returns 2 variables of any type)
Skeleton code:

1
2
3
4
5
6
7
vector<int> vec1;

vector& AddVector(int iNum=0)
{
     vec1.push_back(iNum);
     return vec1;
}
@Aecix

Why not?

1
2
3
4
5
6
7
vector<int> AddVector(int iNum=0)
{
     vector<int> vec1;
     vec1.push_back(iNum);

     return vec1;
}


I have to admit, I don't get why you say
Also, make sure the vector is not a local one.

I see no point in returning a global. It's already accessible, to why go tot he trouble?

Andy

PS I assumed the return of vector& should have been vector<int>&
Last edited on
Here is how I would do it, in your situation:

1
2
3
4
5
6
static vector<int> default_vi   //default vector of type int

void addvect(int n = 0, vector<int>& numbers = default_vi)
{
    numbers.push_back(n);
}


This way, you can put the vector in the calling function. Each time you call this function, it uses the address of the vector instead of making a new one. This way, you can keep adding values to 1 vector.

also, you can create a function that takes integer, and returns its corresponing 'type' as a string.

Also, if you want to return multiple types, you can just create a vector for each type and a function that takes each one as an argument and uses their addresses:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
static vector<int> default_vi
static vector<string> default_vs

void dosomthing(vector<string>& strings = default_vs, vector<int>& integers = default_vi)
{
    if(strings.size() > 0)
    {
         //make sure there's somthing in strings before we do somthing to it...  In this if we put our code for strings
    }
    if(integers.size() > 0)
    {
        //same process as with strings
    }
    //add to them, resize them, w.e.....
}


This is (in my opinion) the biggest difference between vectors and arrays: A lot less hassle. Arrays have to have every block of memory allocated before you can pass them from one function to another, whereas vectors can be simply passed. Optionally, you can take their address so that the function will directly modify that vector. This allows you to 'return' multiple values.
Last edited on
Why do you want to return a vector?
Each of those variables is already in your monster class, so simply allocate an instance of Monster and return it or even return Monster by value if you don't want to use new to allocate an instance. No need for a vector.
I gave you an example of this in your other thread.

@Andy,

vec1 in your code is a local one. And it is "invalid" or popped from the stack or it goes out of scope when the function returns. So if you return a pointer to it, that would be a futile pointer.

Aceix.
Topic archived. No new replies allowed.