一个C++编程问题求解

来源:百度知道 编辑:UC知道 时间:2024/06/09 11:35:04
在屏幕上输入一串字符,输出各个字符的个数,其中连续空格算一个空格

试了一下,要用大众化的方法来实现还真不容易,如果要比较快的实现的话,就只好用一些库算法什么的,也可能用到容器,所以不知道你能否接受这样的?

随便弄了一个,你看看是你要的吗?

#include <iostream>
#include <string>
#include <set>
using namespace std;

class CNode
{
private:
char ch;
int num;
public:
CNode( char c,int n = 1 ):ch(c),num(n){}
void addNum(){ ++num; }
friend ostream& operator << ( ostream& os,const CNode& cn );
friend bool operator == ( const CNode& lhs,const CNode& rhs );
friend bool operator < ( const CNode& lhs,const CNode& rhs );
};
ostream& operator << ( ostream& os,const CNode& cn )
{
os<< cn.ch<<":"<<cn.num<<endl;
return os;
}
bool operator == ( const CNode& lhs,const CNode& rhs )
{
return lhs.ch == rhs.ch;
}
bool operator < ( const CNode& lhs,const CNode& rhs )
{
return lhs.ch < rhs.ch;
}
void comput