C++Talk.NET Forum Index C++Talk.NET
C++ language newsgroups
 
Archives   FAQFAQ   SearchSearch   MemberlistMemberlist   UsergroupsUsergroups   RegisterRegister 
 ProfileProfile   Log in to check your private messagesLog in to check your private messages   Log inLog in 

Built-in sorting algorithms?

 
Post new topic   Reply to topic    C++Talk.NET Forum Index -> C++ language (comp.lang.c++)
View previous topic :: View next topic  
Author Message
York
Guest





PostPosted: Sat Oct 25, 2003 5:34 am    Post subject: Built-in sorting algorithms? Reply with quote



Lets say I have the following structure

struct test_struct
{
int some_number;
char first_name[10]
}

test_struct s_table[100]

/*
In short a table within a table with a number, I would like to know if there
are any standard sorting algorithms implemented in c/c++ to sort s_table in
an alphabetical order based on first_name

I could code myself a small function using stricmp or strcasecmp but I
suspect a built-in algo would be
a lot more efficient and a few lines of code would be saved.

I do know about sort() function but since the structure contains more then
one element I somehow doubt it could be implemented?
Any tips would be appreciated

*/


Back to top
Gianni Mariani
Guest





PostPosted: Sat Oct 25, 2003 6:38 am    Post subject: Re: Built-in sorting algorithms? Reply with quote



York wrote:
Quote:
Lets say I have the following structure

struct test_struct
{
int some_number;
char first_name[10]
}

test_struct s_table[100]

/*
In short a table within a table with a number, I would like to know if there
are any standard sorting algorithms implemented in c/c++ to sort s_table in
an alphabetical order based on first_name

I could code myself a small function using stricmp or strcasecmp but I
suspect a built-in algo would be
a lot more efficient and a few lines of code would be saved.

I do know about sort() function but since the structure contains more then
one element I somehow doubt it could be implemented?
Any tips would be appreciated

*/

all you need to provide is a comparison function and you can use std::sort.

struct test_comparator
{
bool operator()( const test_struct & a, const test_struct & b)
{
return std::strcoll( a.first_name, b.first_name ) < 0;
}
};

then sorting is like

std::sort( s_table, s_table+100, test_comparator() )

I didn't try this code so you'll need to read between the typos.




Back to top
Jerry Coffin
Guest





PostPosted: Sat Oct 25, 2003 6:44 am    Post subject: Re: Built-in sorting algorithms? Reply with quote



In article <3f9a0b9c$1_2 (AT) aeinews (DOT) >, [email]sdfs (AT) dfssdfs (DOT) net[/email] says...
Quote:
Lets say I have the following structure

struct test_struct
{
int some_number;
char first_name[10]
}

test_struct s_table[100]

To sort this, you need to supply a comparison function. In C++, you
could use code like this:

struct test_struct {
int number;
std::string first_name;

bool operator<(test_struct const &other) const {
return first_name < other.first_name;
}
};

For the moment, I've changed first_name to std::string instead of an
array of char -- if you really need to use the latter, your comparison
routine can use strcmp.

The basic idea is pretty similar in C, but it's generally more work, and
when you're done it'll normally run quite a bit slower, so IMO, it's not
worth a lot more discussion unless you have no choice in the matter.

--
Later,
Jerry.

The universe is a figment of its own imagination.

Back to top
Russell Hanneken
Guest





PostPosted: Sat Oct 25, 2003 7:02 am    Post subject: Re: Built-in sorting algorithms? Reply with quote

York wrote:
Quote:
Lets say I have the following structure

struct test_struct
{
int some_number;
char first_name[10]
}

test_struct s_table[100]

In short a table within a table with a number, I would like to know if
there are any standard sorting algorithms implemented in c/c++ to sort
s_table in an alphabetical order based on first_name

In C, you would probably want to use the qsort function. C is off-topic
here, so I'll just tell you to look up "qsort" in your C textbook or on the
web.

In C++, you'd do something like this:

#include <algorithm>
#include <cstddef>
#include <cstring>
#include <iostream>
#include <iterator>
#include <ostream>

namespace
{
std::size_t const NAME_LENGTH(9);

struct test_struct
{
int some_number;
char first_name[NAME_LENGTH + 1];
};

bool order_by_first_name (test_struct const &ts1,
test_struct const &ts2)
{
return std::strcmp(ts1.first_name, ts2.first_name) < 0;
}

std::ostream &operator<< (std::ostream &os, test_struct const &ts)
{
return os << ts.some_number << ": " << ts.first_name;
}
}

int main()
{
std::size_t const ARRAY_SIZE(3);
test_struct s_table[ARRAY_SIZE] = { { 1, "Tom" },
{ 2, "Dick" },
{ 3, "Harry" } };

typedef std::ostream_iterator test_struct_writer ts_writer(std::cout, "n");

std::cout << "Before sorting:n";
std::copy(s_table, s_table + ARRAY_SIZE, ts_writer);

std::sort(s_table, s_table + ARRAY_SIZE, order_by_first_name);

std::cout << "nAfter sorting:n";
std::copy(s_table, s_table + ARRAY_SIZE, ts_writer);
}

--
Russell Hanneken
[email]rghanneken (AT) pobox (DOT) com[/email]
Remove the 'g' from my address to send me mail.



Back to top
York
Guest





PostPosted: Sat Oct 25, 2003 9:06 pm    Post subject: Re: Built-in sorting algorithms? Reply with quote


"Russell Hanneken" <rghanneken (AT) pobox (DOT) com> wrote

Quote:
York wrote:

In C, you would probably want to use the qsort function. C is off-topic
here, so I'll just tell you to look up "qsort" in your C textbook or on
the
web.

In C++, you'd do something like this:

Thanks, that is pretty much what I've been looking for.



Back to top
Micah Cowan
Guest





PostPosted: Sat Oct 25, 2003 10:24 pm    Post subject: Re: Built-in sorting algorithms? Reply with quote

"Russell Hanneken" <rghanneken (AT) pobox (DOT) com> writes:

Quote:
York wrote:
Lets say I have the following structure

struct test_struct
{
int some_number;
char first_name[10]
}

test_struct s_table[100]

In short a table within a table with a number, I would like to know if
there are any standard sorting algorithms implemented in c/c++ to sort
s_table in an alphabetical order based on first_name

In C, you would probably want to use the qsort function. C is off-topic
here, so I'll just tell you to look up "qsort" in your C textbook or on the
web.

However, since std::qsort() is also in C++, it would still be
topical to discuss it, though I'd agree that C++ provides better
candidates.

--
Micah J. Cowan
[email]micah (AT) cowan (DOT) name[/email]

Back to top
Post new topic   Reply to topic    C++Talk.NET Forum Index -> C++ language (comp.lang.c++) All times are GMT
Page 1 of 1

 
 


Powered by phpBB © 2001, 2006 phpBB Group