mazman2011
01-16-2009, 07:37 AM
Hello,
i'm having a simple question which i couldn't answer with google or other web forums. I'm trying to use C++ with Numerical Recipes (2nd Ed.). I would like to implement the quicksort algorithm (NR::sort) which takes an Vector 'Vec_IO_DP &arr'. The question is:
How do i fill a vector with data and pass it to that function?
Vec_IO_DP arr() creates me an array but how do i fill it with data e.g. {2.3,45.,245.12,1.25,12.0}? I do not really understand this format.
Another problem i'm having is that nrutil_nr.h won't compile properly as i get an "expected ')' before '=' token" error in many lines of template <class T>.
davekw7x
01-16-2009, 09:38 AM
...e.g. {2.3,45.,245.12,1.25,12.0}? I do not really understand this format. I suggest that you get a book or find an on-line C or C++ tutorial that describes arrays and their initialization. For example: Tutorial on Arrays (http://www.cplusplus.com/doc/tutorial/arrays.html)
...How do i fill a vector with data and pass it to that function?
In C and C++ you can initialize an array by giving a comma-separated list of constants enclosed by braces. For example to declare an array of five doubles and to specify initial values:
/* Any C or C++ program */
int main()
{
double v[5] = {3.2, 8.2, 5.5, 2.1, 3.0};/* Creates and initialized an array of five doubles */
.
.
.
You can even let the compiler count the number of values in your initializer list. The following has the exact same effect:
/* Any C or C++ program */V
int main()
{
double v[] = {3.2, 8.2, 5.5, 2.1, 3.0};/* Creates and initialized an array of five doubles */
.
.
.
Note that this notation is only permissible when declaring the array. In C and C++ you can never (that's never) copy all of the elements of an entire array with an assignment statement. So the following would not work.
double v[] = {3.2, 8.2, 5.5, 2.1, 3.0};
double v2[5];
v2 = v; // Won't work. Never has worked. Never will work. Period.
The Numerical Recipes Vec_DP class is somewhat similar to the C++ std::vector class. (It's kind of like a vector of doubles with some helpful new stuff thrown in and with some other stuff thrown out.)
It is not possible to create a constructor for an NR::Vec_DP object (or for a std::vector) that uses the {} array initialization syntax. (That's not possible. Really. The language simply doesn't provide for such things.)
However, there is a constructor that allows a Vec_DP to be initialized by copying the contents of an array.
For example, if you want to declare a Vec_DP that has size equal to five and give it the values from the array, then you can do something like:
Vec_DP vdp(v, 5); // You have to tell it how many values to use from the array
You can not copy an array into a Vec_DP (or any other C++ vector object) with an assignment statement. (I am trying to make a point here: In General, in C and C++, you can not copy the entire contents of an array to or from anything with an assignment statement that just uses the name of the array.)
So the following would not work:
double v[] = {3.2, 8.2, 5.5, 2.1, 3.0};
Vec_DP vdp2;
vdp2 = v; // This will not work. Try it if you want to, but it won't work. Period.
You can, however copy one Numerical Recipes Vec_DP to another by a simple assignment statement. The size of the target is automatically adjusted to be the size of the source, and the elements are copied.
For example:
//
// Demonstration of initialization and copying of
// Numerical Vec_DP objects
//
// davekw7x
//
#include "nrtypes.h"
void printVecDP(Vec_I_DP & v);
int main()
{
DP v[] = {3.2, 8.2, 5.5, 2.1, 3.0};
Vec_DP vdp(v, 5); // You have to tell it how to use from the array
Vec_DP vdp2; // No initializer, so it is "empty"
cout << "Initially:" << endl;
cout << " vdp.size() = " << vdp.size() << endl;
cout << " vdp2.size() = " << vdp2.size() << endl;
cout << " Here's vdp: " << endl;
printVecDP(vdp);
cout << " Here's vdp2:" << endl;
printVecDP(vdp2);
vdp2 = vdp;
cout << "After assignment:" << endl;
cout << " vdp.size() = " << vdp.size() << endl;
cout << " vdp2.size() = " << vdp2.size() << endl;
cout << " Here's vdp: " << endl;
printVecDP(vdp);
cout << " Here's vdp2:" << endl;
printVecDP(vdp2);
return 0;
}
void printVecDP (Vec_I_DP & v)
{
if (v.size() == 0) {
cout << " In printVecDP: The vector is empty." << endl;
}
else {
for (int i = 0; i < v.size(); i++) {
cout << " v[" << i << "] = " << v[i] << endl;
}
}
cout << endl;
}
Output:
Initially:
vdp.size() = 5
vdp2.size() = 0
Here's vdp:
v[0] = 3.2
v[1] = 8.2
v[2] = 5.5
v[3] = 2.1
v[4] = 3
Here's vdp2:
In printVecDP: The vector is empty.
After assignment:
vdp.size() = 5
vdp2.size() = 5
Here's vdp:
v[0] = 3.2
v[1] = 8.2
v[2] = 5.5
v[3] = 2.1
v[4] = 3
Here's vdp2:
v[0] = 3.2
v[1] = 8.2
v[2] = 5.5
v[3] = 2.1
v[4] = 3
Another problem i'm having is that nrutil_nr.h won't compile ...
Tell us what compiler you are using and what version.
Tell us how you are compiling. (Command line? Integrated Development Environment? What?)
Regards,
Dave
Footnote: If you want to learn how to implement quicksort, you can write it yourself. If you just want to use quicksort, on Vec_DP objects, then it's already supplied:
//
// Demo of Vec_DP initialization and use of nr::sort
//
// davekw7x
#include "nr.h"
void printVecDP(Vec_I_DP & v);
int main()
{
DP v[] = {3.2, 8.2, 5.5, 2.1, 3.0};
Vec_DP vdp(v, 5);
cout << "Initially:" << endl;
cout << " vdp: " << endl;
printVecDP(vdp);
NR::sort(vdp);
cout << "After NR::sort:" << endl;
cout << " vdp: " << endl;
printVecDP(vdp);
return 0;
}
void printVecDP (Vec_I_DP & v)
{
if (v.size() == 0) {
cout << " In printVecDP: The vector is empty." << endl;
}
else {
for (int i = 0; i < v.size(); i++) {
cout << " v[" << i << "] = " << v[i] << endl;
}
}
cout << endl;
}
Output
Initially:
vdp:
v[0] = 3.2
v[1] = 8.2
v[2] = 5.5
v[3] = 2.1
v[4] = 3
After NR::sort:
vdp:
v[0] = 2.1
v[1] = 3
v[2] = 3.2
v[3] = 5.5
v[4] = 8.2