passing vector to function

Dec 19, 2011 at 2:40pm
Hello,
im tring to write a function which get 1D or 2D vector and assigen values for it from file. this is waht i wrote:

void readFromFile(float* dst,const char* fileName,int row, int col){
float value;
int index;
ifstream myFile;
myFile.open(fileName);
while(!myFile.eof()) {
myFile >> value;
dst[index++]=value;
}
myFile.close();
}

void main()
{
int n=3;//number of points
vector<float> input(n);
vector<vector<float> > Lap ( n, vector<float>(n));
readFromFile(input,"t1.txt",1,3);
readFromFile(&Laplacian,"test.txt",3,3);
}

Can you please help me find what i did wrong?
Thanks!
Last edited on Dec 19, 2011 at 2:41pm
Dec 19, 2011 at 4:34pm
what is this line doing .
vector<vector<float> > Lap ( n, vector<float>(n));
Dec 19, 2011 at 5:00pm
- In your function "void readFromFile(...)" the integer index and the float value are not initialized, this is a bad habit that you should work on. Also the "void main(...)" issue...

- You seem to be confused as to how vectors are initialized, either that or like bluecoder I'm also having trouble reading your code. Bad spacing and a lack of formatting tags are contributing to this.

- In your "void main()" function I don't see where Laplacian is declared but you are trying to pass it to "readFromFile(...)". Is this a global variable or something? It helps us more if we can see all of your code.

- You are definiatly going out of bounds when you are entering data into your vector in "readFromFile(...)". This is not how you enter data into a vector.
Dec 19, 2011 at 5:36pm
Also, it's generally not a good idea to pass an entire vector to a function because the process can be performance heavy. Instead, I'd recommend passing two iterators too the function.
Dec 20, 2011 at 1:25am
You can pass by pointer or reference of the entire vector into a function and it should do fine on the performance department. The same way you pass C++ objects by pointer of reference into a function.
Dec 20, 2011 at 1:34am
Why would you pass a pointer to a vector to a function, instead of just passing the iterators in the first place? As far as passing by reference, yes that works as well, but chances are you'll end up making the iterators anyways, so there's no real advantage that way either. I feel like you're just playing devils advocate :p
Dec 20, 2011 at 1:46am
You can pass iterators in also. This shows once again C++ flexibility. And it is this flexibility that can cause confusion among beginning programmers. Java does not suffer as it is more rigid so beginning programmers once learn of a way can use it all the way. In C++, due to the various ways possible, they have to expose themselves in order to do maintenance later in their programming career :)

Pass by value,pointers,reference. Pass in vector, iterators... and many more.

PS Still nothing will compare to Perl flexibility but that will be another topic for another day :P
Dec 20, 2011 at 1:56am
Yes, I'd almost argue Perl has too much flexibility.
Dec 20, 2011 at 2:04am
Hey don't let those Perl monks in another forum read this post! They are hardcore Perl'ers and will defend Perl design to their death. They like the flexibility so much that some use that as a factor to distinguish themselves from other programming languages developers!

I guess another hardcore Python'ers would disagree with them though :P

PS I am not starting yet another programming language wars in this forum!
Dec 20, 2011 at 3:18am
vector<vector<float> > Lap ( n, vector<float>(n)); It creates an nxn 'zero' matrix.

@OP: [code] "Please use code tags" [/code]
Your code doesn't compile, it could be nice if you post the compile errors.
Your function ask for a float* but you are passing a vector<float>

About the iterators. There is a huge advantage if you templatize them, that way your function can work for others containers.
However, they know nothing about the container so you can't use the methods of the container. By instance, I doubt that std::find is optimized to work with set::iterator
Its syntax can be a little annoying too, ¿did you tried the set_{difference,intersection,union} functions with sets ?
Last edited on Dec 20, 2011 at 3:18am
Topic archived. No new replies allowed.