473,231 Members | 1,695 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,231 software developers and data experts.

Built-in sorting algorithms?

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

*/
Jul 19 '05 #1
5 9124
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

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.

Jul 19 '05 #2
In article <3f9a0b9c$1_2@aeinews.>, sd**@dfssdfs.net says...
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.
Jul 19 '05 #3
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.

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> 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);
}

--
Russell Hanneken
rg********@pobox.com
Remove the 'g' from my address to send me mail.
Jul 19 '05 #4

"Russell Hanneken" <rg********@pobox.com> wrote in message
news:ge****************@newsread4.news.pas.earthli nk.net...
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.
Jul 19 '05 #5
"Russell Hanneken" <rg********@pobox.com> writes:
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
mi***@cowan.name
Jul 19 '05 #6

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

1
by: JimmyT | last post by:
I just configured and installed 2.3.4 and noticed there is no datetime module. I noticed there is a datetimemodule.c file that did not get built (ie no object file). Is there something I need to...
1
by: Alex Elbert | last post by:
Hi I have built dynamic HTMLTable. Now I want to attach it directly to the Email Body - it is already built, so why not to use a ready table. However, I cannot find the way of getting plain HTML...
0
by: Andrew Crook | last post by:
does MYSQL have a quota built into it! I need it limit the size of each database AndiC
1
by: Mark | last post by:
Is there a way to execute a statement that is built dynamically by a .NET application. For example I have a loop that is reading values from a database and I want to do something like the...
4
by: Yasutaka Ito | last post by:
Hi, Is there a way to determine which version of .NET Framework any given assembly is built with? thanks! -Yasutaka
1
by: William | last post by:
Looking for a pre built dotnet corporate or small business website template.
1
by: William | last post by:
Looking for a pre built dot net website for consulting business. I am trying to put up a quick business web for a dot net frame work. I have a provider already. I am trying to save time. Any...
1
by: Daniel | last post by:
is there any way to get to a unique build verion of an assembly at runtime? e.g. a version that is unique to the time that the assembly was built?
48
by: meyer | last post by:
Hi everyone, which compiler will Python 2.5 on Windows (Intel) be built with? I notice that Python 2.4 apparently has been built with the VS2003 toolkit compiler, and I read a post from Scott...
3
by: drewj840 | last post by:
I built a Windows service that sweeps a set of folders every 60 seconds and puts the files into a SQL Server database. I am creating a second service that will delete this set of folders and recreate...
3
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 3 Jan 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). For other local times, please check World Time Buddy In...
0
by: stefan129 | last post by:
Hey forum members, I'm exploring options for SSL certificates for multiple domains. Has anyone had experience with multi-domain SSL certificates? Any recommendations on reliable providers or specific...
0
Git
by: egorbl4 | last post by:
Скачал я git, хотел начать настройку, а там вылезло вот это Что это? Что мне с этим делать? ...
1
by: davi5007 | last post by:
Hi, Basically, I am trying to automate a field named TraceabilityNo into a web page from an access form. I've got the serial held in the variable strSearchString. How can I get this into the...
0
by: MeoLessi9 | last post by:
I have VirtualBox installed on Windows 11 and now I would like to install Kali on a virtual machine. However, on the official website, I see two options: "Installer images" and "Virtual machines"....
0
by: DolphinDB | last post by:
The formulas of 101 quantitative trading alphas used by WorldQuant were presented in the paper 101 Formulaic Alphas. However, some formulas are complex, leading to challenges in calculation. Take...
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: Aftab Ahmad | last post by:
Hello Experts! I have written a code in MS Access for a cmd called "WhatsApp Message" to open WhatsApp using that very code but the problem is that it gives a popup message everytime I clicked on...
0
by: Aftab Ahmad | last post by:
So, I have written a code for a cmd called "Send WhatsApp Message" to open and send WhatsApp messaage. The code is given below. Dim IE As Object Set IE =...

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.