akashprasad
November 15th, 2006, 10:07 AM
how do i write an N by N vector to a file name specified by the user in c++? each line should display i row of the vector....can anyone please help?
|
Click to See Complete Forum and Search --> : writing vectors to a file in c++ akashprasad November 15th, 2006, 10:07 AM how do i write an N by N vector to a file name specified by the user in c++? each line should display i row of the vector....can anyone please help? jfaust November 15th, 2006, 10:10 AM A vector is 1 dimensional and only has one "row". Jeff grahamr (work) November 15th, 2006, 10:11 AM Unless it is a vector of vectors, victor:) Eli Gassert November 15th, 2006, 10:13 AM You've got clearance, clarance! You'll have to do the writing on your own. There is no automagical way of taking care of this. You can use things like serialization (see boost.org) but you sound like you have a specific format you want the file in, so looks like you need to suck it up and write it out to a file yourself. akashprasad November 15th, 2006, 11:21 AM yes, i have a vector of vectors.... exterminator November 15th, 2006, 12:05 PM What do you have in the vector? akashprasad November 15th, 2006, 07:21 PM its a vector of vector of doubles...... exterminator November 15th, 2006, 09:57 PM Very simply put, is there a problem with this:for(size_t i=0;i<vectorOfvector.size();++i) { for(size_t j=0;j<vectorOfvector[i].size(); ++j) { ostream << vectorOfvector[i][j] << " "; } ostream << "\n"; }take a look at this as well - http://www.codeguru.com/forum/showthread.php?t=269648 NMTop40 November 16th, 2006, 10:47 AM vector< vector< T > > is not the best way to implement a matrix. vector< vector< T > > should normally be used when the rows are not guaranteed to all be of the same length. Anyway, use a wrapper, like my cvec_wrapper: template < typename T > struct CVecWrapper // C prefix for Const not for Class! { const std::vector< T > & theVec; std::string theDelim; CVecWrapper( const std::vector< T > & vec, const std::string & delim ) : theVec( vec ), theDelim( delim ) { } }; template < typename T > std::ostream & operator << ( std::ostream & os, CVecWrapper< T > vecw { std::copy( vecw.theVec.begin(), vecw.theVec.end(), std::ostream_iterator< T >( os, vecw.theDelim.c_str() ); } template < typename T > CVecWrapper< T > cvec_wrapper( const std::vector & vec, const std::string & delim ) { return CVecWrapper< T >( vec, delim ); } Now for your vector of vectors you can use the above twice. But you need a transformer. bind2nd might work, so possibly std::transform ( mat.begin(), mat.end(), std::ostream_iterator< CVecWrapper< double > >( os, "\n" ), std::bind2nd( cvec_wrapper< double >, " " ) ); For some reason I can never get bind2nd to work though. It never quite does what I want it to do, and I end up having to write my own functor instead. akashprasad November 16th, 2006, 02:24 PM ok guys, this is what i have basically written (with help from this forum).i need to print the elements of phinew to a file specified by the user(variable fileName) instead of the screen. can u please suggest the changes i need to make to this code? sorry for the trouble. #include<iostream> #include<vector> #include<fstream> #include<string> using namespace std; double max(vector<vector<double> >phi); void printNewPhi(vector<vector<double> > & phi); int main() { cout<<"Enter the edge length"<<endl; double L; cin>>L; cout<<"Enter the number of points on the grid"<<endl; int N; cin>>N; cout<<"Enter the radius of the source in the center"<<endl; double R; cin>>R; cout<<"Enter the tolerance"<<endl; double tolerance; cin>>tolerance; cout<<"Enter the name of the file"<<endl; string fileName; vector<vector<double> > phi(N,N); double rsq=R*R; double xc=L/2.0; double yc=L/2.0; double h=L/(N-1); double hsq=h*h; vector<vector<double> > S(N,N); for (int i=0;i<N;i++) { for (int j=0;j<N;j++) { double x=(i-1)*h; double y=(j-1)*h; double newvar=(x-xc)*(x-xc)+(y-yc)*(y-yc); if (newvar<=rsq) { S[i][j]=hsq; } } } vector<vector<double> > phinew(N,N); vector<vector<double> > diff(N,N); double d=tolerance+1; while (d>tolerance) { for (int i=1;i<N-1;i++) { for (int j=1;j<N-1;j++) { phinew[i][j]=(phi[i+1][j]+phi[i-1][j]+phi[i][j+1]+phi[i][j-1]+S[i][j])/(4+hsq); diff[i][j]=phinew[i][j]-phi[i][j]; d=max(diff)/max(phinew); phi[i][j]=phinew[i][j]; } } } printNewPhi(phinew); return 0; } double max(vector<vector<double> > phi) { double maximum=0; for (int i=0;i<phi.size();i++) { for (int j=0;j<phi[i].size();j++) { if (phi[i][j]<0) { phi[i][j]=-1*phi[i][j]; } if (phi[i][j]>maximum) { maximum=phi[i][j]; } } } return maximum; } void printNewPhi(vector<vector<double> > & phi) { int N; for (int i=0;i<N;i++) { for (int j=0;j<N;j++) { cout<<phi[i][j]<<" "; } cout << "\n"; } } Paul McKenzie November 16th, 2006, 02:53 PM ok guys, this is what i have basically written (with help from this forum).i need to print the elements of phinew to a file specified by the user(variable fileName) instead of the screen. can u please suggest the changes i need to make to this code? sorry for the trouble. Please take a look at your post. Do you see what's wrong with it? Yes, it isn't formatted and is practically unreadable. Please use code tags when posting code. Regards, Paul McKenzie codeguru.com
Copyright Internet.com Inc., All Rights Reserved. |