![]() |
|
#1
|
|||
|
|||
|
Fill vector (Vec_IO_DP) with data
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>. |
|
#2
|
|||
|
|||
|
Quote:
Quote:
Code:
/* 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 */
.
.
.
Code:
/* 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 */
.
.
.
Code:
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.
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: Code:
Vec_DP vdp(v, 5); // You have to tell it how many values to use from the array So the following would not work: Code:
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.
For example: Code:
//
// 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;
}
Code:
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
Quote:
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: Code:
//
// 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;
}
Code:
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
Last edited by davekw7x; 01-17-2009 at 08:53 AM. |
![]() |
| Thread Tools | |
| Display Modes | |
|
|