C + + string Find_first of implementation

Source: Internet
Author: User

One: Before the implementation of the first find_first_of sister function

(1) find_first_of (string &str, Size_type index = 0):

Finds the first character in a string that matches a character in Str and returns its position. search starts from index forward and returns String::npos if not found

(2) find_first_not_of (CString &str, Size_type index = 0):

Finds the first character in a string that does not match the character in Str and returns its position. The search starts from index forward . If you don't find it, return to String::nops.

(3) find_last_of (CString &str, Size_type index = 0):

Finds the last character in a string that matches a character in Str and returns its position. search starts at the reverse of index . If you don't find it, return to String::nops.

(4) find_last_not_of (CString &str, Size_type index = 0):

Finds the last character in a string that does not match the character in Str and returns its position. search starts at the reverse of index . If you don't find it, return to String::nops.

(5) Total (3) and (4) lookup from the reverse of index, also equivalent to (1) and (2) in the first

Two: Manually implement the Find_first_of () function

(1) int mystring::find_first_of (const char* str,size_t index)

int mystring::find_first_of (const char* str,size_t index)    {        int tmp_len = strlen (str), J;        size_t flag,mymin = Ini_max;        for (j=0;j<tmp_len;j++)        {            flag =-1;            for (size_t i=index;i<strlength;i++)            {                if (str[j] = = P_str[i])                {                    flag = i;                    break;                }            }            indexs[j] = flag;            if (flag! =-1)            {                mymin = mymin<flag?mymin:flag;            }        } For        (j=0;j<tmp_len;j++)//        {//            if (indexs[j]!=-1)//                min = min<indexs[j]?min:indexs[j] ;//        }        if (mymin = = Ini_max)        {            return-1;//            mymin = -1;//           cout << "----1----" < < mymin << ",,,,";        }        return mymin;    }

(2) int mystring::find_first_of (const char ch,size_t index)

int mystring::find_first_of (const char ch,size_t index)    {        int J;        size_t flag =-1;        for (size_t i=index;i<strlength;i++)        {            if (ch = = P_str[i])            {                flag = i;                break;            }        }        return flag;    }
(3) int mystring::find_first_of (const mystring& str,size_t index)

   int mystring::find_first_of (const mystring& str,size_t index)    {        int J;        size_t flag,mymin = Ini_max;        for (j=0;j<str.strlength;j++)        {            flag =-1;            for (size_t i=index;i<strlength;i++)            {                if (str[j] = = P_str[i])                {                    flag = i;                    break;                }            }            if (flag! =-1)            {                mymin = mymin<flag?mymin:flag;            }        }        if (mymin = = Ini_max)        {            return-1;        }        return mymin;    }


Three: Look back at the run example of the string class itself

(1) First look at the results, and then to the code


(2) Main.cpp code

#include <string> #include <iostream>using namespace Std;int main () {string Strfir ("ABCEDCFCA"), Strsec ("    ABC abc abddef ");    cout << "1:" << strfir.find ("a") << Endl; Finds the first character in a string that matches a character in Str and returns its position.    Search from index forward, if not found return string::npos cout << "2:" << strfir.find_first_of ("DCE", 0) << endl;//any one character Finds the first character in a string that does not match the character in Str and returns its position. The search starts from index forward.    If not found, return to String::nops cout << "3:" << strfir.find_first_not_of ("AC", 2) << Endl; Finds the last character in a string that matches a character in Str and returns its position. Search starts at the reverse of index.    If not found, return to String::nops cout << "4:" << strfir << strfir.find_last_of ("Ca", 4) << Endl;    cout << "4:" << strfir << strfir.find_last_of ("Ca", 9) << Endl; Finds the last character in a string that does not match the character in Str and returns its position. The search starts at index.    If not found, return to String::nops cout << "3:" << strfir << strfir.find_last_not_of ("AC") << Endl; return 0;}
Four: Sentiment

(1) Student age can not be wasted like this, need to learn to learn, self-study;

(2) Weekly understanding of a type of STL, do not too much, analysis and understanding of the implementation of an STL container, estimated to take at least two days;

(3) Every week to do the experiment must be advanced, you can not write a word to the present ~ ~ ~ ~ Hands must have the ability to do.


C + + string Find_first of implementation

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.