may be you can change the string type to the bitset type and then you could transfer the operation of the bitset type ,just like that:
size_t num=bitvec.cout()//the bitvec is a object of bitset type and the number means that the 1's units;
another way :you can make a use of the iterator to count the 1's units in you string ,for exemple :
#include<iostream>
#include<string>
#include<cstddef>
using namespace std;
int main(){
size_t num0=0,num1=0;
string str("0001111000");
string::iterator iter;
for(iter=str.begin();iter!=str.end();++iter){
if(*iter!=' '){
if(*iter=='1')
num1++;
if(*iter=='0')
num0++;
}
else cout<<"errer!"<<endl;
}
cout<<num1<<endl;
cout<<num0<<endl;
return 0;
}
if you have some problems please show me and give me a reason ,thank you.
by the way ,i see ,i can't make sure that i understand you real idea.
如果你对我报有仇恨,我将背负它,接受它!
我不知道风在向哪一个方向吹,我知道,你有你的,我有我的方向.