#include <iostream>
#include <map>
#include <sstream>
#include <fstream>
#include <vector>
#include <algorithm>
//create a function that will be used as comparator
bool sortInDecreasingOrder(std::pair<std::string, int> const &a, std::pair<std::string, int> const &b)
{
return a.second > b.second;
}
int main() {
//this map maps each word in the file to their respective count
std::map<std::string, int> stringCount;
std::string word, line;
int count = 0;//this count the total number of words
std::ifstream inputFile("log.txt");
if(inputFile)
{
while(std::getline(inputFile, line))//go line by line
{
std::istringstream ss(line);
//increment the count
stringCount[line]++;
}
}
else
{
std::cout<<"File cannot be opened"<<std::endl;
}
inputFile.close();
std::cout<<"Total number of unique ip's are:"<<stringCount.size()<<std::endl;
//since we cannot sort a std::map by its second value we have create a vector and sort that vector
std::vector<std::pair<std::string, int>> vec(stringCount.begin(), stringCount.end());
//now you can sort this vector
std::sort(vec.begin(), vec.end(), sortInDecreasingOrder);
//lets create a output file and write into it the unique ips
std::ofstream outputFile("code.txt");
for(std::pair<std::string, int> pairElement: vec)//note this time we go through vector vec instead of map
{
std::cout<<pairElement.first<<" => "<<pairElement.second<<" times "<<std::endl;
outputFile<<pairElement.first<<" => "<<pairElement.second<<" times \n";
}
outputFile.close();
return 0;
}
192.168.4.163
192.168.4.163
192.168.4.163
192.168.6.163
192.168.6.163
192.168.5.163
192.168.5.163
192.168.5.163
192.168.5.163
192.168.5.163
192.168.3.163
192.168.3.163
192.168.3.163
192.168.6.163
192.168.6.163
192.168.3.163
192.168.3.163
192.168.3.163