how to order a struct?

M

_mario.lat

I have a struct like that (for a graph):

struct edge {
int nodea;
int nodeb;
int weight;
}

I have an array of this struct:
struct e edge[250000000000];



How can I order this struct by the weight?
how can I use 'sort' function of std library?

Thankyou in advance, Mario.
 
S

svenne

_mario.lat said:
I have a struct like that (for a graph):

struct edge {
int nodea;
int nodeb;
int weight;
}

I have an array of this struct:
struct e edge[250000000000];



How can I order this struct by the weight?
how can I use 'sort' function of std library?
so you could do a bubble sort or something similar, or use the std::sort
func,
but it seems to me that you are looking for a tree, why have nodea and nodeb
if you don't want a tree?
check out google for tree algorithms,
/g
 
H

hall

_mario.lat said:
I have a struct like that (for a graph):

struct edge {
int nodea;
int nodeb;
int weight;
}

I have an array of this struct:
struct e edge[250000000000];



How can I order this struct by the weight?
how can I use 'sort' function of std library?

Thankyou in advance, Mario.

First of all, I'd like to advise you to drop the array and use a
std::vector instead, unless you have a good reason to use an array.
Vectors are more user friendly.

Now, in order to sort your struct, you need a function that compares two
Edge objects. In the code below, the function a_less does this by
returning true if the first argument has a smaller 'a' than the second.
This function can also be made a static member function of Edge. For
convenience, i also added a constructor to your class.

The code sorts both an array and a std::vector.

have fun.

//---------------------------------------------------------------------------
#include <vector>
#include <algorithm>

struct Edge {
int a;
int b;
Edge(int a0, int b0): a(a0), b(b0){}
};

bool a_less(Edge & arg1, Edge & arg2 ){ return arg1.a < arg2.a; }

int main(int argc, char* argv[])
{
// sorting an std::vector
std::vector<Edge> v;
v.push_back( Edge(3,0) );
v.push_back( Edge(1,0) );
v.push_back( Edge(2,0) );
std::sort( v.begin(), v.end(), a_less );

// sorting an array
Edge array[3] = { Edge(3,0), Edge(1,0), Edge(2,0) };
std::sort( &array[0], &array[3], a_less );

return 0;
}
//---------------------------------------------------------------------------
 
A

Alex Jin

_mario.lat said:
I have a struct like that (for a graph):

struct edge {
int nodea;
int nodeb;
int weight;
}

I have an array of this struct:
struct e edge[250000000000];



How can I order this struct by the weight?
how can I use 'sort' function of std library?

Thankyou in advance, Mario.

struct edge_compare
{
bool operator()( const edge& xx1, const edge& xx2 ) const
{
return xx1.weight < xx2.weight;
}
};

sort( edge_array, edge_array + sizeof(edge_array) /
sizeof(edge_array[0]), edge_compare() );


Alex Jin.
Shanghai, China.
 
H

Howard

_mario.lat said:
I have a struct like that (for a graph):

struct edge {
int nodea;
int nodeb;
int weight;
}

I have an array of this struct:
struct e edge[250000000000];

!!! I'd love to see the machine that can allocate 250 billion objects!

How can I order this struct by the weight?
how can I use 'sort' function of std library?

Thankyou in advance, Mario.

Even if you *could* allocate 250 billion items, sorting would take forever!

:)
 
J

JKop

Howard posted:
_mario.lat said:
I have a struct like that (for a graph):

struct edge {
int nodea;
int nodeb;
int weight;
}

I have an array of this struct:
struct e edge[250000000000];

!!! I'd love to see the machine that can allocate 250 billion objects!

How can I order this struct by the weight?
how can I use 'sort' function of std library?

Thankyou in advance, Mario.

Even if you *could* allocate 250 billion items, sorting would take
forever!

:)


http://www.intel.com/products/server/processors/server/xeon/index.htm?
iid=ipp_srvr_proc+xeon512kb&


-JKop
 
H

Howard

JKop said:
Howard posted:
_mario.lat said:
I have a struct like that (for a graph):

struct edge {
int nodea;
int nodeb;
int weight;
}

I have an array of this struct:
struct e edge[250000000000];

!!! I'd love to see the machine that can allocate 250 billion objects!

How can I order this struct by the weight?
how can I use 'sort' function of std library?

Thankyou in advance, Mario.

Even if you *could* allocate 250 billion items, sorting would take
forever!

:)


http://www.intel.com/products/server/processors/server/xeon/index.htm?
iid=ipp_srvr_proc+xeon512kb&

Well, I see at least one 64-bit machine there. But I don't see anything
suggesting that you can put 256gig of memory in one!

-Howard
 

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
474,172
Messages
2,570,933
Members
47,472
Latest member
blackwatermelon

Latest Threads

Top