How to do this in most simple way? about list of arrays

A

Atemporal

I want to create a list,
each element is an array of 2 integers,
and I want to sort the list according to the first integer of each
element,
is there a simple way to do this? thanks a lot.
 
J

joseph cook

Just break down what you said...
I want to create a list,

OK... std::list said:
each element is an array of 2 integers,

and I want to sort the list according to the first integer of each
element,

std::sort(myList, accordingToTheFirstInteger);

bool accordingToTheFirstInteger(boost::array<int,2>& x,
boost::array<int,2>& y)
{
// Put how you want to sort them here. (Odd first, then even? OR
largest to smallest)
// See http://msdn2.microsoft.com/en-us/library/ecdecxh1(VS.80).aspx
}
 
A

Atemporal

Just break down what you said...




std::sort(myList, accordingToTheFirstInteger);

bool accordingToTheFirstInteger(boost::array<int,2>& x,
boost::array<int,2>& y)
{
// Put how you want to sort them here.   (Odd first, then even? OR
largest to smallest)
// Seehttp://msdn2.microsoft.com/en-us/library/ecdecxh1(VS.80).aspx



}

- Show quoted text -


thanks for your reply.
i'm a beinnger and i do not have boost library.
Need I to create a struct or a class to be the element of the list?
 
A

Atemporal

thanks for your reply.
i'm a beinnger and i do not have boost library.
Need I to create a struct or a class to be the element of the list?- Hide quoted text -

- Show quoted text -


I compile the following code, and it works ok. But I wonder if it is
safe? as no document say list::sort function sort the first element of
its element.


#include <list>
#include <vector>
#include <algorithm>
#include <iostream>


int main( )
{

std::list <std::vector <int>> v1;
std::vector<int> ev;
std::list <std::vector <int>>::iterator Iter;

ev.push_back(7);
ev.push_back(2);

v1.push_back(ev);

ev[0]=3;
ev[1]=4;
v1.push_back(ev);

for ( Iter = v1.begin( ); Iter != v1.end( ); Iter++ )
std::cout << " " << (*Iter)[0];
for ( Iter = v1.begin( ); Iter != v1.end( ); Iter++ )
std::cout << " " << (*Iter)[1];

v1.sort();

for ( Iter = v1.begin( ); Iter != v1.end( ); Iter++ )
std::cout << " " << (*Iter)[0];

for ( Iter = v1.begin( ); Iter != v1.end( ); Iter++ )
std::cout << " " << (*Iter)[1];

}
 
E

Erik Wikström

thanks for your reply.
i'm a beinnger and i do not have boost library.
Need I to create a struct or a class to be the element of the list?

If you know that there will always only be two elements then a struct
might be a good idea. Or you can use std::map as Jim Langston suggested
(or std::multimap if the first integer is not always unique).
 
O

Obnoxious User

thanks for your suggestion.

And I try the following code, it also works. Does list::sort
automatically sort on the first element of its element?

No, it uses by default operator< to sort the data.
If you need to adjust the sorting you could simply write:

struct sorter {
bool operator()(Int_Pair const & a, Int_Pair const & b) const {
return a.first < b.first;
}
};

v1.sort(sorter());
 
A

Atemporal

If you know that there will always only be two elements then a struct
might be a good idea. Or you can use std::map as Jim Langston suggested
(or std::multimap if the first integer is not always unique).


thanks for your suggestion.

And I try the following code, it also works. Does list::sort
automatically sort on the first element of its element?

#include <list>
#include <vector>
#include <algorithm>
#include <iostream>

typedef std::pair <int, int> Int_Pair;

int main( )
{

std::list <Int_Pair> v1;

std::list <Int_Pair>::iterator Iter;

v1.push_back(Int_Pair(8,4));

v1.push_back(Int_Pair(5,4));

v1.push_back(Int_Pair(3,4));


for ( Iter = v1.begin( ); Iter != v1.end( ); Iter++ )
{
std::cout << (*Iter).first << (*Iter).second << std::endl;
}

std::cout << std::endl;

v1.sort();

for ( Iter = v1.begin( ); Iter != v1.end( ); Iter++ )
{
std::cout << (*Iter).first << (*Iter).second << std::endl;
}

}
 

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,769
Messages
2,569,577
Members
45,054
Latest member
LucyCarper

Latest Threads

Top