Probably the memory you save will be less noticeable than the time you spend implementing and debugging your 3-byte int implementation. (The First Thing To Optimize Is Developer Time) Furthermore, these ints will presumably not be as tightly optimised by the compiler, so it will probably not be time efficient.
If you are interested in maintaining a sorted array of ints in the range 0 to 1,000,000 then I would think the best data structure may be an array of 1,000,001 ints representing how many times each number appears. On 5 Mar 2012, at 23:14, Βαγγέλης Μαμαλάκης<[email protected]> wrote: > Well I a writing a program for a competition and I am initializing some > arrays whose values will be less than 1million so I am trying to save memory. > I am also curious. > Would it be possible to give me some example code or psedocode. > Thanks for your interest. > > On Mar 6, 2012 12:44 AM, "Damian Walczak" <[email protected]> wrote: > Hi > > On Mon, Mar 5, 2012 at 8:27 PM, Sidhartha Mani <[email protected]> wrote: > You could > use 3 char vars to hold your values, but i believe, the size of char > differs on different architectures. > > AFAIR size of char is always 1 byte in C/C++ and it doesn't matter what > architecture you are using. > > the implementation of this though. I think std::sort() only works and > is meant for abstract data structures like vectors and such, not on > elementary ones like int. > > You can use std::sort for sorting arrays, example is even on wikipedia [1] > If you write your own class, it's enough to write proper comparison function > and use second version of std::sort [2] > > > If I may ask - why do you need 3-byte 'int' ? > > Best, > D. > > [1] http://en.wikipedia.org/wiki/Sort_(C%2B%2B) > [2] http://www.cplusplus.com/reference/algorithm/sort/ > > > > Sidhartha > > > > On 06-Mar-2012, at 12:42 AM, bugos <[email protected]> wrote: > > > Hello, > > is there a way to define my own data type, which will be idetical to > > an int, but just 3 bytes size? i must be able to use it as an int and > > even use std::sort() with it. > > > > -- > > You received this message because you are subscribed to the Google Groups > > "Google Code Jam" group. > > To post to this group, send email to [email protected]. > > To unsubscribe from this group, send email to > > [email protected]. > > For more options, visit this group at > > http://groups.google.com/group/google-code?hl=en. > > > > -- > You received this message because you are subscribed to the Google Groups > "Google Code Jam" group. > To post to this group, send email to [email protected]. > To unsubscribe from this group, send email to > [email protected]. > For more options, visit this group at > http://groups.google.com/group/google-code?hl=en. > > > -- > You received this message because you are subscribed to the Google Groups > "Google Code Jam" group. > To post to this group, send email to [email protected]. > To unsubscribe from this group, send email to > [email protected]. > For more options, visit this group at > http://groups.google.com/group/google-code?hl=en. > -- > You received this message because you are subscribed to the Google Groups > "Google Code Jam" group. > To post to this group, send email to [email protected]. > To unsubscribe from this group, send email to > [email protected]. > For more options, visit this group at > http://groups.google.com/group/google-code?hl=en. -- You received this message because you are subscribed to the Google Groups "Google Code Jam" group. To post to this group, send email to [email protected]. To unsubscribe from this group, send email to [email protected]. For more options, visit this group at http://groups.google.com/group/google-code?hl=en.
