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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 13, 20262026-05-13T02:07:52+00:00 2026-05-13T02:07:52+00:00

In one of my programs for school, I use the following function to count

  • 0

In one of my programs for school, I use the following function to count the frequency of identifiers in a string, separated by newlines and #:

Input:

dog
cat
mouse
#
rabbit
snake
#

Function:

//assume I have the proper includes, and am using namespace std
vector< pair<string,int> > getFreqcounts(string input) {
    vector<string> items = splitString(input,"\n");
    map<string,int> counts;

    for (int i=0; i<items.size(); i++) {
        if (items[i] == "#") continue;
        counts[items[i]] = 0;
    }
    for (int i=0; i<items.size(); i++) {
        if (items[i] == "#") continue;
        counts[items[i]]++;
    }

    return vector< pair<string,int> > (counts.begin(),counts.end());
}

I would like to at the very least

  • remove the double for loop
  • find a better way to get a vector< pair<string,int> >

Any ideas?

BTW, this is NOT homework. The real homework will use this function, but this is purely out of my own curiosity and desire to have “better” code.

  • 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-13T02:07:53+00:00Added an answer on May 13, 2026 at 2:07 am

    You can get rid of the first for loop by simply deleting it. It accomplishes nothing useful. When/if the subscript into the map creates a new item, that item will have the chosen key, and your associated int will be initialized to zero automatically.

    Personally, I’d probably do things a bit differently, using a stringstream instead of your SplitString(). I’m hesitant about posting code, but I guess I’ll trust you…

    typedef vector<pair<string, int> > count_vec;
    
    count_vec GetFreqCounts(string const &input) { 
        istringstream in(input);
        string line;
        map<string, int> counts;
    
        while (getline(in, line))
            if (line != "#")
                ++counts[line];
        return count_vec(counts.begin(), counts.end());
    }
    

    Edit: I honestly didn’t pay a whole lot of attention to efficiency as I was writing this, but I think Steve Jessop’s comment on it is pretty accurate. As long as the input is small, it won’t make any real difference. If the input is really big, the fact that this only uses an extra copy of one word at a time could save enough memory to be meaningful.

    The solution Steve gave in his reply looks pretty nice too though. Since it also processes words as they’re produced, I’d expect it to have characteristics similar to the code above. If you can break the string into words faster than stringstream does, it’ll undoubtedly be faster. Given the number of virtual functions that get in the way with iostreams, there’s a pretty good chance of that — but unless you’re dealing with a lot of text there’s not much chance of it making a significant difference. Of course, exactly what qualifies as significant is open to question. To put it in perspective, I ran some similar code across a word list I had handy. Using code pretty close to what’s above, it processes text at a little over 10 megabytes a second.

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

Sidebar

Related Questions

I'm doing a System.Text.Encoding.UTF8.GetBytes(s) on a string in two different programs (one console, one
One of our programs is sometimes getting an OutOfMemory error on one user's machine,
one of the programs I'm developing at work is reading the registry to figure
One of our programs is erroring out because a stored procedure was created with
Greetings, currently I am refactoring one of my programs, and I found an interesting
One thing that annoys me when debugging programs in Visual Studio (2005 in my
I have seen many programs consisting of structures like the one below typedef struct
Just wondering what little scripts/programs people here have written that helps one with his
I've been looking into different web statistics programs for my site, and one promising
I write a lot of short throwaway programs, and one of the things I

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.