|
From: Albert J. <a_j...@ho...> - 2007-04-05 08:37:15
|
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 = new double[5];
int *B = new int[5];
associate arr[5];
for (int a=0; a<5; a++) {
scanf("%lf %d", &A[a], &B[a] );
arr[a] = 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 = v.begin(), j = v1.begin(), k = v2.begin();
i != v.end(); i++, j++, k++)
cout << *i << " " << *j << " " << *k << endl;
return 0;
}
_________________________________________________________________
Dont waste time standing in linetry shopping online. Visit Sympatico / MSN
Shopping today! http://shopping.sympatico.msn.ca
|