Re: multimap help
From: Mike Wahler (mkwahler_at_mkwahler.net)
Date: 11/27/04
- Next message: B. v Ingen Schenau: "Re: multimap help"
- Previous message: B. v Ingen Schenau: "Re: alternatives to indent for c++"
- In reply to: iwasinnihon: "multimap help"
- Next in thread: B. v Ingen Schenau: "Re: multimap help"
- Messages sorted by: [ date ] [ thread ] [ subject ] [ author ]
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
- Next message: B. v Ingen Schenau: "Re: multimap help"
- Previous message: B. v Ingen Schenau: "Re: alternatives to indent for c++"
- In reply to: iwasinnihon: "multimap help"
- Next in thread: B. v Ingen Schenau: "Re: multimap help"
- Messages sorted by: [ date ] [ thread ] [ subject ] [ author ]
Relevant Pages
|
|