std::sort

J

JasBascom

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.
 
U

Unforgiven

JasBascom said:
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.
 
J

Jeff Flinn

Unforgiven said:
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
 
J

JasBascom

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?
 
J

John Harrison

JasBascom said:
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
 
U

Unforgiven

JasBascom said:
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);
 
J

John Harrison

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
 
J

JasBascom

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.Newcrecord.customercode, '\0') ||
strcmp(str_ptr1.Newdrecord.customercode, '\0') ||
strcmp(str_ptr1.Newirrecord.customercode, '\0'))
{
str_ptr2 = str_ptr1 + 1;//set to next element.

for( i=0; i<loop; i++)
while( strcmp(str_ptr2.Newcrecord.customercode, '\0') ||
strcmp(str_ptr2.Newdrecord.customercode, '\0'))
{
for(int i=0; i<loop; i++)
if( strcmp( str_ptr1.Newirrecord.customercode,
str_ptr2.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++;
}

}
 
U

Unforgiven

JasBascom said:
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.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
474,162
Messages
2,570,896
Members
47,434
Latest member
TobiasLoan

Latest Threads

Top