473,327 Members | 2,007 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,327 software developers and data experts.

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 13053
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

8
by: lok | last post by:
i have a class: template <class T1, class T2> class CPairMapping { public: typedef std::pair<T1, T2> ValuePair_t; typedef std::vector<ValuePair_t> ValueList_t; typedef std::binary_function<...
6
by: alexhong2001 | last post by:
Does "std::sort" work only with sequence containers, not associative containers at all? Among sequential containers, can it be used with "list", "queue" and other sequence containers besides...
1
by: Ravi | last post by:
I came across code which contains: std::sort<unsigned char*>((newAnswer.begin()+1),newAnswer.end()); with newAnswer defined as std::vector<unsigned char>& newAnswer However upon...
4
by: hall | last post by:
I accidently overloaded a static member function that I use as predicate in the std::sort() for a vector and ended up with a compiler error. Is this kind of overload not allowed for predicates and...
7
by: Ireneusz SZCZESNIAK | last post by:
I want to sort a vector with the std::sort function. There are two functions: one with two arguments, the other with three arguments. I am using the one with three arguments. I noticed that...
8
by: Manfred | last post by:
Hello I am new to template programming, so i tried the 'example' from http://www.sgi.com/tech/stl/functors.html. I can compile the code but when i want to run the program I get a segmentation...
15
by: Peter Olcott | last post by:
Does anyone know how to do this?
5
by: fade | last post by:
Good afternoon, I need some advice on the following: I've got a class that has a member std::vector<CStringm_vFileName and a member CString m_path; The vector contains a bunch of filenames with...
11
by: Jeff Schwab | last post by:
Would std::sort ever compare an object with itself? I'm not talking about two distinct, equal-valued objects, but rather this == &that. The container being sorted is a std::vector. I've never...
1
by: Markus Dehmann | last post by:
In the following code example, I define several Comparator classes which contain different compare functions to use with std::sort. I have a Sorter class that gets passed a Comparator and is...
0
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
0
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
1
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: Vimpel783 | last post by:
Hello! Guys, I found this code on the Internet, but I need to modify it a little. It works well, the problem is this: Data is sent from only one cell, in this case B5, but it is necessary that data...
0
by: jfyes | last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...
1
by: PapaRatzi | last post by:
Hello, I am teaching myself MS Access forms design and Visual Basic. I've created a table to capture a list of Top 30 singles and forms to capture new entries. The final step is a form (unbound)...
0
by: af34tf | last post by:
Hi Guys, I have a domain whose name is BytesLimited.com, and I want to sell it. Does anyone know about platforms that allow me to list my domain in auction for free. Thank you
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.