In: Computer Science
The best way to explain the working of this program is to explain how each line of code works. For that I have added comments on every important line of code in this program. Check it, learn how things work and let me know if you have any doubts or if you need anything to change. If you are satisfied with the solution, please rate the answer. If not, PLEASE let me know before you rate, I’ll help you fix whatever issues. Thanks
Note: To put things simply, this program opens a file that stores country names and populations in the format <country name><space or tab><population> on each line, and display names of countries with population above or equal to 1000,000,000 and then the names of countries with population below or equal to 1000,000.
//including needed libraries
#include <iostream>
#include <string>
#include <fstream>
#include <vector>
#include <sstream>
using namespace std;
int main()
{
//opening worldpop.txt file in read mode, assuming file exists.
ifstream infile("worldpop.txt");
//creating a vector of pair objects. each pair has first value of type string and
//second value of type int.
vector<pair<string, int> > population_directory;
//a string variable to hold a single line
string line;
//looping as long as a line is successfully read from file (will keep looping until
//the end of file)
while (getline(infile, line)) {
//ensuring that line is not blank
if (line.size() > 0) {
//creating a stringstream object to tokenize the line (to split into individual words/values)
stringstream ss(line);
//defining two variables to store country name and population
string country;
int population;
//reading country and population from ss object.
//for example, if the line is "Alabama 1234567", then "Alabama" is stored in country and
//1234567 is stored in population
ss >> country;
ss >> population;
//make_pair method will create and return a pair object of given two values. so here we create
//a pair object of country and population, add that pair to the end of population_directory
//vector.
population_directory.push_back(make_pair(country, population));
}
}
//always remember to close the file once done.
infile.close();
//now we will display the countries with population >= 1000,000,000
cout << "Task 1" << endl;
cout << "Names of countries with population>=1000,000,000" << endl;
//looping through each index in population_directory vector
for (int i = 0; i < population_directory.size(); i++) {
//each pair has the attributes first and second, where first points to the country name
//and second points to the population. here we check if population is above the target
//for pair at index=i in vector
if (population_directory[i].second >= 1000000000) {
//yes, in which cas we print country name (first attribute of pair)
cout << population_directory[i].first << endl;
}
}
//now we repeat the same process to display countries with population <= 1000,000
cout << "Names of countries with population<=1000,000" << endl;
for (int i = 0; i < population_directory.size(); i++) {
if (population_directory[i].second <= 1000000) {
cout << population_directory[i].first << endl;
}
}
}