POV-Ray : Newsgroups : povray.programming : Vectors in C++ : Re: Vectors in C++ Server Time
6 Oct 2024 14:04:01 EDT (-0400)
  Re: Vectors in C++  
From: Warp
Date: 16 Dec 2002 06:17:15
Message: <3dfdb63b@news.povray.org>
Reusser <reu### [at] chorusnet> wrote:
>    I know this will probably sound like a dumb question to all of the
> programmers in here, but I'm completely new to C++ and I'm pretty
> confused.  I found a vector class online but I don't know quite how to
> use it.  Every time I try, I get more errors than there are lines of
> text.  How would I define a simple 3d vector like I would in POV so I
> can do basic operations on it?  Thanks in advance.

  There are more efficient way of using 3-component vectors in C++ than
using the standard vector class, but since you are a complete beginner,
it might be easier for you to use it for now.

  If you want to make a vector containing three doubles, you simply do this:

std::vector<double> myVector(3);

  The three doubles are automatically initialized to 0 (the reason behind
this is very complicated for a newbie so I will simply skip explaining why).
  Now you can access the three components with the [] operator. The index
values you can use are 0, 1 and 2. Like this:

myVector[0] = 5.2; // sets the first component to 5.2
myVector[2] = myVector[0]; // sets the third component the same as the first

  (Don't use index values greater than 2 or else your program will misbehave
(if you are lucky you will only get a segmentation fault error).)

  Note that if you give such vector as a parameter to a function, the function
should take it as a const reference for efficiency (in which case a single
pointer is copied instead of the whole instance plus the allocated memory).
For example:

void myFunction(const std::vector<double>& parameter)
{
  ...
}

  You can then call this function simply by giving your vector as parameter:

myFunction(myVector);

-- 
#macro M(A,N,D,L)plane{-z,-9pigment{mandel L*9translate N color_map{[0rgb x]
[1rgb 9]}scale<D,D*3D>*1e3}rotate y*A*8}#end M(-3<1.206434.28623>70,7)M(
-1<.7438.1795>1,20)M(1<.77595.13699>30,20)M(3<.75923.07145>80,99)// - Warp -


Post a reply to this message

Copyright 2003-2023 Persistence of Vision Raytracer Pty. Ltd.