使用迭代器将string对象中的字符都改为大写字母
#include <iostream>
#include <string>
#include <cctype>
using namespace std;
int main()
{
string str="This is a example";
for(string::iterator iter=str.begin();iter!=str.end(); ++iter)
{
*iter=toupper(*iter);//将字符转换为对应的大写字母
}
//输出查看结果
cout<<str<<endl;
return 0;
}
使用迭代器寻找和删除string对象中所有的大写字符
#include <iostream>
#include <string>
#include <cctype>
using namespace std;
int main()
{
string str="This IS A example";
for(string::iterator iter=str.begin();iter!=str.end(); ++iter)
{
if(isupper(*iter))
{
str.erase(iter);
--iter;
}
}
//输出查看结果
cout<<str<<endl;
return 0;
}
#include <iostream>
#include <string>
#include <cctype>
using namespace std;
int main()
{
string q1("When lilacs last in the dooryard bloom'd");
string q2("The child is father of the man");
string sentence;
//将sentence赋值为"The child is "
sentence.assign(q2.begin(),q2.begin()+13);
//在sentence末尾添加"in the dooryard"
sentence.append(q1.substr(q1.find("in"), 15));
//输出查看结果
cout<<sentence<<endl;
return 0;
}