vector in header file

Mar 20, 2012 at 12:49pm
I want to create a funtion that adds two vectors in my header file.

the vectors look like this:
1
2
vector<int>vector1[3];
vector<int>vector2[3];


How should I initate them in the function without mention anything about their size?
Mar 20, 2012 at 1:00pm
That appears to be two arrays, one containing 3 vector<int> objects and another also containing 3 vector<int> objects, so six vectors overall.

Is that what you mean, or do you just want two vector<int> objects?
Mar 20, 2012 at 1:04pm
closed account (zb0S216C)
Those statements create 6 std::vectors that hold ints. Was that intentional?

en liten kille wrote:
How should I initate them in the function without mention anything about their size?

If what you wrote was in fact intentional, then you can obtain the length of one of those std::vectors by invoking std::vector::size( ), like so: vector1[0].size( ), or vector1.size( ).

Wazzak
Last edited on Mar 20, 2012 at 1:04pm
Mar 20, 2012 at 1:05pm
Its not so hard once you got the basics of vector. But it appears to me as if you do not have a good knowledge of them. Do you realise that you've created two array of 3 vectors? If you don't then I suggest having a look at
www.cplusplus.com/reference/stl/vector/
1
2
3
vector<int> vector1;
vector<int> vector2;
//Now add them. 
Last edited on Mar 20, 2012 at 1:07pm
Mar 20, 2012 at 1:15pm
vector 1 and 2 shall contain one 3d vector.
If
1
2
3
vector1[0][x]=5;
vector1[1][x]=10;
vector1[2][x]=7;

and
1
2
3
vector2[0][x]=1;
vector2[1][x]=2;
vector2[2][x]=3;


I want to add them vector1 + vector2.
I wrote this but it didn't work.
1
2
3
int add_vectors(*vector<int>vector1, *vector<int>vector2){
    return({vector1[0] + vector2[0], vector1[1] + vector2[1], vector1[2] + vector2[2];})
}

Mar 20, 2012 at 5:25pm
you do not need array of vector . .
1
2
3
4
5
6
7
8
9
vector<int> vector1 ; 
vector<int< vector2; 
vector1.pushback(5);
vector1.pushback(10);
vector1.pushback(7);

vector2.pushback(1);
vector2.pushback(2);
vector2.pushback(3);



then pass it to the function and add like vector1[0] + vector2[0] ;
Mar 20, 2012 at 11:29pm
What about making a simple class, or a simple typedef?
1
2
3
typedef int[3] int_vector;
typedef float[3] float_vector;
typedef double[3] double_vector;

You will be able to declare a vector like this then:
1
2
3
4
int_vector Vector;
Vector[0] = ...
Vector[1] = ...
Vector[2] = ...
Topic archived. No new replies allowed.