New to Vector

I have this code that my professor put up in class. if someone don't mind explain it i would appreciate it a lot!
Thank you... New to vector!

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
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
#include<iostream>
#include<fstream>
#include<iomanip>
#include<vector>

using namespace std;

vector<float>GetInfo();
void PrintVectors(vector<int>,vector<float> fMarks);
int GetOccur(float,int,vector<float>&);

//--------------------------------
int main()
{
	vector<float> fMarks;
	vector<int> iCount;

	fMarks = GetInfo();
	int iI = 0;
	while (iI < fMarks.size())
	{
		int iElem = GetOccur(fMarks[iI], iI, fMarks);
		iCount.push_back(iElem);
		iI++;
	}
	PrintVectors(iCount,fMarks);

}
//--------------------------------
vector<float> GetInfo()                     //Get the info
{								//from the data.
	float iElement;
	vector<float> iX;
	ifstream fin;
	fin.open("data.txt");
	fin >> iElement;
	while (fin)
	{
		iX.push_back(iElement);
		fin >> iElement;
	}
	fin.close();
	return(iX);

}
//--------------------------------
void PrintVectors(vector<int>iCounts,vector<float> fMarks)
{
 cout << endl;
 cout << endl;
 cout << setw(40) << "Marks" << setw(20) << " Occurrance" << endl;
 cout << setw(40) << "-----" << setw(20) << " ----------" << endl;
 for (int iI = 0; iI<fMarks.size(); iI++)
	 cout << setw(38) << fMarks[iI] << 
			 setw(18) << iCounts[iI] << endl;
}
//--------------------------------
int GetOccur(float fMark, int pos, vector<float>& fMarks)
{
	int iCount = 1;
	for (int iI = pos+1; iI<fMarks.size(); iI++)
	{
		while (fMarks[iI]==fMark)
		{
			fMarks.erase(fMarks.begin()+iI);
			iCount++;
			if (iI >= fMarks.size()) 
				break;
		}
	}
	return(iCount);
}
//--------------------------------
Think of a vector as a more advanced array. I hope you are more familiar with those.
Vectors are less efficient, but safer (they expand or contract as needed, so you can't overrun the bounds) and can save some coding time (because they have convenient member functions that save you having to code lots of loops). Some of the member functions used here are:
.push_back( ) - adds another element to the end of the vector (making it one bigger)
.size() - returns the current length of the vector; unlike a normal array this can change as you add or delete elements
.erase() - remove an element and close up the remainder

Just as you can use a pointer to traverse the elements of an array, you can use an "iterator" to do roughly the same in a vector (fMarks.begin() returns an iterator (like a pointer) to the start of an array; adding i1 points i1 elements onward - just like pointers).



From the top, your program
- creates vectors to hold floating point numbers and integers respectively
- GetInfo reads marks from a file and, one-by-one, adds them (using push_back) to a vector
- the while loop in main goes through the marks and sends them to GetOccur, which does two things:
- counts the number of occurrences of a particular mark;
- removes this particular mark from the remainder of the array (note the .erase) so it isn't counted again.
- PrintVectors outputs the particular mark (from vector fmarks) and number of occurrences (from vector iCount)
Topic archived. No new replies allowed.