How to use C++ STL iterator in string class

//The following example illustrates the use of iterators in the string class
#include <iostream>
#include <string>
#include <algorithm>
using namespace std;

intmain()
{
    string s("Hello World! I love programming!");
    cout<<s<<endl;
    //initialize sd with s
    string sd(s.begin(),s.end());
    cout<<sd<<endl;
    // Convert all the contents of sd to uppercase, ::toupper means to use the toupper function in the global
    transform(sd.begin(),sd.end(),sd.begin(),::toupper);
    cout<<sd<<endl;
    string sd1;
    //Append part of sd to sd1
    sd1.append(sd.begin(),sd.end()-7);
    cout<<sd1<<endl;
    string sd2;
    string::reverse_iterator iterA;
    string temp="0";
    // Traverse sd in reverse order and append the content of sd to sd2
    for (iterA = sd.rbegin (); iterA! = sd.rend (); iterA ++) {
        temp=*iterA;
        sd2.append(temp);
    }
    cout<<sd2<<endl;
    //Remove sd2 0 to 15 bits
    sd2.erase(0,15);
    cout<<sd2<<endl;
    string::iterator iterB=sd2.begin();
    string sd3=string("12345678");
    //insert sd3
    sd2.insert(iterB,sd3.begin(),sd3.end());
    cout<<sd2<<endl;
    //replace string
    sd2.replace(sd2.begin(),sd2.end(),"This is an Example of Replace!");
    cout<<sd2<<endl;
    return 0;
}

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=325555097&siteId=291194637