Simple STL question
Hi -
I'm trying to store a simple "struct" in an STL set. The goal is to create a list of all the colors in a series of RGB images, and reduce them down to a single 256-entry color palette. The reasons I want to use a "set" are: a) Since the entries are stored in sorted order (here, by RGBA value), lookup is fast and easy b) I don't want any duplicates (if a color appears more than once, I tally the #/occurrences). I'm not proficient with STL. All of the examples I could find deal with scalars (numbers or strings), and not "structs". I got "structs" to work - but it's really ugly and convoluted. QUESTION: Is the way I saved "structs" in my STL "set" correct, or is there a better way? Thanx in advance .. PSM Code:
#include <stdio.h> |
Well, it's clear you normally code in C, not C++ and it would have been nice with some platform neutral code complete with a test program, but why does the set need to store pointers?
|
i don't like the fact you need to create a struct which you may not want, maybe using a std::map with the key as the unsigned long, maps can also do quick look ups. but this may mean storing the 4 bytes of rgb data twice. once as the key and once in the struct.
Just a thought ;( heres another thought whats the value of the left byte of the RGB_TYPE quad = pixel[0]+(pixel[1]<<8)+(pixel[2]<<16); it maybe an idea to push a zero value to the byte to be safe. <edit> or will this be ok hmmmmm. too many things on my mind to think straight ;(</edit> |
Thanx, dmail - I don't like it either, which was the reason for my post. Nevertheless, I absolutely couldn't think of any better/easier way to:
a) Sort my elements into a set (needed a "set" because 1) I needed fast inserts and fast lookups, and 2) because I needed to insure uniqueness) b) Make the elements "structs" (aggreggate data: not scalars, but not full-fledged classes, either). Anyway, thank you for the response. PS: No thanks to you, Hivemind, for your arrogant, condescending attitude. I'd like to think you were just having a bad day ... and that you took it out on me, rather than going home and kicking the dog ;-) |
My new edition of C/C++ User's Journal arrived in the mail today, and there was a note in Andrei Alexandrescu's column that suggests maybe my problem using "structs" with STL "sets" might indeed be a fundamental limitation in STL itself:
Quote:
Quote:
Quote:
Quote:
PS: Sadly, it looks like this month's edition of C/C++ User's Journal (the Feb 2006 edition) is going to be the last one. After nearly 30 years, they're ceasing publication. |
All times are GMT -5. The time now is 04:08 AM. |