Re: multimap help

From: Mike Wahler (mkwahler_at_mkwahler.net)
Date: 11/27/04


Date: Sat, 27 Nov 2004 19:22:00 GMT


"iwasinnihon" <iwasinnihon@hotmail.com> wrote in message
news:41a7f626$0$19042$3a2ecee9@news.csolutions.net...
> I was wondering if anyone new or had example code that uses a multimap (or
> just a map). I have been looking for some and haven't really been
> successful. If anyone knows where I can find one if you'd let me know I'd
> appreciate it.
>
> Thanks

Using a std::map to store counts of occurrences of
each character value in a string:

#include <iomanip>
#include <iostream>
#include <map>
#include <ostream>
#include <string>

void tally(std::map<char, std::string::size_type>&m, const std::string& s)
{
    m.clear();
    std::string::size_type i(0);
    std::string::size_type sz(s.size());

    for(i = 0; i < sz; ++i)
        ++m[s[i]];
}

void show(std::ostream& os, const std::map<char, std::string::size_type>& m)
{
    os << "char count\n";

    std::map<char, std::string::size_type>::const_iterator it(m.begin());
    std::map<char, std::string::size_type>::const_iterator en(m.end());

    while(it != en)
    {
        os << " " << it->first << " " << std::setw(5) << it->second <<
'\n';
        ++it;
    }
}

int main()
{
    std::string s("This is a string of characters");
    std::map<char, std::string::size_type> counts;

    std::cout << "string:\n" << s << "\n\n";
    tally(counts, s);
    show(std::cout, counts);

    return 0;
}

Output:

string:
This is a string of characters

char count
          5
  T 1
  a 3
  c 2
  e 1
  f 1
  g 1
  h 2
  i 3
  n 1
  o 1
  r 3
  s 4
  t 2

Book recommendation for C++ standard library:
www.josuttis.com/libbook

-Mike



Relevant Pages

  • Re: map/multimap/wildcards
    ... character string lookup in sorted order. ... This seems to cry out for a multimap, ... multimap won't help you there: the only difference between a map and a ...
    (microsoft.public.vc.stl)
  • Re: Read in & count characters from a text file
    ... I need to read a text file character by character, ... I found I had to convert the character to a string before it would save to the map.Ideally I would like to save as a character. ... if not found insert into map with value of 1. ... My problems seems to be I cannot "check the map" if the character exists and if it does exist how do I get at the value to increment it. ...
    (comp.lang.java.programmer)
  • Re: char[]
    ... char plus String equals String. ... way return key and tab key do not map to string. ... Character y = new Character; ...
    (comp.lang.java.programmer)
  • RfD: Escaped Strings version 4
    ... the S" string can only contain printable characters, ... the S" string cannot contain the '"' character, ... as an escape character for the entry of characters that cannot be ... \b BS (backspace, ASCII 8) ...
    (comp.lang.forth)
  • RfD: Escaped Strings version 4
    ... the S" string can only contain printable characters, ... the S" string cannot contain the '"' character, ... as an escape character for the entry of characters that cannot be ... \b BS (backspace, ASCII 8) ...
    (comp.lang.forth)