469,599 Members | 2,866 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 469,599 developers. It's quick & easy.

std::sort

say i had
97456 and
23456

is there already a sort function to check which one begins with the smaller
number rearrange it. in this case the bottom number should clearly be
rearranged to the top as it lower in numerical order.
Jul 22 '05 #1
9 12371
JasBascom wrote:
say i had
97456 and
23456

is there already a sort function to check which one begins with the
smaller number rearrange it. in this case the bottom number should
clearly be rearranged to the top as it lower in numerical order.


Yes, it's called std::sort, as you already guessed:

std::vector<int> v;
v.push_back(97456);
v.push_back(23456);
std::sort(v.begin(), v.end())

You needn't specifically use a vector, it works with all containers that
support random access iterators.

--
Unforgiven

Jul 22 '05 #2

"Unforgiven" <ja*******@hotmail.com> wrote in message
news:c1*************@ID-136341.news.uni-berlin.de...
JasBascom wrote:
say i had
97456 and
23456

is there already a sort function to check which one begins with the
smaller number rearrange it. in this case the bottom number should
clearly be rearranged to the top as it lower in numerical order.


Yes, it's called std::sort, as you already guessed:

std::vector<int> v;
v.push_back(97456);
v.push_back(23456);
std::sort(v.begin(), v.end())

You needn't specifically use a vector, it works with all containers that
support random access iterators.


Or ensure they're sorted to begin with using:

std::set<int> s;

s.insert(97456);
s.insert(23456);

int smallest = *s.begin();

Jeff F

Jul 22 '05 #3
sorry to bother you again.
if i had
union Allrecords{
struct crecord Newcrecord;
struct irrecord Newirrecord;
struct drecord Newdrecord;
}
union Allrecords *rec;
the structure element I want to sort now has to be accessed throw the union
say
rec.Newcrecord.customer_code; and
rec.Newirrecord.customer_code and
rec.Newdrecord.customer_code.

this is being read in from a file, so I don't know what i would be pushing back
(push_back)
does it still work if I don't have a value to put in the brackets?
instead of std::vector
could I use std::rec?
Jul 22 '05 #4

"JasBascom" <ja*******@aol.com> wrote in message
news:20***************************@mb-m01.aol.com...
sorry to bother you again.
if i had
union Allrecords{
struct crecord Newcrecord;
struct irrecord Newirrecord;
struct drecord Newdrecord;
}
union Allrecords *rec;
the structure element I want to sort now has to be accessed throw the union say
rec.Newcrecord.customer_code; and
rec.Newirrecord.customer_code and
rec.Newdrecord.customer_code.

this is being read in from a file, so I don't know what i would be pushing back (push_back)
does it still work if I don't have a value to put in the brackets?
instead of std::vector
could I use std::rec?


There's no such thing as std::rec. However you can use an array with
std::sort (rec is a pointer to an array, right?). However you have to write
a comparison functor to tell std::sort what to base the sort on. That's not
too easy if you've never done one before, and I'm guessing here but I expect
the point of the exercise is to write your own sort function?

john
Jul 22 '05 #5

"John Harrison" <jo*************@hotmail.com> wrote in message
news:c1*************@ID-196037.news.uni-berlin.de...

"JasBascom" <ja*******@aol.com> wrote in message
news:20***************************@mb-m01.aol.com...
sorry to bother you again.
if i had
union Allrecords{
struct crecord Newcrecord;
struct irrecord Newirrecord;
struct drecord Newdrecord;
}
union Allrecords *rec;
the structure element I want to sort now has to be accessed throw the

union
say
rec.Newcrecord.customer_code; and
rec.Newirrecord.customer_code and
rec.Newdrecord.customer_code.


Another point which isn't clear from your post, is how you can tell which of
the different records types you have in your array. How can you whether an
Allrecords is a Newcrecord, a Newirecord or a Newdrecord? That is something
you are going to have to work out whatever method of sorting you end up
using.

john
Jul 22 '05 #6
JasBascom wrote:
sorry to bother you again.
if i had
union Allrecords{
struct crecord Newcrecord;
struct irrecord Newirrecord;
struct drecord Newdrecord;
}
union Allrecords *rec;
the structure element I want to sort now has to be accessed throw the
union say
rec.Newcrecord.customer_code; and
rec.Newirrecord.customer_code and
rec.Newdrecord.customer_code.

this is being read in from a file, so I don't know what i would be
pushing back (push_back)
does it still work if I don't have a value to put in the brackets?
instead of std::vector
could I use std::rec?


You can sort any kind of user defined data type, you just need to make sure
you define either an operator<() for that type or pass a BinaryPredicate as
third parameter to std::sort.

So you'd need to create something like this:
bool operator<(const Allrecords &left, const Allrecord &right)
{
/* put any condition you want to sort on here */
return left.Newcrecord.customer_code < right.Newcrecord.customer_code;
}

Then just use a std::vector<Allrecords>.

You can also sort any array, because a pointer behaves like an iterator and
can be used as such. You could sort your rec, if it has num_elts elements,
with std::sort(rec, rec + num_elts);

--
Unforgiven

Jul 22 '05 #7
> However you have to write
a comparison functor to tell std::sort what to base the sort on.


My mistake, Unforgiven is right, ignore the above.

john
Jul 22 '05 #8
thank you unforgiven
is left an object of Allrecords
say: union Allrecords left, right?
I've never come across operators before, what is their purpose in the great
scheme of things?

std::vector<Allrecords> is that to be declared before the operator function?

This is the sort function I have, how can i change it so it works in the manner
you suggested.

the union object rec
union Allrecords *rec
is being sorted from a binary file.
The idea behind this sort is to take two lines from the file and check that
they null terminate '\0' because customer_code is 6 arrays long. Then use
strcmp to compare the two, as Newcrecord.customer_code is declared as type
char, use temp to store it while shuffling the pack around.
As it is it doesn't work.
void sort_function( union Allrecords *rec, ifstream& validdata )
{

union Allrecords *str_ptr1 = rec;
union Allrecords *str_ptr2, tempstr;
for(int i =0; i< loop; i++)
while( strcmp(str_ptr1[i].Newcrecord.customercode, '\0') ||
strcmp(str_ptr1[i].Newdrecord.customercode, '\0') ||
strcmp(str_ptr1[i].Newirrecord.customercode, '\0'))
{
str_ptr2 = str_ptr1 + 1;//set to next element.

for( i=0; i<loop; i++)
while( strcmp(str_ptr2[i].Newcrecord.customercode, '\0') ||
strcmp(str_ptr2[i].Newdrecord.customercode, '\0'))
{
for(int i=0; i<loop; i++)
if( strcmp( str_ptr1[i].Newirrecord.customercode,
str_ptr2[i].Newirrecord.customercode + 1))
{
tempstr = *str_ptr1;
*str_ptr1 = *str_ptr2;
*str_ptr2 = tempstr;

}
*str_ptr1++;//incremented, so that the same code isn't sorted again
}
str_ptr2++;
}

}
Jul 22 '05 #9
JasBascom wrote:
thank you unforgiven
is left an object of Allrecords
Yes, so is right.
say: union Allrecords left, right?
You don't need to repeat the union keyword in C++. That's necessary in C,
but not in C++.
I've never come across operators before, what is their purpose in the
great scheme of things?
You use operators all the time. +, -, <, >, ==, whatever are all operators.
C++ has the ability to create your own operators for your own types. What my
code did, was define a 'smaller than' operator for two objects of type
Allrecords. The code I put in the operator function is probably not what you
would want it to do though. You just need to make sure that the function
returns 'true' when 'left' is smaller than 'right' (ie. 'left' should appear
before 'right' in the sort order), and 'false' when 'left' and 'right' are
equivalent or 'right' is smaller.
std::vector<Allrecords> is that to be declared before the operator
function?
No, std::vector<Allrecords> would be a replacement for Allrecords *rec.
vector is a (safer) alternative to an array, but as I said (and John
Harrison too) it's perfectly possible to sort the existing array using
std::sort.
This is the sort function I have, how can i change it so it works in
the manner you suggested.

the union object rec
union Allrecords *rec
is being sorted from a binary file.
The idea behind this sort is to take two lines from the file and
check that they null terminate '\0' because customer_code is 6 arrays
long. Then use strcmp to compare the two, as Newcrecord.customer_code
is declared as type char, use temp to store it while shuffling the
pack around.
As it is it doesn't work.


It's not exactly clear what you're trying to do. What sort algorithm are you
using? It looks a bit like Bubblesort, but it doesn't seem right. Are you
sure you need three nested loops? In any case, you shouldn't be reusing 'i'
in all three loops, because this way it'll have the value 'loop' when it
exits the innermost loop and therefore exits the outer loops as well. What
is 'loop' exactly? Is it the upperbound on rec (it looks like that anyway)?
What exactly is the definition of customercode? char or char* or char[]?
What is the criterea that defines whether to objects of type Allrecords are
equal, smaller or larger than each other? Could you perhaps give the entire
definition of Allrecords as well, including the structs in it, it would help
a lot.

--
Unforgiven

Jul 22 '05 #10

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

8 posts views Thread by lok | last post: by
6 posts views Thread by alexhong2001 | last post: by
1 post views Thread by Ravi | last post: by
7 posts views Thread by Ireneusz SZCZESNIAK | last post: by
8 posts views Thread by Manfred | last post: by
5 posts views Thread by fade | last post: by
11 posts views Thread by Jeff Schwab | last post: by
reply views Thread by suresh191 | last post: by
4 posts views Thread by guiromero | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.