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

Vector sort with a struct?

Guys, I have these headers:
#include <stdlib>
#include <math>
#include <iostream>
#include <iomanip>
#include <fstream>
#include <vector>
#include <string>
#include <algorithm>

*amongst others) and a a data structure:

// Creates part of basic framework of the output numbering system
struct vertexPoints {
int num;
double xc;
double yc;
double zc;
};

from this, I have created a vector

vector<vertexPoints> myPoints;

with vertices push_back()'ed onto it. I want to be able to sort this -
mainly by x, but ideally by x, then y, then z.

if I type mypoints.sort(myPoints.begin(), myPoints.end, comparex);

with

bool comparex(myPoints& x, myPoints& y)
{
return x.xc < y.xc;
}

the compiler throws a tantrum saying that sort is not a member function of
the vertex<myPoints> vector.

What am I doing wrong?

TIA,

Alex.

--
Reply to:alex an.ti livingstone sp@am btinternet.com cutting the usual...
Jul 19 '05 #1
2 11245
On Fri, 12 Sep 2003 12:39:26 +0100, {AGUT2} {H}-IWIK
<al********************@ambtinternet.com> wrote:
Guys, I have these headers:
#include <stdlib>
#include <math>
#include <iostream>
#include <iomanip>
#include <fstream>
#include <vector>
#include <string>
#include <algorithm>

*amongst others) and a a data structure:

// Creates part of basic framework of the output numbering system
struct vertexPoints {
int num;
double xc;
double yc;
double zc;
};

from this, I have created a vector

vector<vertexPoints> myPoints;

with vertices push_back()'ed onto it. I want to be able to sort this -
mainly by x, but ideally by x, then y, then z.

if I type mypoints.sort(myPoints.begin(), myPoints.end, comparex);
std::sort(myPoints.begin(), myPoints.end, comparex);

with

bool comparex(myPoints& x, myPoints& y)
bool comparex(myPoints const& x, myPoints const& y)

{
return x.xc < y.xc;
}

the compiler throws a tantrum saying that sort is not a member function of
the vertex<myPoints> vector.


Sort is not a member of vector, it is a namespace std function.

Tom
Jul 19 '05 #2

"tom_usenet" <to********@hotmail.com> wrote in message
news:u9********************************@4ax.com...
On Fri, 12 Sep 2003 12:39:26 +0100, {AGUT2} {H}-IWIK
<al********************@ambtinternet.com> wrote:
Guys, I have these headers:
#include <stdlib>
#include <math>
#include <iostream>
#include <iomanip>
#include <fstream>
#include <vector>
#include <string>
#include <algorithm>

*amongst others) and a a data structure:

// Creates part of basic framework of the output numbering system
struct vertexPoints {
int num;
double xc;
double yc;
double zc;
};

from this, I have created a vector

vector<vertexPoints> myPoints;

with vertices push_back()'ed onto it. I want to be able to sort this -
mainly by x, but ideally by x, then y, then z.

if I type mypoints.sort(myPoints.begin(), myPoints.end, comparex);
std::sort(myPoints.begin(), myPoints.end, comparex);


Should probably be:

std::sort(myPoints.begin(), myPoints.end(), comparex);

with

bool comparex(myPoints& x, myPoints& y)
bool comparex(myPoints const& x, myPoints const& y)


Try:

bool comparex(const vertexPoints& x, const vertexPoints& y)

instead.
{
return x.xc < y.xc;
}

the compiler throws a tantrum saying that sort is not a member function ofthe vertex<myPoints> vector.


Sort is not a member of vector, it is a namespace std function.

Tom


You could also consider making a "bool operator<(const vertexPoints&)" in
your vertextPoints struct. Then you could use the sort function like this:

std::sort(myPoints.begin(), myPoints.end());

That is IMHO a more object oriented way of doing this.

BR,
Peter Kragh
Jul 19 '05 #3

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

Similar topics

2
by: Sims | last post by:
Hi, I have a structure as follow struct sIntStructure { int m_nNumber; // // A few more variables //
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),
5
by: SB | last post by:
I have an assignment where we are supposed to mimic a scheduler routine of an OS. We are supposed to schedule some processes, an each process has the following attributes... - process id -...
5
by: RocTheEngy | last post by:
Greetings c.l.c... I am trying to understand some structure definitions in working code (e.g. compiles, runs & produces expected results) that I have. I think I've trimmed the code to what is...
8
by: markww | last post by:
Hi, If I have a vector of structs like this: struct IMAGE { unsigned char *pPix; string strName; int nNumber; };
1
by: avimitrani | last post by:
Hi, I am trying to apply the sort() algorithm on a vector of structs. I want the vector to be sorted according to one of the struct fields. I see two ways in which this could be done, and neither...
22
by: sandy | last post by:
I am trying to make a simulated directory structure application for a course. I am using a Vector to store pointers to my Directory objects (as subdirectories of the current object). In my...
4
by: gallows | last post by:
I've tried to use C qsort() on an object derivate by std::vector, but it doesn't work. I've the follow structure: struct Item { std::string name; int number; }
3
by: lweunice | last post by:
hi, I need to partially sort a struct vector and I have written a function that looks like: void AxisSort(int start, int end){ sort(prims.begin() + start, prims.begin() +...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 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 former...
0
by: ryjfgjl | last post by:
In our work, we often need to import Excel data into databases (such as MySQL, SQL Server, Oracle) for data analysis and processing. Usually, we use database tools like Navicat or the Excel import...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
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: 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:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...

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.