Re: How to do this in most simple way? about list of arrays
On Mar 29, 11:07 pm, Erik Wikstr=F6m <Erik-wikst...@telia.com> wrote:
On 2008-03-29 15:11, Atemporal wrote:
On Mar 29, 9:44 pm, joseph cook <joec...@gmail.com> wrote:
On Mar 29, 9:20 am, Atemporal <Atemporal.s...@gmail.com> wrote:
Just break down what you said...
I want to create a list,
OK... std::list<N> mylist;
each element is an array of 2 integers,
OK... std::list< boost::array<int,2> > myList (or whatever array
structure you like if you don't have the boost library)
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)
// Seehttp://msdn2.microsoft.com/en-us/library/ecdecxh1(VS.80).aspx
}
is there a simple way to do this? thanks a lot.- Hide quoted text -
- 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?
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).
--
Erik Wikstr=F6m- Hide quoted text -
- Show quoted text -
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;
}
}