Common usage of C + + STL replace () function

Source: Internet
Author: User

Replace algorithm:

The Replace function is included in the header file #include<string>.

generic algorithm replace replaces all values in the queue that are equal to the given value with another value, and the entire queue is scanned, that is, each version of the algorithm is

Execution ——— in linear time with an O (n) complexity.

That is, the execution of replace will traverse the entire queue bounded by the interval [frist,last] to replace the old_value with New_value.

The following describes the nine common methods used in the Replace () function:

Usage One:

Replaces the specified string with str from the starting position POS starting at the length Len character

Example code:

#include <iostream> #include <string>using namespace Std;int main () {string line= "qwer& &qwer& & && "; Line=line.replace (Line.find (" & "), 1," 1 ");//Replace the first & in line with 1 Cout<<line<<endl ; return 0;

Usage Two:

The character that replaces the iterator's starting position to the ending position with str

Example code:

#include <iostream> #include <string>using namespace Std;int main () {string line= "qwer& &qwer& & && "; Line=line.replace (Line.begin (), Line.begin () +6," 1 ");//Replace line 6 characters starting at begin position with 1 cout<<line <<endl;return 0;}


Usage Three:

Replaces a string from a specified position with a specified substring of substr (given starting position and length)

Example code:

#include <iostream> #include <string>using namespace Std;int main () {string line= "qwer& &qwer& & && "; string substr=" 012345 "; Line=line.replace (0,5,substr,substr.find (" 1 "), 3);// Replaces the character on line string 0 to 5 with the specified substring of substr (3 characters starting at ' 1 ' position) Cout<<line<<endl;return 0;}


Usage Four:

Replace a substring at a specified position with STR (the compiler may report a warning when string goes char*, it is not recommended)

Example code:

#include <iostream> #include <string>using namespace Std;int main () {string line= "qwer& &qwer& & && "; char* str=" 012345 "; Line=line.replace (0,5,STR);//Replace with str a string of length 5 starting at the specified position 0 cout<<line<< Endl;return 0;}

Usage Five:

Replaces a string from the specified iterator position with STR (the compiler may report a warning when string char* is not recommended)

Example code:

#include <iostream> #include <string>using namespace Std;int main () {string line= "qwer& &qwer& & && "; char* str=" 012345 "; Line=line.replace (Line.begin (), Line.begin () +9,str);cout<<line<< Endl;return 0;}


Usage VI:

Replace the first n characters of s with a string from the start position of the POS length len (the compiler may report a warning when string char* is not recommended)

Example code:

#include <iostream> #include <string>using namespace Std;int main () {string line= "qwer& &qwer& & && "; char* str=" 012345 "; Line=line.replace (0,9,str,5);//Replace the string with a length of 9 from 0 position with the first 5 characters of Str cout<<line <<endl;return 0;}

Usage VII:

Replaces a string of the specified iterator position (from I1 to I2) with the first n characters of S (the compiler may report a warning when string goes to char*, which is not recommended)

Example code:

#include <iostream> #include <string>using namespace Std;int main () {string line= "qwer& &qwer& & && "; char* str=" 012345 "Line=line.replace (Line.begin (), Line.begin () +9,str,5);// Replaces the string of the specified iterator position with the first 5 characters of Str  Cout<<line<<endl;return 0;}


Usage Eight:

Replaces the contents of the POS length len from the specified position with C characters repeating n times

Example code:

#include <iostream> #include <string>using namespace Std;int main () {string line= "qwer& &qwer& & && "; char c= ' 1 '; Line=line.replace (0,9,3,c);//Replace the contents of the 0-length 9 with a repeat 3 times of the C character cout<<line<<endl; return 0;}


Usage Nine:

Replaces the contents of the specified iterator position (starting from I1 to the end) with a C character that repeats n times

Example code:

#include <iostream> #include <string>using namespace Std;int main () {string line= "qwer& &qwer& & && "; char c= ' 1 '; Line=line.replace (Line.begin (), Line.begin () +9,3,c);//Replace the content from the specified iterator with a C character that repeats 3 times cout< <line<<endl;return 0;}


Note:

Parameters that use iterator types are not limited to string types, but can be other types of iterators, such as vectors, lists, and so on.

Copyright NOTICE: This article for Bo Master original article, without Bo Master permission not reproduced.

Common usage of C + + STL replace () function

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.