|
-
December 10th, 2009, 06:22 PM
#1
best way to deal with big array or vector?
Hi all,
I am trying c++ with some maths now, and I started with simple 2D vector (or array, or matrix, what ever you call it). What I want to do is: in the file below
Code:
a1 12 17
a1 20 25
a1 21 26
a1 33 38
a1 35 40
a1 36 41
a1 51 56
a1 65 70
...
each line is one "line", or "vector", with the second and third columns are x coordinates. The program read line by line, reads those two x-coordinates (say x1 and x2), and gives y at any point between those two xs additional one unit (so y(20) = y(21) = ... = y(25) = 1 for the first line). y of all points start at zero. You can see that y(21) = 2 since 21 appears in two intervals (two first lines), y(37) = 3 since 37 appears in three lines.
After reading the file, it should write to a new text file with the non-zero points only, point by line, like below:
Code:
12 1
13 1
14 1
15 1
16 1
17 1
20 1
21 2
22 2
23 2
24 2
25 2
26 1
33 1
34 1
35 2
36 3
37 3
38 3
39 2
40 2
41 1
51 1
52 1
...
Quite simple if the input file is small. The algorithm I came up with is:
- define array y from 0 to max of x, initial values are all zero.
- read the file, if x is from x1 to x2, then y(x)++;
- after done, write to file: from 0 -> x max, if y(x) is not zero, then write it down.
Problem is if x max is big, then the defined array would be big, and that is a waste of memory as well since a lot of y array is zero. Also, if x max is very big (millions to billions) then the algorithm would not work.
Anybody please help!
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|