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 8057813
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 5, 20262026-06-05T09:09:15+00:00 2026-06-05T09:09:15+00:00

I am trying to use the std::map template, but I haven’t been able to

  • 0

I am trying to use the std::map template, but I haven’t been able to get it working. From research on the internet I’ve come to this solution and redirecting the a file to the input stream, here is the code:

typedef map<char*, int> wc;
int main() {
    int c;
    char cc[75], nombre[75];
    wc m;
    scanf("%d", &c);
    while (c--) {
        scanf("%s %[ a-zA-Z]", cc, nombre);
        ++m[cc]; // This should work
        printmap(m);
    }
}

Print map is a function that just prints the map object. Here is my file input.txt

3
Spain Donna Elvira
England Jane Doe
Spain Donna Anna

When I execute the program, the ouput is:

Spain -> 1
England -> 2
Spain -> 1

What I expect is:

Spain -> 2
England -> 1

The number of occurrences of the Country mapped to the number of times it appears

  • 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-06-05T09:09:17+00:00Added an answer on June 5, 2026 at 9:09 am

    As a solution to my comment above, the C++ version of the code you presented:

    typedef map<std::string, int> wc;
    int main() {
        int c;
        string cc, nombre;
        wc m;
        std::cin >> c;
        while (c--) {
            std::cin >> cc;
            std::getline(std::cin, nombre);
            m[cc] += 1; // This should work
        }
        printmap(m);
    }
    

    http://ideone.com/2JP82

    First: std::map sorts it’s data based on the key, in your code, char*, which points at char cc[75]. So when you replaced the text in cc, then the keys of the map changed, and that breaks everything. The keys of a map must not change ever. Since we’re using C++, you should not use char[] at all; use std::string instead, which (since it is a “value type”) will make everything just magically work. I have no idea how it was working before, since you don’t show the printmap function.

    Second: You call printmap each and every time you read a single line, and since a map has no way of printing “the last thing added”, that idea makes no sense at all. The printmap call should probably print the entire map, and be outside the loop.

    Third: Don’t use scanf, it’s not safe. Use streams: std::cin >> cc for reading in a single word, or std::getline(std::cin, nombre) for reading in what’s left on the line. That way the code won’t crash if someone enters the line (sources for longest country name and longest last name)

    Krungthepmahanakornamornratanakosinmahintarayuttha-yamahadilokphopnopparatrajathaniburiromudomrajaniw-esmahasatharnamornphimarnavatarnsathitsakkattiyavi-sanukamprasit Wolfeschlegelsteinhausenbergerdorffvortschaferswesenchafewarenwholgepflegeunzenvonangereifenduchihrraubgiriigfeindtausendjahresvorandieerscheinenbanderechiffgebrauchlichtalsseinursprungvonkrrthinzwischensternartigraumaufdersuchebtbewohnbarplanetenkreisedrehensichundstandigmenshlichkeittkonntevortpflanzeslamdlichfreudeundruhemitnichteinfurchrintlligentgeschopfsvonhinzwischenster
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm trying to use std::string instead of char* whenever possible, but I worry I
I'm trying to use templates to get std:list of items, where each item has
I'm trying to use an associative_property_map to include index for vertices, but I get
I'm trying to use openmp to multithread a loop through std::set. When I write
I was trying use svn to find a checkin using svn log, but it
I'm trying use mod_rewrite to rewrite URLs from the following: http://www.site.com/one-two-file.php to http://www.site.com/one/two/file.php The
I'm trying to map some structs to some other instances, like this: template <typename
I have a struct with a std::map of pointers inside it. I'm trying to
Can I use following syntax: std::map<int,std::list<int>> mAllData; Where Key Value(int) will be ID of
I am trying to use the map container in C++ in the following way:

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.