## Tutorial For Vectors in C++?

Hi, I'm programming the orbit of planets using a verlet algorithm, and while i feel comfortable i could implement the program, i would like to get some more practice with vectors so I can really learn how they work in C++. If anyone knows of a good tutorial for vectors (applied to physics would ideal, but not at all necessary) I would be very grateful.

Thanks,
Grant
 PhysOrg.com science news on PhysOrg.com >> Leading 3-D printer firms to merge in $403M deal (Update)>> LA to give every student an iPad;$30M order>> CIA faulted for choosing Amazon over IBM on cloud contract
 Do you mean "arrays"? Or a class that models physical vectors in 3D space? If you mean the physical vectors, to my knowledge they're not a part of C++ (but possibly of some library you have access to) so you won't find a tutorial for them as a part of C++.
 After doing some research C++'s vectors appear to be a kind of array? I guess I can just write my own methods for 3D space vectors?

Mentor

## Tutorial For Vectors in C++?

It's probably more accurate to refer to them as a container than an array, since an array implies a block of contiguous memory addresses. The vector template class is now a part of C++ - see http://en.cppreference.com/w/cpp/container/vector.

If you do a web search for "cpp vector" you'll probably find some tutorial material.
 It sounds like you're talking about the physics/engineering concept of vectors (a direction and a magnitude) rather than the C++ concept of vectors (a special type of container that's like an array with a dynamic size). In that case, the standard C++ libraries don't include vector support. You could either write your own class, or use a library like Boost. The Boost library is very widely used for software development, and the Boost.Math library contains a library called uBLAS which is used for linear algebra operations and which has full support for vector and matrix math.
 Hey grantwilliams. If you are doing 3D simulation stuff, I'd recommend doing a search for open source implementations and going through them unless you want to write them yourself. In fact if you are trying to render something and you don't have code to do so (and haven't learned what it takes to get something up and running) then i strongly suggest you look at current implementations. There are libraries that allow you to call a few functions to create windows, draw models and that kind of thing, but depending on what you want to do and what data you need access to, you will need to think about what functionality you need in what is out there (and thankfully there is a lot out there to pick from). If you want to get a simple object spinning on a screen then you could get the OpenGL docs and SDK, SDL, and a few decent open source classes for matrices and vectors and put the whole thing together after a bit of pain. However the minute you start thinking "I want it do this", "Now what about this" and so on, then you will need to sit down and think for a minute about how complex your needs are and more importantly (especially if they are complex) if someone has taken steps towards meeting them (partially). This is why people use things like 3DSMAX, MATLAB (or Maple), or AutoCAD, or Oracle to do stuff: it's because they have managed to take an extremely complex problem and actually figure out how to do it well enough so that not only can people end up getting things done, but also so that it can be extended when people come along with their endless wish lists for plugins and features. IMO having dealt with OpenGL and DirectX, OpenGL is a lot easier to deal with and I recommend it over DirectX even though I understand and appreciate the use for interface pointers and redirected structures through the whole COM approach of design (which I think is really good by the way).
 Yes I was referring to 2D/3D Math/Physics/Engineering vectors. Is there a book I could read about how vectors can be implemented in C++? I am starting research and using computation to solve problems and I sort of understand how I could implement vectors in C++, but I would love to be able to really learn them and establish a nice foundation for the computation I will be doing later.

 Quote by grantwilliams Yes I was referring to 2D/3D Math/Physics/Engineering vectors. Is there a book I could read about how vectors can be implemented in C++? I am starting research and using computation to solve problems and I sort of understand how I could implement vectors in C++, but I would love to be able to really learn them and establish a nice foundation for the computation I will be doing later.
There are tonnes of books talking about implementing vector libraries including a lot of game design books, computer graphics books, and computational science/engineering books.

As I said before though, a lot of free implementations can be found online already.

All the open-source game engines, visualization and rendering platforms, computational science and engineering packages, and others have classes for vectors and matrices with a wide range of features and optimizations relevant to that particular platform.

Game engines and high performance platforms will have for example, code with assembly routines for matrix multiplication because games have to do tonnes of these operations every frame so they need to have optimized code.

The OGRE library is a large open source game engine that will have these as well Irrlicht which is another open source game engine.

The Intel website has PDF's for optimized matrix multiplication in SSE and SSE2 for various common matrix sizes (3x3 up to 6x6).

I would personally figure out some of your requirements first with regards to features and performance characteristics and then search for a few implementations: I have given two suggestions and the code in OGRE should be very well developed for those libraries.

There are tonnes of open source repositories with matrix and vector routines but what I recommend is that you get one from a very large complex project and the reason is that they will have included the features in their classes that are required to do the breadth of things that the platform needs to do, and game engines need to do a lot of stuff.

If someone writes a stand-alone piece of code simply as an exercise and is used only in isolation, then it won't have this.

It's not to say that all individual submissions are in isolation and are purely theoretical exercises: a lot of submissions are just a few files from a massive project but if you have the entire project, then you can at least see how a particular class supports the rest of the platform.