9.41
int main() {
vector<char > cVec = {'a','b','c','d'};
string str(cVec.begin(),cVec.end());
cout << str << endl;
for (;;);
return 0;
}
9.42
提前预分配至少100个元素大小给vector
9.43
void stringChangeIter(string& s, const string& oldVal, const string& newVal){
for(string::iterator it = s.begin(); it != s.end(); ){
string theCheckString(it, it+ oldVal.size());
if(theCheckString == oldVal){
it = s.erase(it,it+oldVal.size());
it = s.insert(it,newVal.begin(),newVal.end());
it += newVal.size();
}else{
it++;
}
}
}
9.44
void stringChangePos(string& s, const string& oldVal, const string& newVal) {
for(size_t i = 0 ; i <s.size();){
if((s.size() - i) < oldVal.size()){
break;
}
string theCheckString(s,i,oldVal.size());
if(theCheckString == oldVal){
s.replace(i,oldVal.size(),newVal);
}else{
i++;
}
}
}
9.45
string& stringChangeAddPrefixAndSuffixIter(string& s, const string& prefix , const string& suffix){
s.insert(s.begin(), prefix.begin(), prefix.end());
s.append(suffix);
return s;
}
9.46
string& stringChangeAddPrefixAndSuffixPos(string& s, const string& prefix, const string& suffix) {
s.insert(0, prefix);
s.insert(s.size(), suffix);
return s;
}
9.47int main() {
string str("ab2c3d7R4E6");
int pos = 0;
while((pos = str.find_first_of("0123456789", pos)) != string::npos){
cout << str[pos] << endl;
++pos;
}
pos = 0;
while ((pos = str.find_first_not_of("0123456789", pos)) != string::npos) {
cout << str[pos] << endl;
++pos;
}
for (;;);
return 0;
}
9.48
猜测返回查询失败消息
结果为-1,猜测正确
9.49
string stringCheckForNotAscenderAndDescender(const string & str){
string theStr(str);
string ascender("df");
string descender("pg");
int pos = 0;
while((pos = theStr.find_first_of(ascender)) != string::npos||
(pos = theStr.find_first_of(descender)) != string::npos){
theStr.erase(pos,1);
++pos;
}
return theStr;
}