统计字符串中重复的字符个数并输出

//输出字符串各个字符的个数
/*
对重复的字符将其下标存放在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;

}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值