tags:

views:

211

answers:

7

Just wondering if C++ has a standard equivalent of the Vector2/3/4 (structures I think?) in C#?

Edit: For clarification a XNA C# Vector2/3/4 "structures" (I'm not entirely sure what they are) basically hold 2, 3, or 4 float values like a struct in C++ defined as:

struct Vector3
{
    float x, y, z;
};

I've been basically using just that code in C++, but I was hoping for a standard alternative and was unable to find one.

A: 

Yes, it is called vector.

1st result for Google: c++ vector.

mcandre
This is completely different than C#'s Vector3. It is more like C#'s `List<T>` class.
Reed Copsey
-1: Totally different. More like a `List<T>` from C#
Callum Rogers
Vector doesn't behave the same way at all, but thanks ^^
Joe.F
@Reed @Callum @Joe: I'll agree it's not the same, but it's not "completely different". Consider the reason `std::vector` has it's name is because it represents a mathematical vector: a tuplet of homogeneous elements. The fact you can change the dimension of the vector at run-time has no implications on it still being analogous to a math vector. That said, we do agree this answer doesn't answer the question, since OP wants an equivalence.
GMan
A: 

A valarray<complex<T>> could be used like a Vector2 in many cases. For Vector3/4 I know no standard equivalent.

Peter G.
Thanks, unfortunately I'm in need of a 3/4 version much more than a 2, but I hadn't previously known about valarray, thanks for the knowledge!
Joe.F
+5  A: 

The Vector3 struct in C# is from XNA, not the base class libraries.

The equivelent in C++ would be to use XMFLOAT3.

Reed Copsey
Oh, hmm, well I only used XNA when I was using C# - my apologies. Thank you though :)
Joe.F
@Joe: No problem. Just letting you know ;) There is no "standard" Vector3, etc. (Though WPF includes Vector3D in C#/.NET: http://msdn.microsoft.com/en-us/library/system.windows.media.media3d.vector3d.aspx)
Reed Copsey
+2  A: 

Nothing standard that I know of, but here's some code to get you started

http://www.flipcode.com/archives/Faster_Vector_Math_Using_Templates.shtml

If you are using C++/CLI and targeting Windows and .NET, you can use Vector2, etc.

Lou Franco
That flipcode link is excellent, thank you very much!
Joe.F
A: 

I don't believe that there are any "canned" objects in C++ which are equivalent. The STL vector is not the same thing. That is essentially a dynamic array for storage of any value types whereas the vector that you're referring to is class with added member functionality, typically seen used in XNA graphic applications (in my experience at least). You are on to something with your recognition of the fact that Vector2/3/4 are data structures. You could very easily store the values of the W, X, Y, Z components in your own structure, but the added member functions provided with the Vector2/3/4 object would need to be added by you.

Rich Hoffman
That's the method I've currently been using, but I was hopeful for a standard alternative. The link provided by Lou Franco looks to be very helpful though!
Joe.F
A: 

I know I'm late to the party, but...

Though there's no equivalent to Vector3 in standard C++, there are several linear algebra libraries that have one. Two I know of are very small and fast:

greyfade
Thanks for the response! Eigen looks very attractive, I'll look into it!
Joe.F
A: 

There are also several C++ 3D graphics libraries similar to XNA, such as Ogre3D, Irrlicht, and others. Might be a bit of an overkill if you only need the Vector3 structure though.

Felix Ungman