Dismiss Notice
Join Physics Forums Today!
The friendliest, high quality science and math community on the planet! Everyone who loves science is here!

Tutorial For Vectors in C++?

  1. Nov 5, 2012 #1
    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.

  2. jcsd
  3. Nov 6, 2012 #2
    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++.
  4. Nov 6, 2012 #3
    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?
  5. Nov 6, 2012 #4


    Staff: Mentor

    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.
  6. Nov 8, 2012 #5
    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.
  7. Nov 9, 2012 #6


    User Avatar
    Science Advisor

    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).
  8. Nov 10, 2012 #7
    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.
  9. Nov 10, 2012 #8


    User Avatar
    Science Advisor

    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.

    Here is the link:

  10. Nov 11, 2012 #9
    Thank you very much Chiro, you seem very knowledgable and I really appreciate your help
  11. Nov 11, 2012 #10
  12. Nov 12, 2012 #11

    D H

    User Avatar
    Staff Emeritus
    Science Advisor

    All of the above overload the numerical operators so you can do vector addition, vector subtraction, and scalar multiplication using good old +, -, and *. For example, x = a*(u+v) sets x to the sum of vectors u and v, scaled by some scalar a. This is very nice, but there is a hidden problem here. The problem is that the overloaded operators in C++ can spell the ruin of an application if done incorrectly. The above are examples of how *not* to do things. You certainly don't want to have to be forced to make your graphics and AI so simplified just so that the game can run on a typical computer that no one will buy your game. The naive implementations of the overloaded operators may force exactly that kind of tradeoff.

    Doing things right so that there isn't a whole lot of allocation, copying, and deallocation going on takes a lot of work. One approach is to use expression templates to implement lazy evaluation. This is how the Eigen package (http://eigen.tuxfamily.org/index.php?title=Main_Page) and Boost uBLAS (http://www.boost.org/doc/libs/release/libs/numeric/ublas/doc/index.htm [Broken]) overcome some basic performance issues with C++ and operator overloading. Sometimes lazy evaluation will get you into trouble, and at least the Eigen package tries to be pretty smart about those issues.
    Last edited by a moderator: May 6, 2017
Know someone interested in this topic? Share this thread via Reddit, Google+, Twitter, or Facebook

Similar Discussions: Tutorial For Vectors in C++?
  1. C# Tutorial (Replies: 13)