468,537 Members | 1,682 Online

# vector question

Hi! i'm a beginner user of c++ and i need some help. I use visual c++
6.0 on a p4 under windows 2000.

First, i read a text file with 3 fields: gestionnal age (GA), birth
weight (BW) and repetition (the number fo patient for specific GA and
BW). The file is ordered by GA and BW. I put each field in an vector.
GA took value between 22 and 44 and i want to create 2 others vector
(qcount, qcumulative) containing the number of patients for each GA
and the cumulative number of patients.

/************************************************** *************************
ex:
22 100 2
22 125 3
23 150 1
23 165 4
23 170 8
25 180 10

qcount=(5,13,10)
qcumulative=(5,18,28)
************************************************** **************************/

here's my code:
#include <stdio.h>
#include <stddef.h>
#include <stdlib.h>
#include <math.h>
#define pi 3.1415926

#include <iostream>
#include <fstream>
#include <algorithm>
#include <vector>

using namespace std;
main()
{
vector<int> vga;
vector<int> vrep;
vector<float> vweight;
/* add space between variables for output */
ostream_iterator <int> sortie( cout, " ");
/*FILE1 should be your data file, consisting only of gestational age,
birthweight and
number of repetition in integer format.
*/

/* output and input files */
ifstream input_file ("growth1sorted.txt", ios::in);
/* Import data */

if (!input_file)
{
cerr << "The file cannot be opened";
exit(1);
}
int temp_ga; int repet;
float temp_wt;
while (input_file >> temp_ga >> temp_wt >> repet)
{
vga.insert(vga.end(), temp_ga);
vweight.insert(vweight.end(), temp_wt);
vrep.insert(vrep.end(), repet);
}
/* close the file */
input_file.close();

....

DO I WORK WITH THE RIGHT CLASS (VECTOR) FOR THIS KIND OF TASK?

thanks
Jul 19 '05 #1
2 4007
"Justin" <ju*************@hotmail.com> wrote in message
Hi! i'm a beginner user of c++ and i need some help. I use visual c++
6.0 on a p4 under windows 2000.

First, i read a text file with 3 fields: gestionnal age (GA), birth
weight (BW) and repetition (the number fo patient for specific GA and
BW). The file is ordered by GA and BW. I put each field in an vector.
GA took value between 22 and 44 and i want to create 2 others vector
(qcount, qcumulative) containing the number of patients for each GA
and the cumulative number of patients.
[snip]
DO I WORK WITH THE RIGHT CLASS (VECTOR) FOR THIS KIND OF TASK?

thanks

While it's good that you used a vector, I'd recommend making your program
more organized by making a single vector. You did something this:

vector<int> vga;
vector<int> vrep;
vector<float> vweight;
I'd recommend doing this:

struct patient
{
int vga;
int vrep;
float vweight;
};

vector <patient> patients;

It'll just make your program more organized.

-- MiniDisc_2k2
To reply, replace nospam.com with cox dot net.
Jul 19 '05 #2
ju*************@hotmail.com says...
Hi! i'm a beginner user of c++ and i need some help. I use visual c++
6.0 on a p4 under windows 2000.

First, i read a text file with 3 fields: gestionnal age (GA), birth
weight (BW) and repetition (the number fo patient for specific GA and
BW). The file is ordered by GA and BW. I put each field in an vector.
GA took value between 22 and 44 and i want to create 2 others vector
(qcount, qcumulative) containing the number of patients for each GA
and the cumulative number of patients.
[ ... ]
DO I WORK WITH THE RIGHT CLASS (VECTOR) FOR THIS KIND OF TASK?

separate vectors. I, however, would advise against using a vector for
the data, and use a multiset instead. My reasoning is pretty simple:
using a multiset simplifies some of the other operations you want to do.
As a first cut, I'd consider code something like this:

// warning: untested code.
class patient_data {
return is >> vga >> vrep >> vweight;
}

public:

friend std::istream &operator>>(std::istream &, patient_data &) {
}

bool operator<(patient_data const &other) const {
return vga < other.vga;
}

operator int() { return vrep; }

int vga;
ing vrep;
float vweight;
};

bool read_data(std::set<patient_data> &pd, std::string fname) {
std::ifstream input_file(fname.c_str());
if ( ! input_file)
return false;

std::istream_iterator<patient_data> pd_in(input_file);
std::istream_iterator<patient_data> pd_end;

std::copy(pd_in, pd_end, std::inserter(pd, pd.end()));
return true;
}

int main() {
std::set<patient_data> pd;
typedef std::set<patient_data>::iterator pdi;

std::cerr << "Unable to read data" << std::endl;
return EXIT_FAILURE;
}

std::vector<int> qcount;
std::vector<int> qcumulative;

std::pair<pdi, pdi> r(pd.begin(), pd.begin());

unsigned long total = 0;

while((r=std::equal_range(r.second->vga).first != pd.end()) {
int current = 0;

std::accumulate<r.first, r.second, current);
qcount.push_back(current);
total += current;
qcumulative.push_back(total);
}
}

The condition of the while loop is just a TAD on the gruesome side, but
I think I got it about right. The idea's a little complex, but not too
terrible. std::equal_range finds a range in which the keys (vga in our
case) compare equal, and returns iterators to the first and one beyond
the last item with that value. We initialize the second of the pair to
point to the beginning of the array, so the first time through, it finds
all the records with that value of vga. On the second and subsequent
iterations, it starts one past the last range it just processed, and
continues until the first iterator is set to end(), indicating that
we've processed all the data.

The processing itself uses std::accumulate to add up the values of vrep
for all the records in the range we've found. Since current is an int,
it attempts to treat the record as an int to add its value to current.
We allow that by providing a conversion to int that returns the value we

Note that since we're using std::multiset, there's no real need for the
data to be sorted and pre-processed as you're using it now -- the
multiset will sort the data during input, and counting elements instead
of adding up values of vrep would be just as easy (if anything, it would
be a bit cleaner).

--
Later,
Jerry.

The universe is a figment of its own imagination.
Jul 19 '05 #3

### This discussion thread is closed

Replies have been disabled for this discussion.