Built-in sorting algorithms?

Y

York

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

*/
 
G

Gianni Mariani

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

Jerry Coffin

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

Russell Hanneken

York said:
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::eek:stream &operator<< (std::eek:stream &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::eek:stream_iterator<test_struct> test_struct_writer;
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);
}
 
Y

York

Russell Hanneken said:
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.
 
M

Micah Cowan

Russell Hanneken said:
York said:
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.
 

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

No members online now.

Forum statistics

Threads
473,754
Messages
2,569,527
Members
44,999
Latest member
MakersCBDGummiesReview

Latest Threads

Top