Vec4 as a vector of four floats?

Hi friends,

Define a type Vec4 as a vector of four floats.saying it now says the following.


class Vec4{
vector<float> f1;
vector<float> f2;
vector<float> f2;
vector<float> f3;
};

I understand that if I do wrong.
Last edited on
Not sure I understand what you're saying.

Here you've defined a class with four vectors of floats as member vairables. I think if you need it to be four floats, then an array is better for you than a vector? Vectors are meant to be dynamically sized.
something like this?

class Vec4{
vector<float> f(4);
public:
Vec4()
{
}

};

Last edited on
or something like that?

typedef float float4[4];

class Vec4{
vector<float4> fval;
public:
Vec4()
{
}

};
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
#include <iostream>
#include <vector>
using namespace std;

struct FourFloats
{
    float data[4];

    FourFloats(float a, float b, float c, float d)
    {
        data[0]=a;
        data[1]=b;
        data[2]=c;
        data[3]=d;
    }
};

int main()
{
    vector<FourFloats> my_ff_vec;

    my_ff_vec.push_back(FourFloats(1.0f, 2.0f, 3.0f, 4.0f));
    my_ff_vec.push_back(FourFloats(1.1f, 2.2f, 3.3f, 4.4f));

    cout << my_ff_vec[0].data[1] << endl;
    cout << my_ff_vec[1].data[2] << endl;

    return 0;
}

Or...

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
#include <iostream>
#include <vector>
using namespace std;

template <unsigned int N, class T>
struct NTs
{
    T data[N];
};

int main()
{
    vector<NTs<4,float> > my_ff_vec;

    NTs<4,float> temp;
    int i;

    for (i=0; i<4; i++) temp.data[i]=i+1;
    my_ff_vec.push_back(temp);

    for (i=0; i<4; i++) temp.data[i]=i+1+0.1f*(i+1);
    my_ff_vec.push_back(temp);

    cout << my_ff_vec[0].data[1] << endl;
    cout << my_ff_vec[1].data[2] << endl;

    return 0;
}
Last edited on
I think yes, like this one,
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
class floatt{
	float f[4];
public:
	floatt(float f1, float f2, float f3, float f4)
	{
		f[0] = f1;
		f[1] = f2;
		f[2] = f3;
		f[3] = f4;
	}
};


class Vec4{
	vector<floatt> fval;
public:
	Vec4()
	{
		fval.push_back(floatt(rand() % 2 + 2.f, rand() % 3 + 1.f,rand() % 5 + 2.f, rand() % 3 + 4.f));
	}

};


int main()
{
	Vec4  f;

	return 0;

}

Topic archived. No new replies allowed.