|
Prev: Need help in polymorphism exercise
Next: I am learning recursion, but I couldnt find my wrong,can u help me?
From: Daniel on 21 Nov 2007 09:45 I tried to write a parser that reads a file of comma seperated numbers, but the code was buggy messy and a mix of C and C++ (using stof and c_str functions.) It is not known in advance how many numbers there are in each line, but this is needed to set the size of the vector. Does anyone have a nice block of code to do this in C++ ? Thanks.
From: Chris ( Val ) on 21 Nov 2007 10:41 On Nov 22, 1:45 am, Daniel <danwgr...(a)gmail.com> wrote: > I tried to write a parser that reads a file of comma seperated > numbers, but the code was buggy messy and a mix of C and C++ (using > stof and c_str functions.) Do you have an example of what you wrote? > It is not known in advance how many numbers there are in each line, > but this is needed to set the size of the vector. No, it's not. That's the reason we would use a vector over a plane jane array. > Does anyone have a nice block of code to do this in C++ ? You haven't explained how you want to store the numbers in a std::vector. Do you want to store the numbers from each line in the file in a single dimensioned vector? or do you want the vector to act as a jagged array, storing each line separately? Either way is quite easy, it just determines whether you'll need to use an additional loop and a std::istringstream object with a "std::vectoe<std::vector<int> >" for example. -- Chris
From: Chris ( Val ) on 21 Nov 2007 10:43 On Nov 22, 2:41 am, "Chris ( Val )" <chris...(a)gmail.com> wrote: > On Nov 22, 1:45 am, Daniel <danwgr...(a)gmail.com> wrote: > > > I tried to write a parser that reads a file of comma seperated > > numbers, but the code was buggy messy and a mix of C and C++ (using > > stof and c_str functions.) > > Do you have an example of what you wrote? > > > It is not known in advance how many numbers there are in each line, > > but this is needed to set the size of the vector. > > No, it's not. > > That's the reason we would use a vector over a > plane jane array. Of course, that should have been "plain" :-) -- Chris
From: Antoon on 21 Nov 2007 10:52 "Daniel" <danwgrace(a)gmail.com> schreef in bericht news:50a4be23-afd9-423f-8475-283e47d73c42(a)y43g2000hsy.googlegroups.com... > I tried to write a parser that reads a file of comma seperated > numbers, but the code was buggy messy and a mix of C and C++ (using > stof and c_str functions.) It is not known in advance how many > numbers there are in each line, but this is needed to set the size of > the vector. Does anyone have a nice block of code to do this in C++ ? > > Thanks. Not realy code but: - use std::getline to extract std::strings from your file - create an std::istringstream from the string - use the extraction operator to extract all integers from the stream - use std::vector<int>::push_back to insert each integer in your vector Antoon
From: Jim Langston on 21 Nov 2007 21:32 "Daniel" <danwgrace(a)gmail.com> wrote in message news:50a4be23-afd9-423f-8475-283e47d73c42(a)y43g2000hsy.googlegroups.com... >I tried to write a parser that reads a file of comma seperated > numbers, but the code was buggy messy and a mix of C and C++ (using > stof and c_str functions.) It is not known in advance how many > numbers there are in each line, but this is needed to set the size of > the vector. Does anyone have a nice block of code to do this in C++ ? It's easier to just google for "CSV Parse C++" and download a class.
|
Next
|
Last
Pages: 1 2 3 Prev: Need help in polymorphism exercise Next: I am learning recursion, but I couldnt find my wrong,can u help me? |