reading integer value

Rkbs,5453; Wdqiz,1245; Rwds,3890; Ujma,5589; Tbzmo,1303;
Vgdfz,70; Mgknxpi,3958; Nsptghk,2626; Wuzp,2559; Jcdwi,3761;
Yvnzjwk,5363; Pkabj,5999; Xznvb,3584; Jfksvx,1240; Inwm,5720;
Ramytdb,2683; Voclqmb,5236;

if this were the input how would i read the int values ignoring the alphabets
Read each line from the file into a string.
Then parse out the parts of the line.
The easiest way to do this is with stringstream.

1
2
3
4
5
6
7
8
9
10
11
12
  string line;
  while (getline(file,line))
  {  stringstream ss (line);
      string junk;
      int val; 
      while (getline (ss, junk, ','))
      {  //  Got a label 
          ss >> val;   // read in the int
          //  Do something with the value
          getline (ss, junk, ';');  // flush the trailing ; 
       }
  }



Something like this takes in a lot of memory. I am trying to read from a file and the size is quite big. so line could contain 100 inputs. Is there a way in which it searches in the input and just stores the numbers.
Something like this takes in a lot of memory

Huh? The code I posted above allocates one stringstream object and two string objects. That's not very much memory. The memory (except line) is going to be released at the bottom of the loop. line will be only as big as the longest line (a few hundred bytes).

I am trying to read from a file and the size is quite big

The code I posted takes a small amount of memory, irrespective of the size of the file. What you do with each value read, now that is a different matter.

so line could contain 100 inputs.

So?

Is there a way in which it searches in the input and just stores the numbers.

There are other ways to parse the lines. None as simple as what I posted.



for example you could use adapt the lexer from bajarne stroustrops (infamous) calculator for this to find all of the numbers
I am not familiar with lexer. Is there like a code snippet that you could post?
http://www.stroustrup.com/Programming/Solutions/Ch7/e7-1.cpp

You really want to implement all that, verses the 12 lines of code I provided? You're not going to convince me it takes any less memory.
@abstractionAnon val == 0 does not get the right number. Did you try and run it?
AbstractionAnon wrote:
There are other ways to parse the lines. None as simple as what I posted.
I wrote:
for example you could use adapt the lexer from bajarne stroustrops (infamous) calculator for this to find all of the numbers
https://www.codeeval.com/open_challenges/124/
this is what i am trying to solve.
Here is my code, it works but memory usage is high. So what can i do?

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
#include<iostream>
#include<fstream>
#include<sstream>
#include<vector>
#include<algorithm>

using namespace std;

int main(int argc, char * argv[])
{
    ifstream infile(argv[1]);

    string line;

    while(getline(infile, line))
    {
        stringstream ss(line);
        vector<int>v;

        int val;
        while(getline(ss, line, ';'))
        {
            stringstream ss1(line);

            getline(ss1, line,',');

            getline(ss1, line);
            val = stoi(line);
            v.push_back(val);
        }

        sort(v.begin(), v.end());

        cout << v[0] << ",";
        for(unsigned int i = 1; i < v.size(); ++i)
        {
            int num = v[i] - v[i - 1];
            if(i == v.size() - 1)
                cout << num;
            else
                cout << num << ",";
        }
        cout << endl;
    }
}
getline (ss, junk, ';'); // flush the trailing ; this isn't needed. If you think about it the read till comma will work fine.

Rkbs,5453; Wdqiz,1245
What makes you think that program takes an inordinate amount of memory?

The only variable amount of memory is your vector at line 18.
How many numbers are you reading total?
there is not a definite amount. So what do i use instead of a vector?
vector should be perfectly fine. How big is the file?
if you read the question it says
The route length is an integer in range [10000, 30000]
The number of cities is in range [500, 600]
So you have a maximum of 600 integers in your vector. No big deal.

That's only 2400 bytes of memory (600x4) on a 32 bit platform ignoring the overhead of the vector object itself, which is minimal.
cool thanks.
Topic archived. No new replies allowed.