can anyone help me convert this code from array to Vector?
I need this in it

class VectorList  {

      vector<WeatherStation> List;

      vector<WeatherStation>::iterator ThroughTheList;

public:

      VectorList() {;}
#include <iostream>
#include <string>

using namespace std;


class WeatherStation
{
	string StationDesignation;  //Identifies the station
	string StationAgent;        //Who's responsible
	double Temperature;         //The temperature

public:	
	//Define some mutators...These member functions allow us to
    //change values in the object...

	void SetDesignation(string ID)      { StationDesignation = ID; }
	void SetAgent(string Agent)         { StationAgent = Agent; }
	void SetTemperature(double Degree)  {Temperature = Degree; }

	//Define some accessors...These member functions allow us to get
    //  at the internal values

	string GetDesignation()  { return StationDesignation; }
	string GetAgent()        { return StationAgent; }
	double GetTemperature()  { return Temperature; }
	
	 //Define a displayer...this will show the contents of the object
	 // in  an easy to read format..

	void Show()
	{
		cout << StationDesignation << endl;
		cout << StationAgent << endl;
	}

};

//Functions

void Menu();
int AddStation(WeatherStation[],int);
void PostTemperatures(WeatherStation*, int);
void DailyReport(WeatherStation*, int);
void HighLow(WeatherStation*, int);

int main()
{
	string Command;
	WeatherStation names[25];
	int Size = 0;

	while (true){
	Menu();
	cout << "Enter Command: " ;
	getline (cin,Command);
	cout << endl;
	cout << endl;
		if (Command == "Quit")
			break;
		else if (Command == "Add Stations")
			Size = Size + AddStation(names,25);
		else if (Command == "Post Temperatures")
			PostTemperatures (names,Size);
		else if (Command == "Daily Report")
			DailyReport (names, Size);
		else if (Command == "High-Low Report")
			HighLow (names, Size);
	}
}
int AddStation(WeatherStation List[], int MaxSize)             //Add Stations & Agent
{
	string ID, Agent;
	int K;

	cout << "Enter Station Information Below, Stop to Quit" << endl;

	for (K=0 ; K<MaxSize ; K++) {
		cout << "Enter Weather Station Designation: " ;
		getline(cin, ID);
		if (ID == "Stop")
			break;
		cout << "Enter Contact Person: " ;
		getline(cin,Agent);
		if (Agent == "Stop")
			break;

		List[K].SetDesignation(ID);
		List[K].SetAgent(Agent);
	}
	return K;
}

void Menu()  //The Menu
{
	cout << "=========== Menu ===========" << endl;
	cout << "==== Choices: ==============" << endl;
	cout << "Add Stations" << endl;
	cout << "Post Temperatures" << endl;
	cout << "Daily Report" << endl;
	cout << "High-Low Report" << endl;
	cout << "Quit" << endl;
	cout << "==============================" << endl;
	cout << endl;
}

void PostTemperatures(WeatherStation* List, int Size)     //Post Temperatures
{
	int K;
	int Degree;
	cout << "Enter Reported Temperatures.." << endl;

	for (K=0 ; K<Size ; K++){
		List[K].Show();

		cout << "Enter Temperature: ";
		cin>>Degree;
		
		List[K].SetTemperature(Degree);
	}
	
}

void DailyReport (WeatherStation* List, int Size)    //Daily report
{
	int K;
	double Sum=0;
	double CelSum;
	double CelDegrees [25];
	cout << "==========NGS Daily Temperature Report==========" << endl;
	cout << "================================================" << endl;
	cout << "------------------------------------------------" << endl;
	for (K=0 ; K<Size ; K++){
		CelDegrees[K]=(5 *(List[K].GetTemperature() - 32))/9;   //Finding the Celsius
		CelSum=(5 *(Sum - 32))/9;								//Finding the Mean Celsius
		cout << List[K].GetDesignation() << "    :      " << List[K].GetTemperature() << "\370F     " << CelDegrees[K] << "\370C" << endl;
		cout << "------------------------------------------------" << endl;
		
		Sum=Sum+List[K].GetTemperature();                       //Getting the Sum of all temperatures
	}
	cout << "Mean    :        " << (Sum/Size) << "\370F     " << (CelSum/Size) <<"\370C" << endl;
	cout << "================================================" << endl;
}

void HighLow(WeatherStation* List, int Size)       //High-Low
{
	int K;
	double Max=List[0].GetTemperature();
	double Min=List[0].GetTemperature();
	double MinCelDegrees=0, MaxCelDegrees=0;

	for (K=0 ; K < Size ; K++){
		if(List[K].GetTemperature() > Max) {        //Finding Max
			Max=List[K].GetTemperature();
		MaxCelDegrees=(5 *(Max - 32))/9;            //Finding Max Celsius
		}
	
		
		if(List[K].GetTemperature() < Min){         //Finding Min
			Min=List[K].GetTemperature();
			MinCelDegrees=(5 *(Min - 32))/9;        //Finding Min Celsius
		}
	}
		

	cout << "========NGS Temperature Data Report========" << endl;
	cout << "-------------------------------------------" << endl;
	cout << "Lowest Temperature:  " << Min << "\370F    " << MinCelDegrees << "\370C" << endl;
	cout << "-------------------------------------------" << endl;
	cout << "Highest Temperature:  " << Max << "\370F    " << MaxCelDegrees << "\370C" << endl;
	cout << "-------------------------------------------" << endl;
	cout << "========End Temperature Data Report========" << endl;
}

Recommended Answers

All 4 Replies

can anyone help me convert this code from array to Vector?

I will help you.

  1. Instead of declaring an array, declare a vector.
  2. Anytime you want to add to the vector, call the push_back() member:
    myvector.push_back(input);
  3. Anytime you want to get stuff from the vector, you can use subscripts to access a specific array element:
    cout << myvector[i];
  4. You can get the size of the vector easily by calling the size() member:
    for(int i=0, size=myvector.size(); i<size; i++)
cout << myvector[i];

Isn't this an array? because of the

I'm still kinda confused, maybe you can give some examples using my code above. I'll probably understand it more that way. Thanks!

can give some examples using my code above. I'll probably understand it more that way.

You will actually understand it less when you are spoon fed the code.

You seem like an intelligent individual.. why not give it a try? What's the worst that could happen..??

Isn't this an array? because of the

Being the astute CS student that I know you are, I'm sure you've studied the vector class and found out that it is based on the array datastructure:

Vector
Vectors are a kind of sequence containers. As such, their elements are ordered following a strict linear sequence.

Vector containers are implemented as dynamic arrays; Just as regular arrays, vector containers have their elements stored in contiguous storage locations, which means that their elements can be accessed not only using iterators but also using offsets on regular pointers to elements.

But unlike regular arrays, storage in vectors is handled automatically, allowing it to be expanded and contracted as needed.

Vectors are good at:

* Accessing individual elements by their position index (constant time).
* Iterating over the elements in any order (linear time).
* Add and remove elements from its end (constant amortized time).

So, to answer your question.. yes, the subscript operator is overloaded for the vector object, giving it array-like capability.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.