Hvordan laver man Vector Arrays i C++?
Og hvordan generelt laver man Arrays i C++?
Og hvilke forskellige array typer findes der?
[Redigeret d. 17/01-04 21:42:43 af Mikkel Jans]
Jeg går ud fra at du mener matematiske vektorer.
Brug stl's vector i stedet for C arrays:
#include <vector>
#include <iostream>
using namespace std;
class Vector
{
friend ostream & operator<<(ostream &, Vector &);
private:
float m_x, m_y, m_z;
public:
Vector(float x = 0.0, float y = 0.0, float z = 0.0) : m_x(x),m_y(y),m_z(z){}
//Andre metoder her...
};
ostream & operator<<(ostream & out, Vector & v)
{
out << "X: " << v.m_x << " Y: " << v.m_y << " Z: " << v.m_z;
return out;
}
typedef vector<Vector> VectorArray;
int main(int argc, char ** argv)
{
VectorArray vectors;
vectors.push_back(Vector(1.0,2.0,3.0));
vectors.push_back(Vector(4.0,5.0,6.0));
vectors.push_back(Vector(-7.5,-3.3));
for(VectorArray::iterator ite = vectors.begin(); ite != vectors.end(); ite++)
{
cout << (*ite) << endl;
}
return 0;
}