1

I was tasked to convert a project from java to c++, and I have been trying to convert the code below. Currently, the project was a web scraper, so I have managed to scrape data off a website and store it in a vector. In C++, now that I have the "month" data in a vector and I would like to store the count of each month in a linked hashmap. However, I can't seem to find a C++ version of linked hashmap. Any ideas for me to convert the code below to C++ ?

LinkedHashMap<String, Integer> numberOfPost = new LinkedHashMap<String, Integer>();
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; 
String pattern = "MMM";
SimpleDateFormat sdf = new SimpleDateFormat(pattern);
ArrayList<?> STArray;
try {
    STArray = this.data;
    Iterator STitr = STArray.iterator();
    while (STitr.hasNext()) {
        STPost St = (STPost) STitr.next();
        Date retrievedate = St.getTime();
        String strDate = sdf.format(retrievedate);
        if(strDate.equals("Jan")) {
            janCounter++;
        }
        else if (strDate.equals("Feb")) {
            febCounter++;
        }
        else if (strDate.equals("Mar")) {
            marCounter++;
        }
        else if (strDate.equals("Apr")) {
            aprCounter++;
        }
        else if (strDate.equals("May")) {
            mayCounter++;
        }
        else if (strDate.equals("June")) {
            juneCounter++;
        }
        else if (strDate.equals("July")) {
            julyCounter++;
        }
        else if (strDate.equals("Aug")) {
            augCounter++;
        }
        else if (strDate.equals("Sept")) {
            septCounter++;
        }
        else if (strDate.equals("Oct")) {
            octCounter++;
        }
        else if (strDate.equals("Nov")) {
            novCounter++;
        }
        else if (strDate.equals("Dec")) {
            decCounter++;
        }
        numberOfPost.put("December", decCounter);
        numberOfPost.put("January", janCounter);
        numberOfPost.put("Feburary", febCounter);
        numberOfPost.put("March", marCounter);
        numberOfPost.put("April", aprCounter);
        numberOfPost.put("May", mayCounter);
        numberOfPost.put("June", juneCounter);
        numberOfPost.put("July", julyCounter);
        numberOfPost.put("August", augCounter);
        numberOfPost.put("September", septCounter);
        numberOfPost.put("October", octCounter);
        numberOfPost.put("November", novCounter);
    }
} catch (ParseException e) {
    // TODO Auto-generated catch block
    e.printStackTrace();
}
Denise Tay
  • 380
  • 3
  • 14

1 Answers1

1

There is no such thing in the standard. But I found one implementation:

linked_map

I couldn’t translate everything, but I think it will help

    cxxext::linked_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;

    try {
        auto STArray & = this->data;
        for (auto val : STArray) {
            STPost St = static_cast<STPost>(val);
            Date retrievedate = St.getTime();
            std::string strDate = sdf.format(retrievedate);
            if (strDate = "Jan") {
                janCounter++;
            }
            else if (strDate == "Feb") {
                febCounter++;
            }
            else if (strDate == "Mar") {
                marCounter++;
            }
            else if (strDate == "Apr") {
                aprCounter++;
            }
            else if (strDate == "May") {
                mayCounter++;
            }
            else if (strDate == "June") {
                juneCounter++;
            }
            else if (strDate == "July") {
                julyCounter++;
            }
            else if (strDate == "Aug") {
                augCounter++;
            }
            else if (strDate == "ept") {
                septCounter++;
            }
            else if (strDate == "Oct") {
                octCounter++;
            }
            else if (strDate == "Nov") {
                novCounter++;
            }
            else if (strDate == "Dec") {
                decCounter++;
            }
            numberOfPost.insert({ "December", decCounter });
            numberOfPost.insert({ "January", janCounter });
            numberOfPost.insert({ "Feburary", febCounter });
            numberOfPost.insert({ "March", marCounter });
            numberOfPost.insert({ "April", aprCounter });
            numberOfPost.insert({ "May", mayCounter });
            numberOfPost.insert({ "June", juneCounter });
            numberOfPost.insert({ "July", julyCounter });
            numberOfPost.insert({ "August", augCounter });
            numberOfPost.insert({ "eptember", septCounter });
            numberOfPost.insert({ "October", octCounter });
            numberOfPost.insert({ "November", novCounter });
        }
    }
    catch (const ParseException & e) {
        // TODO Auto-generated catch block
        e.what();
    }
  • Hello, thanks for the code. Could I ask what is the difference between linked_map and just normal map? Both seems to be the same to me? – Denise Tay Mar 22 '20 at 15:53
  • Hi, the main difference between "linked_map", "std::map" and "std::unordered_map" and is that "linked_map" maintains the insertion order of keys, the order in which keys are inserted into "linked_map". On the other hand, the "map" by default it sorts the keys in the increasing order, the "unordered_map" doesn't maintain any order or keys or values. In essence, "linked_map" is an analog of the "linked hashmap" of Java. – Argishti Ayvazyan Mar 23 '20 at 13:23
  • ok so I am assuming it is a library I need to import it into visual studio ? – Denise Tay Mar 23 '20 at 14:21
  • Thanks, I have downloaded the library and included into my project and it seem to print out from jan to dec which is what I wanted. – Denise Tay Mar 23 '20 at 14:34