//输出字符串各个字符的个数
/*
对重复的字符将其下标存放在vector中,使用unique函数只保存一份重复字符的数字
通过下标查找到相应的字符,从map中取出对应的统计数字
*/
include “iostream”
include “windows.h”
include “string”
include “map”
include “vector”
include “algorithm”
include “iterator”
using namespace std;
int main()
{
vector<int> coll,coll1;
string str;
int len;
int nn;
char word;
cin>>str;
int index;
map<char,int> counts;
len=str.length();
int i;
vector<int>::iterator pos;
for(i=0; i<len; i++)
{
word=str.at(i);
++counts[word];//将输入的字符串中的字符挨个输入到map容器
}
for(i=0; i<len; i++)
{
char cha=str.at(i);
index=str.find(cha);
map<char,int>::iterator num(counts.find(cha));//开始统计计数
if(num->second>1)
{
coll.push_back(index);
}
}
sort(coll.begin(),coll.end());
pos=unique(coll.begin(),coll.end());
//copy(coll.begin(),pos,ostream_iterator<int>(cout, " "));
copy(coll.begin(),pos,back_inserter(coll1));
//copy(coll1.begin(),coll1.end(),ostream_iterator<int>(cout," "));
for(i=0; i<coll1.size(); i++)
{
char ch=str.at(coll.at(i));
nn=str.find(ch);
map<char,int>::iterator num1(counts.find(ch));
cout<<num1->first<<" ";
cout<<num1->second<<endl;
}
cout<<endl;
system("pause");
return 0;
}