|
From: Jonathan W. <jon...@gm...> - 2007-04-05 08:57:26
|
Hi,
I don't believe there is a built-in way of doing this with custom
structures, so you'll have to do it yourself:
Iterate over the array of associates, and build a vector with the assoc.num=
or
assoc.chr
On the other hand, you might want to look into the STL 'map' class, which
maintains pairs of keys and values
Yours,
Jonathan
On 4/5/07, Albert Jardinier <a_j...@ho...> wrote:
>
>
> Dear friends,
>
> I am new to C++ programming and I am having some difficulty using a piece
> of
> code I have found on the web. All it does, is sorting an array of pairs
> (a,b) in an increasing order. In the present code, the output is also
> pairs
> (a,b) where only the "a" elements are ordered. My question is: How can =
I
> extract from the output only the arrays of "a's" or the array of "b's"
> separately?
> I would appreciate if you could help me or refer me a specific site where
> I
> could understand better the realated issues.
>
> Regards,
>
> Alberto
>
>
>
> // sort.cpp
> //
>
> #include <stdio.h>
> #include <stdlib.h>
>
> #include <iostream>
> #include <algorithm>
>
> #include <vector>
> #include <functional>
>
> using namespace std;
>
> struct associate
> {
> double num;
> int chr;
>
> associate(double n, int c) : num(n), chr(c){};
> associate() : num(0), chr('\0'){};
> };
>
> bool operator<(const associate &x, const associate &y)
> {
> return x.num < y.num;
> }
>
>
> ostream& operator<<(ostream &s, const associate &x)
> {
> return s << "<" << x.num << ";" << x.chr << ">";
> }
>
> int main ()
> {
> vector<associate>::iterator i, j, k;
>
> double *A =3D new double[5];
> int *B =3D new int[5];
>
> associate arr[5];
> for (int a=3D0; a<5; a++) {
> scanf("%lf %d", &A[a], &B[a] );
> arr[a] =3D associate(A[a], B[a]);
> }
>
> // Set up vectors
> vector<associate> v(arr, arr+5), v1((size_t)5),
> v2((size_t)5);
>
> // Copy original vector to vectors #1 and #2
> copy(v.begin(), v.end(), v1.begin());
> copy(v.begin(), v.end(), v2.begin());
>
> // Sort vector #1
> sort(v1.begin(), v1.end());
>
> // Stable sort vector #2
> stable_sort(v2.begin(), v2.end());
>
> // Display the results
> cout << "Original sort stable_sort" << endl;
> for(i =3D v.begin(), j =3D v1.begin(), k =3D v2.begin();
> i !=3D v.end(); i++, j++, k++)
> cout << *i << " " << *j << " " << *k << endl;
>
> return 0;
> }
>
> _________________________________________________________________
> Don't waste time standing in line=97try shopping online. Visit Sympatico =
/
> MSN
> Shopping today! http://shopping.sympatico.msn.ca
>
>
>
> -------------------------------------------------------------------------
> Take Surveys. Earn Cash. Influence the Future of IT
> Join SourceForge.net's Techsay panel and you'll get the chance to share
> your
> opinions on IT & business topics through brief surveys-and earn cash
> http://www.techsay.com/default.php?page=3Djoin.php&p=3Dsourceforge&CID=3D=
DEVDEV
> _______________________________________________
> Dev-cpp-users mailing list
> Dev...@li...
> TO UNSUBSCRIBE: http://www23.brinkster.com/noicys/devcpp/ub.htm
> https://lists.sourceforge.net/lists/listinfo/dev-cpp-users
>
>
--=20
<Morpheus> linux, c'est une question de VI ou de MORE
|