vector class string sort

V

Venthor

Hello,
I'm having some problems wrapping my head around what I need to do.
I'm using a vector to store an array of a specific class object full
of data.

class part_info
{
public:
string part_no;
string raw_qty;
int qty;
int ref;

part_info() : part_no(" "), raw_qty(" "), qty(0), ref(0) {}
part_info(string NewPartNo, string NewRawQty, int NewQty, int
NewRef) : part_no(NewPartNo), raw_qty(NewRawQty), qty(NewQty),
ref(NewRef) {}

};

.....

void setup {
NAMEVECTOR theVector;
NAMEVECTOR::iterator theIterator;
.....

//add a dataset to my vector
theVector.push_back(part_info(part_no,raw_qty,qty,ref));

}

When I'm finished adding my data to my vector I want to be able to
sort it by part_no.
I've looked at overriding the operator < and then using the std::sort,
but I'm getting compiling errors and while I understand the basic of
what is going on I must be missing something.

Could someone please help me with this?

Thanks
Venthor




typedef vector<part_info> NAMEVECTOR;
 
R

Richard Herring

Hello,
I'm having some problems wrapping my head around what I need to do.
I'm using a vector to store an array of a specific class object full
of data.

class part_info
{
public:
string part_no;
string raw_qty;
int qty;
int ref;

part_info() : part_no(" "), raw_qty(" "), qty(0), ref(0) {}
part_info(string NewPartNo, string NewRawQty, int NewQty, int
NewRef) : part_no(NewPartNo), raw_qty(NewRawQty), qty(NewQty),
ref(NewRef) {}

};

....

void setup {
NAMEVECTOR theVector;
NAMEVECTOR::iterator theIterator;
....

//add a dataset to my vector
theVector.push_back(part_info(part_no,raw_qty,qty,ref));

}

When I'm finished adding my data to my vector I want to be able to
sort it by part_no.
I've looked at overriding the operator < and then using the std::sort,
but I'm getting compiling errors and while I understand the basic of
what is going on I must be missing something.

Could someone please help me with this?

Sure. Where's your definition for the overridden operator<() ?
 
F

Fei Liu

Venthor said:
Hello,
I'm having some problems wrapping my head around what I need to do.
I'm using a vector to store an array of a specific class object full
of data.

class part_info
{
public:
string part_no;
string raw_qty;
int qty;
int ref;

part_info() : part_no(" "), raw_qty(" "), qty(0), ref(0) {}
part_info(string NewPartNo, string NewRawQty, int NewQty, int
NewRef) : part_no(NewPartNo), raw_qty(NewRawQty), qty(NewQty),
ref(NewRef) {}

};

....

void setup {
NAMEVECTOR theVector;
NAMEVECTOR::iterator theIterator;
....

//add a dataset to my vector
theVector.push_back(part_info(part_no,raw_qty,qty,ref));

}

When I'm finished adding my data to my vector I want to be able to
sort it by part_no.
I've looked at overriding the operator < and then using the std::sort,
but I'm getting compiling errors and while I understand the basic of
what is going on I must be missing something.

Could someone please help me with this?

Thanks
Venthor




typedef vector<part_info> NAMEVECTOR;

How can we help you without the error messages and the source code?
 
K

Keith Halligan

Hello,
I'm having some problems wrapping my head around what I need to do.
I'm using a vector to store an array of a specific class object full
of data.

class part_info
{
public:
string part_no;
string raw_qty;
int qty;
int ref;

part_info() : part_no(" "), raw_qty(" "), qty(0), ref(0) {}
part_info(string NewPartNo, string NewRawQty, int NewQty, int
NewRef) : part_no(NewPartNo), raw_qty(NewRawQty), qty(NewQty),
ref(NewRef) {}

};

....

void setup {
NAMEVECTOR theVector;
NAMEVECTOR::iterator theIterator;
....

//add a dataset to my vector
theVector.push_back(part_info(part_no,raw_qty,qty,ref));

}

When I'm finished adding my data to my vector I want to be able to
sort it by part_no.
I've looked at overriding the operator < and then using the std::sort,
but I'm getting compiling errors and while I understand the basic of
what is going on I must be missing something.

Could someone please help me with this?

Thanks
Venthor

typedef vector<part_info> NAMEVECTOR;

Overload < operator in the partinfo class (compare on part num). The
sort member should then sort the vector for you.

What exact compile errors are you getting?
 
?

=?ISO-8859-1?Q?Erik_Wikstr=F6m?=

Hello,
I'm having some problems wrapping my head around what I need to do.
I'm using a vector to store an array of a specific class object full
of data.

class part_info
{
public:
string part_no;
string raw_qty;
int qty;
int ref;

part_info() : part_no(" "), raw_qty(" "), qty(0), ref(0) {}
part_info(string NewPartNo, string NewRawQty, int NewQty, int
NewRef) : part_no(NewPartNo), raw_qty(NewRawQty), qty(NewQty),
ref(NewRef) {}

bool operator<(const part_info& pi) {
return part_no < pi.part_no;
}
};

....

void setup {
NAMEVECTOR theVector;
NAMEVECTOR::iterator theIterator;
....

//add a dataset to my vector
theVector.push_back(part_info(part_no,raw_qty,qty,ref));

}

When I'm finished adding my data to my vector I want to be able to
sort it by part_no.
I've looked at overriding the operator < and then using the std::sort,
but I'm getting compiling errors and while I understand the basic of
what is going on I must be missing something.

Could someone please help me with this?

Notice however that since part_no is a string you might not always get
the results you want (the strings won't be sorted in natural order).
 
V

Venthor

Hi all,

Sorry I forgot to add the override operator:
bool operator<(const part_info& ls, const part_info& rs)
{
return (ls.part_no < rs.part_no);
}

This is the compile error I get. I suspected that it was because the
< didn't work with strings.

error C2678: binary '<' : no operator found which takes a left-hand
operand of type 'const std::string' (or there is no acceptable
conversion)

Thanks,
Venthor
 
V

Victor Bazarov

Venthor said:
Sorry I forgot to add the override operator:
bool operator<(const part_info& ls, const part_info& rs)
{
return (ls.part_no < rs.part_no);
}

This is the compile error I get. I suspected that it was because the
< didn't work with strings.

error C2678: binary '<' : no operator found which takes a left-hand
operand of type 'const std::string' (or there is no acceptable
conversion)

Could it be you forgot to include some header? There _is_ operator<
for strings.

V
 
V

Venthor

#include <ctype.h>
#include <stdio.h>
#include <string.h>
#include <atlstr.h>
#include <time.h>
#include <vector>
#include <algorithm>

#include "rxregsvc.h"
#include "aced.h"
#include "adslib.h"
#include "dbents.h"
#include "acdocman.h"
#include "accmd.h"

using namespace std ;

class part_info
{
public:
string part_no;
string raw_qty;
int qty;
int ref;

part_info() : part_no(" "), raw_qty(" "), qty(0), ref(0) {}
part_info(string NewPartNo, string NewRawQty, int NewQty, int
NewRef) : part_no(NewPartNo), raw_qty(NewRawQty), qty(NewQty),
ref(NewRef) {}
};

bool operator<(const part_info& ls, const part_info& rs)
{
return (ls.part_no < rs.part_no);
}


typedef vector<part_info> NAMEVECTOR;
//function prototypes
void parse_str(char *,char *,char *,int *,int *,int *);
int sort_pa(const void *a, const void *b);
void print_pa(NAMEVECTOR theVector,int, FILE *);
void readtempfile(int, FILE *, FILE *,const char *);
void strip_underscores(char *);


void setup()
{
//variables used
//stores drawing name
char *ptr;
//stores path of 'opened' file
char FilePath[500];
FILE *fp, *fpt;
//return code for selection set
int error;
//data for entity in selection set
struct resbuf *entdata;
//text height of each entity
double txtht;
//text string data
char *tptr; //text
char *mtptr; //mtext
char *sptr;
char *ptr2;
char *ptr3;
char *newptr;
char mtext[300];
char sbuf1[30];
char part_no[30];
char raw_qty[30];
NAMEVECTOR theVector;
NAMEVECTOR::iterator theIterator;
int tot_dig,dup,ref,qty,raw_pn,i=0;
//flag to see if wrote to file
int flag =0;

(code to filter text objects on drawings and parse string for
vector)
//loop
theVector.push_back(part_info(part_no,raw_qty,qty,ref));
//end loop
//sort
sort(theVector.begin(),theVector.end());
//print sorted list to text file
 
P

peter koch

#include <ctype.h>
#include <stdio.h>
#include <string.h>
#include <atlstr.h>
#include <time.h>
#include <vector>
#include <algorithm>

#include "rxregsvc.h"
#include "aced.h"
#include "adslib.h"
#include "dbents.h"
#include "acdocman.h"
#include "accmd.h"

using namespace std ;
[snip]
Two things. First, you haven't included <string> (<string.h> is a
different (C)beast). Secondly, my advice is that you do not use
namespaces - except for perhaps your own specialised ones. Not having
to write std:: is such a little saving that it really is not worth it.

/Peter
 
?

=?ISO-8859-1?Q?Erik_Wikstr=F6m?=

#include <ctype.h>
#include <stdio.h>
#include <string.h>
#include <atlstr.h>
#include <time.h>
#include <vector>
#include <algorithm>

Make that #include <string>, <string.h> is for the C standard library
functions (like strcpy() and friends). As for ctype.h stdio.h and time.h
you can drop the .h and prepend a c (making it <cctype> etc.) but that
will not change anything as far as I can see, but it does make it a bit
more clear that it's C++ and not C you are using (though it's perfectly
legal as you have it).
 
V

Venthor

#include <ctype.h>
#include <stdio.h>
#include <string.h>
#include <atlstr.h>
#include <time.h>
#include <vector>
#include <algorithm>
#include "rxregsvc.h"
#include "aced.h"
#include "adslib.h"
#include "dbents.h"
#include "acdocman.h"
#include "accmd.h"
using namespace std ;

[snip]
Two things. First, you haven't included <string> (<string.h> is a
different (C)beast). Secondly, my advice is that you do not use
namespaces - except for perhaps your own specialised ones. Not having
to write std:: is such a little saving that it really is not worth it.

/Peter

Thank you Peter. You were correct in the fact that I not included
<string>. It works like a charm now and all the part numbers are
correctly sorted in the bom file.
Thanks for everyone's help and next time I post code I will be sure to
post it all instead of trying to conserve 'web space'.

Venthor
 
D

Default User

Venthor wrote:

Thanks for everyone's help and next time I post code I will be sure to
post it all instead of trying to conserve 'web space'.

It would also be a good idea to realize that usenet is not a web site.
You happen to access it via web page that operates more or less as a
newsreader.

If that doesn't make sense, I recommend a search on "how usenet works".




Brian
 
J

James Kanze

[...]
Thank you Peter. You were correct in the fact that I not included
<string>. It works like a charm now and all the part numbers are
correctly sorted in the bom file.
Thanks for everyone's help and next time I post code I will be sure to
post it all instead of trying to conserve 'web space'.

Try a middle road. Create a very small example which doesn't
work, and post it. There was no point in posting all of your
parsing code, since that had nothing to do with the problem.
And as it is, no one could compile the code you posted anyway,
since we don't have all of your application headers. Had the
problem been more complex than just a forgotten header, it could
have been necessary for someone to compile in order to determine
the answer. The general rule is to post a minimal example of
compilable code.

(FWIW: I don't see how the code ever compiled without including
<string>. Apparently, one of the headers you did include
included, indirectly, a partial header for string, which defined
the class, but not the global functions.)
 

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,780
Messages
2,569,611
Members
45,265
Latest member
TodLarocca

Latest Threads

Top