current position:Home>[fun bath] C + + tips -- knowledge points: find() function & find_ first_ Of() function & similar to indexof() function in Java

[fun bath] C + + tips -- knowledge points: find() function & find_ first_ Of() function & similar to indexof() function in Java

2022-01-26 23:37:09 Playful bass

Welcome to my WeChat official account. :
Programming Pastor

ID:
bianchengzhizhen
Timely sharing algorithm 、 Computer science and game programming

I am CSDN Blog home page :
https://blog.csdn.net/D16100?spm=1000.2115.3001.5343&type=blog
Welcome to exchange and study

Be similar to Java Medium IndexOf function :

Here is an introduction indexOf() Two usages and functions of :

1. indexOf(String str):

Returns the specified character str In the string ( Method caller ) The starting index at the first occurrence , If there is no such character in this string , Then return to -1.

2.indexOf(String str, int index):

Return from index The position starts to find the specified character str The starting index at the first occurrence in the string , If there is no such character in this string , Then return to -1.

C++ in :

find() function

The function structure :

The function prototype :
size_t find ( const string& str, size_t pos = 0 ) const;
size_t find ( const char* s, size_t pos, size_t n ) const;
size_t find ( const char* s, size_t pos = 0 ) const;
size_t find ( char c, size_t pos = 0 ) const;

Parameter description :
pos Find the starting position
n The first... Of the string to be found n Characters

Specific examples :

Use samples :
string str1(“tan wan ba si”);
string str2(“tan”);

Two strings are defined above ;

str1.find(str2);
// From the string str1 When looking in str2, return str2 First character in str1 Address in

str1.find(str2, 5);
// from str1 Of the 5 Character to start looking up str2

str1.find(“ba”);
// If ba stay str1 Look for to , return b stay str1 Position in

str1.find(“a”);
// To find the character o And return the address

str1.find(“ba si”,2,2);
// from str1 Start looking for the second character in ba si The first two characters of

find_first_of() function ( This function looks for the position of any character in the string )

The function structure

The function prototype :

size_t find_first_of ( const string& str, size_t pos = 0 ) const;
size_t find_first_of ( const char* s, size_t pos, size_t n ) const;
size_t find_first_of ( const char* s, size_t pos = 0 ) const;
size_t find_first_of ( char c, size_t pos = 0 ) const;

pos Find the starting position
n The first... Of the string to be found n Characters

Be careful :
find_first_of The most error prone parts of functions are and find Function confusion . The biggest difference is if it's in a string str1 Find another string in str2, If str1 contains str2 Any character in , Then the search will be successful , and find Is different ;

such as :

string str1(“I am change”);
string str2(“about”);

int k=str1.find_first_of(str2);
//k The value returned is about this 5 The first time any one of the characters is in str1 Where in ;

copyright notice
author[Playful bass],Please bring the original link to reprint, thank you.
https://en.cdmana.com/2022/01/202201262337062458.html

Random recommended