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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 25, 20262026-05-25T21:35:34+00:00 2026-05-25T21:35:34+00:00

For example, count the occurrence the words in a book, I saw somebody simply

  • 0

For example, count the occurrence the words in a book, I saw somebody simply wrote:

map<string, int> count;
string s;
while (cin >> s) count[s]++;

Is this the correct way of doing so? I tested on my machine and seems so. But is the initialization to zero guaranteed? If it is not, I would imagine a code like this:

map<string, int> count;
string s;
while (cin >> s)
    if (count.find(s) != count.end())  count[s]++;
    else count[s] = 1;
  • 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-25T21:35:34+00:00Added an answer on May 25, 2026 at 9:35 pm

    Yes, operator[] on a std::map will initialize the value with T(), which in the case of int, is zero.

    This is documented on section 23.4.4.3 of the C++ standard:

    T& operator[](const key_type& x);
    

    Effects: If there is no key equivalent to x in the map, inserts
    value_type(x, T()) into the map.

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

Sidebar

Related Questions

Example: select count(*) from my table where column1 is not null and (column1 =
How to 'group by' a query using an alias, for example: select count(*), (select
For example: declare @bitHaveRows bit select @bitHaveRows = count(*) from table where (predicate) Are
I can't think of an example where the reference count of a file may
For example, if the data in one worksheet looks like: UsedRange.Columns.Count is 6 which
for example I have collection foo of documents like that: {tag_cloud:[{value:games, count:10}, {value:girls, count:500}]}
How can we count space between text in PHP? example: hi how are you?
For example, if we have a table Books, how would we count total number
How do you use cakephp to count, for example the number of posts, made
How can I integrate word count into bassistance.de validation? For example, i dont want

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.