So I have a web scraper that would scrape tons of article off of a news website. Currently, I am writing a function to filter out the article by months. (Code is shown below) I have stored the months and number of post on a map. However, when I printed out the map, the key and value are not order?
CODE
#include "Source.cpp"
#include <map>
int main() {
std::map<std::string, int> numberOfPost;
int janCounter = 0;
int febCounter = 0;
int marCounter = 0;
int aprCounter = 0;
int mayCounter = 0;
int juneCounter = 0;
int julyCounter = 0;
int augCounter = 0;
int septCounter = 0;
int octCounter = 0;
int novCounter = 0;
int decCounter = 0;
//Calling STScraper and load into Vector
std::vector<STPost>stposts = STScraper();
for (auto i = stposts.begin(); i != stposts.end(); ++i) {
std::tm dt = (*i).getTime();
//std::wcout << dt.tm_mon << std::endl;
if (dt.tm_mon == 0)
{
janCounter++;
}
else if (dt.tm_mon == 1)
{
febCounter++;
}
else if (dt.tm_mon == 2)
{
marCounter++;
}
else if (dt.tm_mon == 3)
{
aprCounter++;
}
else if (dt.tm_mon == 4)
{
mayCounter++;
}
else if (dt.tm_mon == 5)
{
juneCounter++;
}
else if (dt.tm_mon == 6)
{
julyCounter++;
}
else if (dt.tm_mon == 7)
{
augCounter++;
}
else if (dt.tm_mon == 8)
{
septCounter++;
}
else if (dt.tm_mon == 9)
{
octCounter++;
}
else if (dt.tm_mon == 10)
{
novCounter++;
}
else if (dt.tm_mon == 11)
{
decCounter++;
}
}
numberOfPost.insert(std::pair<std::string, int>("January", janCounter));
numberOfPost.insert(std::pair<std::string, int>("Feburary", febCounter));
numberOfPost.insert(std::pair<std::string, int>("March", marCounter));
numberOfPost.insert(std::pair<std::string, int>("April", aprCounter));
numberOfPost.insert(std::pair<std::string, int>("May", mayCounter));
numberOfPost.insert(std::pair<std::string, int>("June", juneCounter));
numberOfPost.insert(std::pair<std::string, int>("July", julyCounter));
numberOfPost.insert(std::pair<std::string, int>("August", augCounter));
numberOfPost.insert(std::pair<std::string, int>("September", septCounter));
numberOfPost.insert(std::pair<std::string, int>("October", octCounter));
numberOfPost.insert(std::pair<std::string, int>("November", novCounter));
numberOfPost.insert(std::pair<std::string, int>("December", decCounter));
std::map<std::string, int>::iterator itr;
std::cout << "\tKEY\tELEMENT\n";
for (auto& p : numberOfPost) {
std::cout << '\t' << p.first
<< '\t' << p.second << '\n';
}
std::cout << std::endl;
return 0;
}
As you can see from my code, I create a loop to count the number of post by months and stored it in a map. However, when I printed out the map, the output does not show the order from Jan to dec. So how do I edit my code such that when I print my map out it would show from Jan to dec ?
OUTPUT
Using a custom sort for your map, you can do something like this:
struct customSort {
bool operator()( const std::string &a, const std::string &b ) const {
static const std::unordered_map<std::string, int> months = {
{ "January", 1 },
{ "Feburary", 2 },
{ "March", 3 },
{ "April", 4 },
{ "May", 5 },
{ "June", 6 },
{ "July", 7 },
{ "August", 8 },
{ "September", 9 },
{ "October", 10 },
{ "November", 11 },
{ "December", 12 },
};
return months.at( a ) < months.at( b );
}
};
std::map<std::string, int, customSort> numberOfPost;
This will sort the your map by the month. It will also throw an exception if you try to put in something that isn't a valid month.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.