替換 Algrorithms

提高:: replace_all()

#include <iostream>
#include <string>
#include <boost/algorithm/string.hpp>

using namespace std;

int main()
{

    // String to replace characters in

    string str = "Darn you, Darn you to the 5th power!!!";

    // Replace "Darn" with "*CENSORED*"

    boost::replace_all(str, "Darn", "*CENSORED*");

    // Print str

    cout << str.c_str() << endl;

    // Waits for program to exit

    cin.get();

    return 0;
}

提高:: replace_first()

#include <iostream>
#include <string>
#include <boost/algorithm/string.hpp>

using namespace std;

int main()
{

    // String to replace characters in

    string str = "Darn you, Darn you to the 5th power!!!";

    // Replace the first instance of "Darn" with "*CENSORED*"

    boost::replace_first(str, "Darn", "*CENSORED*");

    // Print str

    cout << str.c_str() << endl;

    // Waits for program to exit

    cin.get();

    return 0;
}

boost_replace_last()

#include <iostream>
#include <string>
#include <boost/algorithm/string.hpp>

using namespace std;

int main()
{

    // String to replace characters in

    string str = "Darn you, Darn you to the 5th power!!!";

    // Replace the last instance of "Darn" with "*CENSORED*"

    boost::replace_last(str, "Darn", "*CENSORED*");

    // Print str

    cout << str.c_str() << endl;

    // Waits for program to exit

   cin.get();

   return 0;
}