CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 11 of 11
  1. #1
    Join Date
    Nov 2006
    Posts
    5

    Exclamation writing vectors to a file in c++

    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?

  2. #2
    Join Date
    Mar 2002
    Location
    California
    Posts
    1,582

    Re: writing vectors to a file in c++

    A vector is 1 dimensional and only has one "row".

    Jeff

  3. #3
    Join Date
    Jan 2006
    Posts
    344

    Re: writing vectors to a file in c++

    Unless it is a vector of vectors, victor

  4. #4
    Join Date
    Apr 2002
    Location
    PA, USA
    Posts
    1,658

    Re: writing vectors to a file in c++

    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.
    =--=--=--=--=--=--=--=--=--=--=--=--=--=
    Please rate this post to show your appreciation for those that helped you.

    Before You Post A Question, Please Read This: How & When To Ask Your Question
    =--=--=--=--=--=--=--=--=--=--=--=--=--=

    -eli
    http://www.toad-software.com
    http://www.dailymission.com - Do It Daily

  5. #5
    Join Date
    Nov 2006
    Posts
    5

    Re: writing vectors to a file in c++

    yes, i have a vector of vectors....

  6. #6
    Join Date
    Feb 2005
    Location
    "The Capital"
    Posts
    5,306

  7. #7
    Join Date
    Nov 2006
    Posts
    5

    Re: writing vectors to a file in c++

    its a vector of vector of doubles......

  8. #8
    Join Date
    Feb 2005
    Location
    "The Capital"
    Posts
    5,306

    Re: writing vectors to a file in c++

    Very simply put, is there a problem with this:
    Code:
    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

  9. #9
    Join Date
    Oct 2000
    Location
    London, England
    Posts
    4,773

    Re: writing vectors to a file in c++

    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:

    Code:
    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
    Code:
    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.

  10. #10
    Join Date
    Nov 2006
    Posts
    5

    Re: writing vectors to a file in c++

    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";

    }

    }

  11. #11
    Join Date
    Apr 1999
    Posts
    27,449

    Re: writing vectors to a file in c++

    Quote Originally Posted by akashprasad
    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

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured