By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
455,128 Members | 1,224 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 455,128 IT Pros & Developers. It's quick & easy.

Which method to use

P: n/a
I'm working with data that streams into a function which I'd like to
sort from low to high based on the value of the first record. What
would be the best most efficient way to do this... linked lists, an
array, or..? Here's an exmplae of what I'm looking at:

void MyData::StreamingData()
{
double a = sData->record;
int b = sData->record2
char ss[256];
printf(ss, "%.4lf %d", a, b);
}
96.01 5
96.01 3
97.01 1
98.02 9
97.01 1


etc.

I get a record every few minutes, so I'd like to limit the stored data
to maybe 10 records... so the container would be continually adding and
deleting on the fly. I'd also like to be able to delete records if the
second record returns a 0. What route would be the best for this
scenario?

With much appreciation,
Marcus

Jul 23 '05 #1
Share this Question
Share on Google+
5 Replies


P: n/a
Ok, well it seems perhaps I need to narrow down the direction for
appropriate guidance.

I currenlty have ruled out using arrays for this problem and have
narrowed it down to using either the STL list or STL set. I'm starting
to veer away from the list, because everytime an insertion is made the
list would have to be resorted... which isn't very efficient. While,
with set it is possible to maintain an ordered container without
sorting. However, I'm still unclear on which route to go, as I've never
attempted something like this before.

Anyway, if anyone has any suggestions, or needs me to be more clear on
what I'm trying to accomplish, please let me know.

Best regards,
Marcus

Jul 23 '05 #2

P: n/a
* Marcus:
I'm working with data [pairs] that streams into a function which I'd like to
sort from low to high based on the value of the first [item in each pair]. What
would be the best most efficient way to do this... linked lists, an
array, or..?
Programming efficiency: use a std::map.
Execution efficiency: don't worry about that.
Memory usage efficiency: don't worry about that.

Here's an exmplae of what I'm looking at:

void MyData::StreamingData()
{
double a = sData->record;
int b = sData->record2
char ss[256];
printf(ss, "%.4lf %d", a, b);
}


Note that you're missing a semicolon in there. That means the printf
specification might also be missing a character, or be misspelled. And
in that case you're out in Undefined Behavior territory (not that I say
you are, but printf is dangerous, not recommended for newbies).

Anyway, don't do i/o in member functions, except in classes _dedicated_
to i/o.

--
A: Because it messes up the order in which people normally read text.
Q: Why is it such a bad thing?
A: Top-posting.
Q: What is the most annoying thing on usenet and in e-mail?
Jul 23 '05 #3

P: n/a
On 2005-07-13 12:47:27 -0500, "Marcus" <mc*******@walla.com> said:
I'm working with data that streams into a function which I'd like to
sort from low to high based on the value of the first record. What
would be the best most efficient way to do this... linked lists, an
array, or..? Here's an exmplae of what I'm looking at:

void MyData::StreamingData()
{
double a = sData->record;
int b = sData->record2
char ss[256];
printf(ss, "%.4lf %d", a, b);
}
96.01 5
96.01 3
97.01 1
98.02 9
97.01 1


etc.

I get a record every few minutes, so I'd like to limit the stored data
to maybe 10 records... so the container would be continually adding and
deleting on the fly. I'd also like to be able to delete records if the
second record returns a 0. What route would be the best for this
scenario?


I don't understand. You have a process/function call that produces
data, and you have to write a sorting function on that data based on
your criteria? You say that you will limit data allocation to 10 items,
but so the stream wouldn't be sorted if you just sort them 10 at a time
step.a

For what I understand, if you have a function that uses a tuple, then
you really don't need STL or C++. You have a slow data rate, but you
don't say anything about the amount of data you have actually to sort,
could be terabytes...

With qsort and standard C you can easily do whatever you want without
adding complexity. If your data set is huge, then probably you want an
on disk routine, which is far more complex.

Explain better the problem...

--
Sensei <se******@tin.it>

cd /pub
more beer

Jul 23 '05 #4

P: n/a
Alf, thanks for the std::map, suggestion, I'll be looking in to how to
use that. Yes, it appears I missed a semicolon... I must have missed it
when I was modifying it to something more readable/understandable.

Sensei, yes the function call produces data which needs to be sorted
with a maximum of 10 items in the container at any given time. So when
there are 10 items in the container and the function receives a new
pair, there needs to be an operation that adds and removes a pair in
the container so that the limit is always ten based on the value of the
"a" double variable. For example:

If a is > than any a->item in the container, then the pair is not
added. If a is < than any a->item in the container then the pair is
added in order based on the value of a, unless the value of b is 0 in
which case this would give instruction to remove a pair of matching "a"
value.

The amount of data is trivial, just take a look at the example output
above and imagine there never being more than ten items as above in the
container. So, like 70 bytes for the container... very compact. The
container needs to be small as it will be used in a small GUI where
this data will be viewed in real time.

Hope this was more clear.

Marcus

Jul 23 '05 #5

P: n/a
On 2005-07-15 22:12:34 -0500, "Marcus" <mc*******@walla.com> said:
Sensei, yes the function call produces data which needs to be sorted
with a maximum of 10 items in the container at any given time. So when
there are 10 items in the container and the function receives a new
pair, there needs to be an operation that adds and removes a pair in
the container so that the limit is always ten based on the value of the
"a" double variable. [...]


I see. Well, if size *really* matters, use plain C and make a
do-it-yourself function, which is really really small. If you can
afford std::map and std::string and so on, then use it and don't bother
about C :)

--
Sensei <se******@tin.it>

cd /pub
more beer

Jul 23 '05 #6

This discussion thread is closed

Replies have been disabled for this discussion.