Help with passing a vector by reference and creating paired vectors

Hi, I'm still getting the hang of C++ and am currently working on a project for one of my classes. My goal is to create a program that reads a series of integers from a file, places those integers into a vector, tallies those integers by creating separate vectors in pairs (the number in the file, and how many times that number is repeated in the file), then finally outputs the new paired vectors into a new file. I still haven't fully grasped how to pass vectors into functions by reference, so that's mainly where I'm struggling. I've included what I have so far. I'm not looking for anyone to completely solve this for me, I'm just hoping someone can tell me if I'm headed in the right direction, and what I should be doing/where I should be looking for the next step.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
#include <cstdlib>
#include <iostream>
#include <fstream>
#include <vector>
#include "TallyIntegers.h"

using namespace std;

void readFile(vector<int> list, ifstream &file){
	file.open("ProductFile.txt", ios::in);
	int m;
	while (file >> m){
		list.push_back(m);
	}
	file.close();
	cout << "Tally: " << endl;
	for (int i = 0; i < list.size(); i++){
		cout << list[i] << endl;
	}
	cin.get();
}

void tallyInt(){

}

void displayTal(){

}

int main(){
	ifstream inFile;
	ofstream outfile;
	vector<int> n;
	readFile(n, inFile);
	return 0;
}
Last edited on
closed account (SECMoG1T)
void readFile(vector<int> list, ifstream &file) here you'll need to pass your vector by reference , currently you are passing it by value, it should be something like this vector<int>& list
closed account (D80DSL3A)
The syntax for pass by reference is the same regardless of the data type. Just put an '&' following the type name, as you did with ifstream. So, the desired prototype is:
void readFile(vector<int> &list, ifstream &file);.

Using two other vectors to record the values tally is workable, but less than ideal. I believe the data structure given by std::map is ideal. I suppose you're following an assignment requirement though, so you must implement the solution that way.

Declare the other 2 vectors in main(), as you'll have to pass them to the displayTal function after tallyInt fills them.
You'll need to pass all 3 vectors to tallyInt. You could pass the vector n to tallyInt by value (it would work), but why force a copy of the entire vector to be made. I'd recommend passing n by constant reference because the function is intended to not modify n. This prototype would be:
void tallyInt( const vector<int>& sourceVec, vector<int>& valuesVec, vector<int>& tallyVec );. tallyInt could be passed both valuesVec and tallyVec by constant reference, though it would also work without the const specifier.

Hope that gets you going.

EDIT: Sorry andy1992. I wasn't aware of your post. Yes, it took me that long to write my post!
Last edited on
Thank you both. Not sure why I couldn't figure out that I just needed to add &. fun2code, I'm not sure if I was misleading but I don't believe I need to use 3 different vectors. I believe I only need the original vector and then a new vector of pairs.
Topic archived. No new replies allowed.