C++ Line into Vector
-
29-09-2019 - |
Question
Wow I've been all over the place with questions today and I apologize if they seem to overlap, but for every question comes another question...Since one thing wont work......but I should use something else......etc.
Anyways, I have a text file:
6
3.0 2.5 -1.5 0.0 1.7 4.0
6 10
6 is the Number of "floats" in the second line (the 3.0,2.5 etc...) 3.0,2.5,-1.5 are all a series of floats. 6 and 10 are just 2 integers.
I have a Vector
std::vector<double> numbers;
All i need to do, is get the Second line put into numbers. So right now I have
ifstream myfile (filename.c_str());
I can simply just do a myfile>> to get the first value (6) but how would I go about putting the second line in my vector? Remember I ONLY know how big line 2 is, by reading in the first line (the 6 in this case).
Also the last 2 numbers aren't supposed to be in this vector, but two seperate values. Which i can just do myfile >> a >> b.
Sorry again for sooo many questions. but I've literally been looking everywhere and asking probably the wrong questions.
Solution
something like :
int count, a, b;
double tmp;
std::vector<double> numbers;
ifstream myfile (filename.c_str());
myfile >> count;
for(int i = 0; i < count; ++i) {
myfile >> tmp; numbers.push_back(tmp);
}
myfile >> a >> b;
OTHER TIPS
myfile >> numElements;
numbers.resize(numElements);
for (int i = 0; i < numElements; i++) {
myfile >> numbers[i];
}
myfile >> a >> b;
I'm going to start from the point where you've already read in the line from the file, since it seems like you're OK with that. This is the most basic approach. My code below illustrates this approach using very straightforward code that could be used to understand how this works.
- Get the line you want to parse in to a
string
, this isline
in my code below. - Search the
string
for tokens, where each token is seperated by anything that isn't either a digit, a decimal, or a negative sign - For each token, convert the string to a
double
by usingstringstream
. - Add the converted double to your vector
At the end of the loop, I also dump the vector to the screen for inspection.
#include <string>
#include <vector>
#include <sstream>
#include <iostream>
#include <iterator>
using namespace std;
int main()
{
string line = "3.0 2.5 -1.5 0.0 1.7 4.0";
vector<double> doubles;
string::size_type n_begin = 0;
string::size_type n_end = line.find_first_not_of("-.0123456789", n_begin);
while( n_begin != string::npos )
{
string cur_token = line.substr(n_begin, n_end-n_begin);
stringstream ss;
ss << cur_token;
double cur_val = 0.0;
ss >> cur_val;
doubles.push_back(cur_val);
// set up the next loop
if( n_end == string::npos )
n_begin = n_end = string::npos;
else
{
n_begin = n_end + 1;
n_end = line.find_first_not_of("-.0123456789", n_begin);
}
}
copy(doubles.begin(), doubles.end(), ostream_iterator<double>(cout, "\n"));
}
Do you have copy_n()
in your arsenal?
template<class In, class Size, class Out>
Out copy_n(In first, Size n, Out result)
{
while( n-- ) *result++ = *first++;
return result;
}
Put it somewhere where you can easily #include it in other translation units. It's useful. Use it like so to copy your n
floating-point values:
copy_n(std::istream_iterator<double>(std::cin),
n,
std::back_inserter(v));