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

  • Home
  • SEARCH
  • 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 6575477
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 25, 20262026-05-25T15:23:29+00:00 2026-05-25T15:23:29+00:00

My class is like this: class Outgoing { multimap<string,string> outgoing; public: void makeConnection(string key,

  • 0

My class is like this:

class Outgoing
{
    multimap<string,string> outgoing;

    public:
    void makeConnection(string key, string value)
    {
        outgoing.insert(pair<string,string>(key,value));
    }

    void iterate()
    {
        multimap<string, string>::iterator it;
        multimap<string, string>::iterator it2;
        pair<multimap<string,string>::iterator,multimap<string,string>::iterator> ret;
        for (it = outgoing.begin();it != outgoing.end();++it)
        {
            ret = outgoing.equal_range((*it));  ??????
            for (it2=ret.first; it2!=ret.second; ++it2)
            {
                ???????

             }
        }
    }
};

background:

I want to represent a graph which can have many nodes. The key won’t repeat but can have multiple values.

str1  ----> val1
str1  ----> val2
str2 -----> val3

I want to know how can I get number of values for a particular key? for e.g. in the above question , for str1 it will be 2?

As you can see , I tried to do something after some digging around but in vain.

What is wrong with my code?

thanks

EDIT ::: after templatetypedef’s comment, I edited the code to:

for (it = outgoing.begin();it != outgoing.end();++it)
{
    cout<< (*it).first << " "<<  outgoing.count((*it).first); 

}

I can get the count, but the key(“str1”) comes twice. So the answer I see is 2 2 1.

I would appreciate it very much, if somebody teaches me how to iterate in such a way I get only one key. BTW, thanks, templatetypedef

  • 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-25T15:23:30+00:00Added an answer on May 25, 2026 at 3:23 pm

    You can use the count function for this, which returns the number of entries in the multimap with the the given key. In your example, writing

    outgoing.count("str1")
    

    would produce the value 2.

    In C++, there is no way to iterate over just the unique keys in a multimap. If you want to iterate over just those keys, there are two options you might want to consider:

    1. You could change from using a multimap< string, string > to map<string, vector<string> >. That way, each key is unique, and you can easily determine how many values are associated with each key by just looking at the number of elements in the corresponding vector.

    2. You could have a top-level loop to iterate over all keys, then have an inner loop to skip duplicate keys.

    As an example of option 2, you might try something like this:

    for (multimap<string, string>::iterator itr = myMap.begin(); itr != myMap.end(); ) {
        /* ... process *itr ... */
    
        /* Now, go skip to the first entry with a new key. */
        multimap<string, string>::iterator curr = itr;
        while (itr != myMap.end() && itr->first == curr->first)
            ++itr;
    }
    

    Hope this helps!

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

Sidebar

Related Questions

I've got a class like this: [XmlRoot(channel)] public class Channel { [XmlElement(title)] public String
Given a declaration like this: class A { public: void Foo() const; }; What
Assume I have a class like this: public class Server { public static void
Let's say I have a class like this: public class Person { private String
Imagine we have a class like this: class Testee { public: void Func() private:
I have a class like this public abstract class BaseType<T> { public string Name
I have a class like this public class Definition { public virtual string Name
I have a class like this: public class Stretcher : Panel { public static
I have a class like this: public class myClass { public List<myOtherClass> anewlist =
Given a class like this: class Foo { public: Foo(int); Foo(const Foo&); Foo& operator=(int);

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.