Where Can I Find a Good Tutorial for Vectors in C++?

In summary: 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
  • #1
grantwilliams
67
0
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
 
Technology news on Phys.org
  • #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++.
 
  • #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?
 
  • #4
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.
 
  • #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.
 
  • #6
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).
 
  • #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.
 
  • #8
grantwilliams said:
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.

Here is the link:

http://www.ogre3d.org/
 
  • #9
Thank you very much Chiro, you seem very knowledgeable and I really appreciate your help
 
  • #11
chiro said:
Edgardo said:
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 ) 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:

1. What are vectors in C++?

Vectors in C++ are one-dimensional arrays that can store a dynamic number of elements. They are similar to traditional arrays, but with added functionalities such as the ability to resize and insert elements at any position.

2. How do you declare a vector in C++?

To declare a vector in C++, you need to include the <vector> header file and use the std::vector template class. For example, std::vector<int> myVector; declares a vector of integers named myVector.

3. What are the main operations that can be performed on vectors in C++?

The main operations that can be performed on vectors in C++ include adding elements with push_back(), inserting elements at a specific position with insert(), accessing elements with at() or [] operator, removing elements with pop_back() or erase(), and resizing the vector with resize().

4. How do you iterate through a vector in C++?

You can use a for loop or a range-based for loop to iterate through a vector in C++. Alternatively, you can also use iterators, which are objects that point to elements in the vector and can be used to traverse it.

5. Can vectors in C++ store different data types?

Yes, vectors in C++ can store different data types. This is because the std::vector template class is type-agnostic, meaning it can hold any data type that is specified when declaring the vector. However, all elements in a specific vector must be of the same data type.

Similar threads

  • Programming and Computer Science
Replies
8
Views
1K
  • Programming and Computer Science
2
Replies
69
Views
4K
  • Programming and Computer Science
Replies
2
Views
3K
Replies
13
Views
2K
  • Programming and Computer Science
Replies
8
Views
6K
  • Programming and Computer Science
Replies
3
Views
1K
  • Programming and Computer Science
Replies
5
Views
598
  • Programming and Computer Science
Replies
8
Views
863
  • MATLAB, Maple, Mathematica, LaTeX
Replies
11
Views
1K
  • Introductory Physics Homework Help
Replies
17
Views
857
Back
Top