Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

What is the use of the replace () function in C++

2025-01-28 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)05/31 Report--

This article mainly explains the "what is the use of the replace () function in C++". The content in the article is simple and clear, and it is easy to learn and understand. Please follow the editor's train of thought to study and learn "what is the use of the replace () function in C++?"

There are a variety of string applications in C++ programming language, each of which can help us to achieve specific functional requirements. Here we will give you a detailed introduction to one of the more important uses, about the application of C++ replace () function.

Basic_string::max_size

The C++ replace () function returns the number of * * elements that string can put. (different from capacity)

Size_type max_size () const; basic_string:: size_type cap, max; cap = s.capacity (); max= s.max_size (); / / max=4294967294. Basic_string::rfind

Look for a given string. Returns * string subscript values found; if not found, npos is returned.

Unlike find, rfind starts looking from npos by default. Everything else is the same.

Basic_string::replace

Replace the element or substring in the original string. Returns the replaced string.

(1) replace the _ Num1 characters starting with _ Pos1 in the operation string with string or C-string

Basic _ string& replace (size _ type _ Pos1

Size _ type _ Num1, const value _ type* _ Ptr)

Basic _ string& replace (size _ type _ Pos1

Size _ type _ Num1, const basic _ string _ Str)

String a,b

String s ("AAAAAAAA")

String s1p ("BBB")

Const char* cs1p = "CCC"

A = s.replace (1,3, s1p); / / s = "ABBBAAAA"

B = s.replace (5,3, cs1p); / / s = "ABBBACCC"

(2) replace the _ Num2 characters starting with _ Pos2 in the C++ replace () function with the _ Num1 characters starting from _ Pos1 in the operation string

Use _ Num2 characters in C-string instead of _ Num1 characters starting with _ Pos1 in operation string

Basic _ string& replace (size _ type _ Pos1

Size _ type _ Num1, const basic _ string& _ Str

Size _ type _ Pos2, size _ type)

Basic _ string& replace (size _ type _ Pos1, size _ type _ Num1

Const value _ type* _ Ptr, size _ type _ Num2)

String a, b

String s ("AAAAAAAA")

String S2P ("BBB")

Const char* cs2p = "CCC"

A = s.replace (1,3, s2p, 1,2); / / s = "ABBAAAA"

B = s.replace (4,3, cs2p, 1); / / s = "ABBAC"

(3) use _ Count character _ Ch instead of _ Num1 characters starting with _ Pos1 in the operation string

Basic _ string& replace (size _ type _ Pos1, size _ type _ Num1, size _ type _ Count, value _ type _ Ch); string result; string s ("AAAAAAAA"); char ch = 'compressed; result = s.replace (1,3,4, ch); / / s = "ACCCCAAAA"

(4) use string or C-string instead of operating characters from First0 to Last0 in string

Basic _ string&replace (iterator First0, iterator Last0

Const basic _ string& _ Str)

Basic _ string&replace (iterator First0, iterator _ Last0

Const value _ type* _ Ptr)

String s ("AAAAAAAA"); string s4p ("BBB")

Const char* cs4p = "CCC"

Basic_string::iterator IterF0, IterL0

IterF0 = s.begin (); IterL0 = s.begin () + 3

String a, b

A = s.replace (IterF0, IterL0, s4p); / / s = "BBBAAAAA"

B = s.replace (IterF0, IterL0, cs4p); / / s = "CCCAAAAA"

(5) replace the characters from First0 to Last0 in the operation string with the _ Num2 characters starting from _ Pos2 in the C++ replace () function.

Replace the characters from First0 to Last0 in the operation string with the _ Num2 characters in C-string

Basic _ string& replace (iterator _ First0, iterator _ Last0, const value _ type* _ Ptr, size _ type _ Num2); template basic _ string& replace (iterator _ First0, iterator _ Last0, InputIterator _ First, InputIterator _ Last); IterF3 = s.begin () + 1; IterL3 = s.begin () + 3; IterF4 = s.begin (); IterL4 = s.begin () + 2; a = s.replace (IterF3, IterL3, IterF4, IterL4) B = s.replace (IterF1, IterL1, cs5p, 4)

(6) replace the characters from First0 to Last0 in the operation string with _ Count character _ Ch

Basic_string & replace (iterator _ First0, iterator _ Last0, size _ type _ Count, value _ type _ Ch); a = s.replace (IterF2, IterL2, 4, ch); basic_string::swap

Exchange two string.

Void swap (basic_string & _ Str); s1.swap (S2); basic_string::substr

Returns a string of _ Count characters starting with _ Off (subscript)

Basic _ string substr (size _ type _ Off = 0

Size _ type _ Count = npos) const

String s ("I love you!"), sub

Ssub=s.substr (); / / sub= "I love you!"

Ssub=s.substr (1); / / sub= "love you!"

Ssub=s.substr (3 ove 4); / / sub= "ove"

Thank you for your reading, the above is the content of "what is the use of the replace () function in C++". After the study of this article, I believe you have a deeper understanding of the use of the replace () function in C++, and the specific use still needs to be verified in practice. Here is, the editor will push for you more related knowledge points of the article, welcome to follow!

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report