Vectors

closed account (EwCjE3v7)
Hi there,
I just wanted to know what vectors are used for and when they come in handy. Thanks
@CaptainBlastXD

I often wondered the same thing, until I wanted to create a Periodic Table program, and found I would need to keep track of a lot of arrays. So I checked into vectors, and came up with
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
struct Element
{
	int Atomic_Number;
	int x; // To position on screen by column
	int y; // To position on screen by row
	string Symbol;
	string Name;
	double Atomic_Weight;
	float Melting_Point;
	float Boiling_Point;
	double Density;
	int Discovery_Year;
	double Ionization_Energy;
	int Element_Type;
	int Element_State;
};


As you can see, a vector allows you to set up a group that defines one element of a set, that also doesn't need to be of only one type of data.

To create the full set of Elements, I read a text file in this function.

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
void Fill_Vector(vector< Element >& Number, ifstream& ElementStream)
{
	Element Atomic;
	Number.clear();

	while(  ElementStream  ) // Keep reading till the end of file
	{
		ElementStream >> Atomic.Atomic_Number;
		ElementStream >> Atomic.x;
		ElementStream >> Atomic.y;
		ElementStream >> Atomic.Symbol;
		ElementStream >> Atomic.Name;
		ElementStream >> Atomic.Atomic_Weight;
		ElementStream >> Atomic.Melting_Point;
		ElementStream >> Atomic.Boiling_Point;
		ElementStream >> Atomic.Density;
		ElementStream >> Atomic.Discovery_Year;
		ElementStream >> Atomic.Ionization_Energy;
		ElementStream >> Atomic.Element_Type;
		ElementStream >> Atomic.Element_State;
		Number.push_back(Atomic);// After filling in one element, push the data
                                         // onto a stack and then fill in the next elements data
	}
	ElementStream.close(); 
}




All this would have been extremely difficult using individual arrays.

EDIT: Misspelled Periodic. Corrected
Last edited on
closed account (EwCjE3v7)
Oh, that's neat. Thanks
Topic archived. No new replies allowed.