473,396 Members | 2,020 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,396 software developers and data experts.

Sorting a struct

I'm having a problem sorting an array of structs that is a class
member. After doing a bit of research I thought I had my hands around
the problem. I've overloaded the '<' operator for the struct, and
then I pass sort a pointer to the first array element and the number
of elements. Everything compiles fine until I try to call std::sort()
on the array. I'm trying to make a class that acts like a dealer to
use in a card game, and I need to sort the cards as part of my shuffle
routine. I tried to post "the minimum compilable code", but it's
still a little long (circa 80 lines). Any help? Thanks. Joe
_________________________________
#include <algorithm>
#include <iostream>
#include <string>

using namespace std;

struct acard{
public:
int suit;
int value;
string str;
unsigned int index;
bool operator <(const acard& rhs){
return index < rhs.index;
}
};

class deck{
private:
string theSuits; // contains suits in order
string theValues; // contains values in order
int numcards;
acard* card; // an array of cards

public:
deck();
~deck();
void show();
void shuffle();
};

deck::deck():
theSuits("\x05\x04\x03\x06") //c, d, h, s
,theValues("23456789TJKQA")
,numcards(theSuits.length() * theValues.length())
,card(new acard[numcards])
{
int cardnum(0);
for(int i = 0; i < theSuits.length(); ++i){
for(int j = 0; j < theValues.length(); ++j){
cardnum = (i * theValues.length()) + j;
card[cardnum].suit = i;
card[cardnum].value = j;
card[cardnum].str = theValues.substr(j,1) +
theSuits.substr(i,1); }
}
}

deck::~deck(){
delete [] card;

}

void deck::show(){
for(int i=0; i < numcards; ++i){
cout << (i % 13 ? ' ' : '\n') << card[i].str ;
}
cout << endl;
}

void deck::shuffle(){
for(int i=0; i < numcards; ++i){
card[i].index = rand();
}
//sort(card, card + numcards); // this line breaks the
compilability
} // but is needed to shuffle the
deck
int main()
{
srand(time(0));

deck mydeck;
mydeck.show();
mydeck.shuffle();
mydeck.show();

system("pause");
return 0;
}
___________________________________
Jul 22 '05 #1
3 4960
J. Campbell wrote:
I'm having a problem sorting an array of structs that is a class
member. After doing a bit of research I thought I had my hands around
the problem. I've overloaded the '<' operator for the struct, and
then I pass sort a pointer to the first array element and the number
of elements. Everything compiles fine until I try to call std::sort()
on the array. I'm trying to make a class that acts like a dealer to
use in a card game, and I need to sort the cards as part of my shuffle
routine. I tried to post "the minimum compilable code", but it's
still a little long (circa 80 lines). Any help? Thanks. Joe
_________________________________
#include <algorithm>
#include <iostream>
#include <string>

using namespace std;

struct acard{
public:
int suit;
int value;
string str;
unsigned int index;
bool operator <(const acard& rhs){


replace the above line with:

bool operator <(const acard& rhs) const {
Jul 22 '05 #2

"Gianni Mariani" <gi*******@mariani.ws> wrote in message
news:cf********@dispatch.concentric.net...
J. Campbell wrote:
I'm having a problem sorting an array of structs that is a class
member. After doing a bit of research I thought I had my hands around
the problem. I've overloaded the '<' operator for the struct, and
then I pass sort a pointer to the first array element and the number
of elements. Everything compiles fine until I try to call std::sort()
on the array. I'm trying to make a class that acts like a dealer to
use in a card game, and I need to sort the cards as part of my shuffle
routine. I tried to post "the minimum compilable code", but it's
still a little long (circa 80 lines). Any help? Thanks. Joe
_________________________________
#include <algorithm>
#include <iostream>
#include <string>

using namespace std;

struct acard{
public:
int suit;
int value;
string str;
unsigned int index;
bool operator <(const acard& rhs){


replace the above line with:

bool operator <(const acard& rhs) const {


Thanks Gianni. How *should* I have known that the comparitor function
*needed* to be const?

Jul 22 '05 #3
Joe C wrote:
"Gianni Mariani" <gi*******@mariani.ws> wrote in message
news:cf********@dispatch.concentric.net...
J. Campbell wrote:
I'm having a problem sorting an array of structs that is a class
member. After doing a bit of research I thought I had my hands around
the problem. I've overloaded the '<' operator for the struct, and
then I pass sort a pointer to the first array element and the number
of elements. Everything compiles fine until I try to call std::sort()
on the array. I'm trying to make a class that acts like a dealer to
use in a card game, and I need to sort the cards as part of my shuffle
routine. I tried to post "the minimum compilable code", but it's
still a little long (circa 80 lines). Any help? Thanks. Joe
_________________________________
#include <algorithm>
#include <iostream>
#include <string>

using namespace std;

struct acard{
public:
int suit;
int value;
string str;
unsigned int index;
bool operator <(const acard& rhs){


replace the above line with:

bool operator <(const acard& rhs) const {

Thanks Gianni. How *should* I have known that the comparitor function
*needed* to be const?


I found out because the compiler said so :

g++ xxcards.cpp -o xxcards
/include/c++/3.4.0/bits/stl_algo.h: In function `const _Tp&
std::__median(const _Tp&, const _Tp&, const _Tp&) [with _Tp = acard]':
/include/c++/3.4.0/bits/stl_algo.h:2484: instantiated from `void
std::__introsort_loop(_RandomAccessIterator, _RandomAccessIterator,
_Size) [with _RandomAccessIterator = acard*, _Size = int]'
/include/c++/3.4.0/bits/stl_algo.h:2555: instantiated from `void
std::sort(_RandomAccessIterator, _RandomAccessIterator) [with
_RandomAccessIterator = acard*]'
xxcards.cpp:67: instantiated from here
/include/c++/3.4.0/bits/stl_algo.h:90: error: passing `const acard' as
`this' argument of `bool acard::operator<(const acard&)' discards qualifiers
/include/c++/3.4.0/bits/stl_algo.h:91: error: passing `const acard' as
`this' argument of `bool acard::operator<(const acard&)' discards qualifiers
/include/c++/3.4.0/bits/stl_algo.h:93: error: passing `const acard' as
`this' argument of `bool acard::operator<(const acard&)' discards qualifiers
/include/c++/3.4.0/bits/stl_algo.h:97: error: passing `const acard' as
`this' argument of `bool acard::operator<(const acard&)' discards qualifiers
/include/c++/3.4.0/bits/stl_algo.h:99: error: passing `const acard' as
`this' argument of `bool acard::operator<(const acard&)' discards qualifiers
Jul 22 '05 #4

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

Similar topics

5
by: York | last post by:
Lets say I have the following structure struct test_struct { int some_number; char first_name } test_struct s_table
2
by: William Payne | last post by:
Hello, I have two structs: struct FileEntry { FileEntry(const char* name, const char* type, std::size_t file_size, HICON icon) : m_file_size(file_size),
8
by: nidhog | last post by:
Hello guys, I made a script that extracts strings from a binary file. It works. My next problem is sorting those strings. Output is like: ---- snip ---- 200501221530
3
by: b83503104 | last post by:
In matlab, the sort function returns two things: =sort() a = 5 7 8 b = 1 3 2 where a is the sorted result, and b is the corresponding index. Is there C or C++ code...
3
by: sugaray | last post by:
hi, i have to build a linked-list which has another sturcture _score as it's data entry, so how can i sort such linked-list based on, let say, history score into proper order...
4
by: chellappa | last post by:
Hi Everybody, This is i am trying for sorting linked using pointer.......... but i dont know wthere this logic is good or not........... please correct eroor.......it is not working ,i t simple...
4
by: FBM | last post by:
Hi, I am working on a program that simulates one of the elements of ATM. The simulation stores events which occurs every some milliseconds for a certain amount of time. Every time that an event...
4
by: rushik | last post by:
Hello all, I am using structure in my program, and my aim is to sort this structure based on some optimized sorting algo. structure is struct data { int account;
2
by: vikerneso | last post by:
I'm trying to sort an array of structs. My code looks something like this: struct Foo { int number; }; int main() {
4
by: arnuld | last post by:
This program follows from the section 6.5 of K&R2 where authors created a doubly-linked list using a binary-tree based approach. The only thing I have rewritten myself is the getword function. I am...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...

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.