Vector Problems


Luminara19131
07-25-2006, 08:27 AM
Hello all!

I recently learned both c++ and numerical recipes (about two weeks agao:-/) and I am having problems with outputing vectors. I tried using cout but that does not work. I'm using the vectors that are in numerical recipes and any help would be greatly appreciated.

Thanx!
Luminara19131

fintler
07-25-2006, 11:54 AM
Hey luminara, funny seeing you here ;*

Anyway, to print out a vector derived from type NRVec, I'm pretty sure you would do something similar to the following:

This is your variable declaration, you can replace NRVec with the specific kind of vector you're using and myVec with the variable name. Also, make sure you have an instance of the class in current scope.
NRVec myVec;

Here's the for() loop that actually prints out all the data in the vector class.
for( int i = 0; i < myVec.size(); i++ ) {
cout << "Element at position " << i;
cout << "= " << myVec[i] << endl;
}

You may have to modify the loop slightly to account for the starting index, if that's the case, you can use something like the following instead:
for( int i = 0; i <= myVec.size(); i++ ) {
cout << "Element at position " << i;
cout << "= " << myVec[i] << endl;
}

I hope that helps :)

Luminara19131
07-25-2006, 01:07 PM
u rule! it works great!:-):) :D