Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 345157
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 12, 20262026-05-12T11:03:28+00:00 2026-05-12T11:03:28+00:00

This program stores pairs in a map, counting the number of times a word

  • 0

This program stores pairs in a map, counting the number of times a word occurs. The goal is to have the data sorted by number of occurences and output in value/string form. Obviously the normal map sorts by the string key, so I had to reverse it.

To do this I read in words, and increment their values appropriately in a map. Then I create a multimap and copy the pairs from the map into the multimap, but reversed. Then I iterate through the multimap, outputting the pairs. However, a runtime error occurs when I try to output the pairs and I’m not sure why.

Here is the code:

#include <map>
#include <string>
#include <iostream>
using namespace std;

int main()
{
    map<string, int> words;
    multimap<int, string> words2;

    string s;
    while (true) {
        cin >> s;
        if (s == "0") break;
        ++words[s];
    }

    map<string, int>::iterator p;
    for (p = words.begin(); p!=words.end(); ++p)
        words2.insert(make_pair(p->second, p->first));

    multimap<int, string>::iterator p2;
    for (p2 = words2.begin(); p2!=words2.end(); ++p2)
        cout << p->first << ": " << p->second << '\n';
}

Any help is appreciated.

P.S. I read in different places that multimap can have multiple occurences of a key (which is why I used it in the first place) and/or multiple values in a single key. Some clarification would be nice regarding which is true or whether both are true.

Also is there any type of copy algorithm for maps? I decided to just use a for loop for simplicity’s sake, and it would probably be fairly easy to write a custom copy, but I’m just wondering (for copying maps to other pair containers and copying to output.)

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-12T11:03:28+00:00Added an answer on May 12, 2026 at 11:03 am
     for (p2 = words2.begin(); p2!=words2.end(); ++p2)
            cout << p->first << ": " << p->second << '\n';
    

    Shouldn’t the p’s in your output statement be p2’s ?

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have this dictionary that stores pairs of two quiz scores and participants' ID.
This program takes the first number in a file and indicates how many numbers
The program I've written stores a large amount of data in dictionaries. Specifically, I'm
I'd like to write a Java program that takes arbitrary data and stores it
I've made a program which stores any number of objects of my type Project.
I have a program like this: it compiles files in memory and then executes
I'm working on a program that stores a vital data structure as an unstructured
This program is supposed to determine how many units are stored in the value
This program I use has it's own variables to set when you run it,
This program reads emails (really just a .txt file structured like an email) and

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.