How std::find_last_of actually works? Announcing the arrival of Valued Associate #679: Cesar Manara Planned maintenance scheduled April 17/18, 2019 at 00:00UTC (8:00pm US/Eastern) Data science time! April 2019 and salary with experience The Ask Question Wizard is Live!How do I iterate over the words of a string?How do I read / convert an InputStream into a String in Java?Case insensitive 'Contains(string)'Convert bytes to a string?How do I make the first letter of a string uppercase in JavaScript?How to replace all occurrences of a string in JavaScriptWhy is “using namespace std” considered bad practice?How to check whether a string contains a substring in JavaScript?Why should C++ programmers minimize use of 'new'?How to remove the last character from a string?

Does classifying an integer as a discrete log require it be part of a multiplicative group?

Trademark violation for app?

Is the Standard Deduction better than Itemized when both are the same amount?

Is safe to use va_start macro with this as parameter?

Closed form of recurrent arithmetic series summation

How can I use the Python library networkx from Mathematica?

Is it a good idea to use CNN to classify 1D signal?

What do you call the main part of a joke?

Is there any way for the UK Prime Minister to make a motion directly dependent on Government confidence?

Is "Reachable Object" really an NP-complete problem?

What does this Jacques Hadamard quote mean?

Why are both D and D# fitting into my E minor key?

Crossing US/Canada Border for less than 24 hours

What are the out-of-universe reasons for the references to Toby Maguire-era Spider-Man in ITSV

Why didn't Eitri join the fight?

Is it cost-effective to upgrade an old-ish Giant Escape R3 commuter bike with entry-level branded parts (wheels, drivetrain)?

Significance of Cersei's obsession with elephants?

What causes the direction of lightning flashes?

Irreducible of finite Krull dimension implies quasi-compact?

How to react to hostile behavior from a senior developer?

Has negative voting ever been officially implemented in elections, or seriously proposed, or even studied?

What's the meaning of "fortified infraction restraint"?

Why are there no cargo aircraft with "flying wing" design?

How do I make this wiring inside cabinet safer? (Pic)



How std::find_last_of actually works?



Announcing the arrival of Valued Associate #679: Cesar Manara
Planned maintenance scheduled April 17/18, 2019 at 00:00UTC (8:00pm US/Eastern)
Data science time! April 2019 and salary with experience
The Ask Question Wizard is Live!How do I iterate over the words of a string?How do I read / convert an InputStream into a String in Java?Case insensitive 'Contains(string)'Convert bytes to a string?How do I make the first letter of a string uppercase in JavaScript?How to replace all occurrences of a string in JavaScriptWhy is “using namespace std” considered bad practice?How to check whether a string contains a substring in JavaScript?Why should C++ programmers minimize use of 'new'?How to remove the last character from a string?



.everyoneloves__top-leaderboard:empty,.everyoneloves__mid-leaderboard:empty,.everyoneloves__bot-mid-leaderboard:empty height:90px;width:728px;box-sizing:border-box;








3















string s = "I Like C++ Tutorial";
cout << s.find_last_of("Like");


I know that find_last_of returns the last character that matches, however, it returns 16, which is the position of the letter i in Tutorial, But I'm confused because I'm searching for the last position of Like not i, I tried to remove i from the string. It returns 5 as I expected. But the question is why did it return 16?










share|improve this question

















  • 7





    read the docs to see why find_last_of behaves that way, you want rfind if you want to match a substring in a string from the end

    – EdChum
    Mar 22 at 9:36


















3















string s = "I Like C++ Tutorial";
cout << s.find_last_of("Like");


I know that find_last_of returns the last character that matches, however, it returns 16, which is the position of the letter i in Tutorial, But I'm confused because I'm searching for the last position of Like not i, I tried to remove i from the string. It returns 5 as I expected. But the question is why did it return 16?










share|improve this question

















  • 7





    read the docs to see why find_last_of behaves that way, you want rfind if you want to match a substring in a string from the end

    – EdChum
    Mar 22 at 9:36














3












3








3








string s = "I Like C++ Tutorial";
cout << s.find_last_of("Like");


I know that find_last_of returns the last character that matches, however, it returns 16, which is the position of the letter i in Tutorial, But I'm confused because I'm searching for the last position of Like not i, I tried to remove i from the string. It returns 5 as I expected. But the question is why did it return 16?










share|improve this question














string s = "I Like C++ Tutorial";
cout << s.find_last_of("Like");


I know that find_last_of returns the last character that matches, however, it returns 16, which is the position of the letter i in Tutorial, But I'm confused because I'm searching for the last position of Like not i, I tried to remove i from the string. It returns 5 as I expected. But the question is why did it return 16?







c++ string






share|improve this question













share|improve this question











share|improve this question




share|improve this question










asked Mar 22 at 9:35









DamonDamon

212




212







  • 7





    read the docs to see why find_last_of behaves that way, you want rfind if you want to match a substring in a string from the end

    – EdChum
    Mar 22 at 9:36













  • 7





    read the docs to see why find_last_of behaves that way, you want rfind if you want to match a substring in a string from the end

    – EdChum
    Mar 22 at 9:36








7




7





read the docs to see why find_last_of behaves that way, you want rfind if you want to match a substring in a string from the end

– EdChum
Mar 22 at 9:36






read the docs to see why find_last_of behaves that way, you want rfind if you want to match a substring in a string from the end

– EdChum
Mar 22 at 9:36













2 Answers
2






active

oldest

votes


















5














find_last_of finds the last character equal to one of characters in the given character sequence.



The character sequence is "Like". The last L is at position 3, the last i is at position 16, the last k is at position 4 and the the last e is at position 5. So it returned the 16, the greatest of these values.



If the character sequence was "like" instead of "Like", it would have returned 18 because the last l is at position 18.



In case no letter in the character sequence that matches any letter in the string, npos is returned.






share|improve this answer

























  • Might want to note what it does in the case an element is not found, e.g. searching for "Likes" finds no 's'

    – Caleth
    Mar 22 at 9:51


















1














std::find_last_of




Finds the last character equal to one of characters in str




this means when searching the string "I Like C++ Tutorial" for the string "Like" the last character that apears in both strings is "i" wich is at position 16.



when searching for a complete string use std::find



std::string s("I Like C++ Tutorial");
std::cout << s.find("Like"); // prints 2


if you want to find the last occurence of the string use std::rfind



std::string s("I Like C++ Tutorial Like");
std::cout << s.rfind("Like"); // prints 20


to get the position of the last character in the last match you just have to add the length of the string:



std::string s("I Like C++ Tutorial Like");
std::string s2("Like");
std::cout << s.rfind(s2) + s2.length(); // prints 24





share|improve this answer























    Your Answer






    StackExchange.ifUsing("editor", function ()
    StackExchange.using("externalEditor", function ()
    StackExchange.using("snippets", function ()
    StackExchange.snippets.init();
    );
    );
    , "code-snippets");

    StackExchange.ready(function()
    var channelOptions =
    tags: "".split(" "),
    id: "1"
    ;
    initTagRenderer("".split(" "), "".split(" "), channelOptions);

    StackExchange.using("externalEditor", function()
    // Have to fire editor after snippets, if snippets enabled
    if (StackExchange.settings.snippets.snippetsEnabled)
    StackExchange.using("snippets", function()
    createEditor();
    );

    else
    createEditor();

    );

    function createEditor()
    StackExchange.prepareEditor(
    heartbeatType: 'answer',
    autoActivateHeartbeat: false,
    convertImagesToLinks: true,
    noModals: true,
    showLowRepImageUploadWarning: true,
    reputationToPostImages: 10,
    bindNavPrevention: true,
    postfix: "",
    imageUploader:
    brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
    contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
    allowUrls: true
    ,
    onDemand: true,
    discardSelector: ".discard-answer"
    ,immediatelyShowMarkdownHelp:true
    );



    );













    draft saved

    draft discarded


















    StackExchange.ready(
    function ()
    StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f55296650%2fhow-stdfind-last-of-actually-works%23new-answer', 'question_page');

    );

    Post as a guest















    Required, but never shown

























    2 Answers
    2






    active

    oldest

    votes








    2 Answers
    2






    active

    oldest

    votes









    active

    oldest

    votes






    active

    oldest

    votes









    5














    find_last_of finds the last character equal to one of characters in the given character sequence.



    The character sequence is "Like". The last L is at position 3, the last i is at position 16, the last k is at position 4 and the the last e is at position 5. So it returned the 16, the greatest of these values.



    If the character sequence was "like" instead of "Like", it would have returned 18 because the last l is at position 18.



    In case no letter in the character sequence that matches any letter in the string, npos is returned.






    share|improve this answer

























    • Might want to note what it does in the case an element is not found, e.g. searching for "Likes" finds no 's'

      – Caleth
      Mar 22 at 9:51















    5














    find_last_of finds the last character equal to one of characters in the given character sequence.



    The character sequence is "Like". The last L is at position 3, the last i is at position 16, the last k is at position 4 and the the last e is at position 5. So it returned the 16, the greatest of these values.



    If the character sequence was "like" instead of "Like", it would have returned 18 because the last l is at position 18.



    In case no letter in the character sequence that matches any letter in the string, npos is returned.






    share|improve this answer

























    • Might want to note what it does in the case an element is not found, e.g. searching for "Likes" finds no 's'

      – Caleth
      Mar 22 at 9:51













    5












    5








    5







    find_last_of finds the last character equal to one of characters in the given character sequence.



    The character sequence is "Like". The last L is at position 3, the last i is at position 16, the last k is at position 4 and the the last e is at position 5. So it returned the 16, the greatest of these values.



    If the character sequence was "like" instead of "Like", it would have returned 18 because the last l is at position 18.



    In case no letter in the character sequence that matches any letter in the string, npos is returned.






    share|improve this answer















    find_last_of finds the last character equal to one of characters in the given character sequence.



    The character sequence is "Like". The last L is at position 3, the last i is at position 16, the last k is at position 4 and the the last e is at position 5. So it returned the 16, the greatest of these values.



    If the character sequence was "like" instead of "Like", it would have returned 18 because the last l is at position 18.



    In case no letter in the character sequence that matches any letter in the string, npos is returned.







    share|improve this answer














    share|improve this answer



    share|improve this answer








    edited Mar 22 at 9:57

























    answered Mar 22 at 9:47









    P.WP.W

    19.1k41860




    19.1k41860












    • Might want to note what it does in the case an element is not found, e.g. searching for "Likes" finds no 's'

      – Caleth
      Mar 22 at 9:51

















    • Might want to note what it does in the case an element is not found, e.g. searching for "Likes" finds no 's'

      – Caleth
      Mar 22 at 9:51
















    Might want to note what it does in the case an element is not found, e.g. searching for "Likes" finds no 's'

    – Caleth
    Mar 22 at 9:51





    Might want to note what it does in the case an element is not found, e.g. searching for "Likes" finds no 's'

    – Caleth
    Mar 22 at 9:51













    1














    std::find_last_of




    Finds the last character equal to one of characters in str




    this means when searching the string "I Like C++ Tutorial" for the string "Like" the last character that apears in both strings is "i" wich is at position 16.



    when searching for a complete string use std::find



    std::string s("I Like C++ Tutorial");
    std::cout << s.find("Like"); // prints 2


    if you want to find the last occurence of the string use std::rfind



    std::string s("I Like C++ Tutorial Like");
    std::cout << s.rfind("Like"); // prints 20


    to get the position of the last character in the last match you just have to add the length of the string:



    std::string s("I Like C++ Tutorial Like");
    std::string s2("Like");
    std::cout << s.rfind(s2) + s2.length(); // prints 24





    share|improve this answer



























      1














      std::find_last_of




      Finds the last character equal to one of characters in str




      this means when searching the string "I Like C++ Tutorial" for the string "Like" the last character that apears in both strings is "i" wich is at position 16.



      when searching for a complete string use std::find



      std::string s("I Like C++ Tutorial");
      std::cout << s.find("Like"); // prints 2


      if you want to find the last occurence of the string use std::rfind



      std::string s("I Like C++ Tutorial Like");
      std::cout << s.rfind("Like"); // prints 20


      to get the position of the last character in the last match you just have to add the length of the string:



      std::string s("I Like C++ Tutorial Like");
      std::string s2("Like");
      std::cout << s.rfind(s2) + s2.length(); // prints 24





      share|improve this answer

























        1












        1








        1







        std::find_last_of




        Finds the last character equal to one of characters in str




        this means when searching the string "I Like C++ Tutorial" for the string "Like" the last character that apears in both strings is "i" wich is at position 16.



        when searching for a complete string use std::find



        std::string s("I Like C++ Tutorial");
        std::cout << s.find("Like"); // prints 2


        if you want to find the last occurence of the string use std::rfind



        std::string s("I Like C++ Tutorial Like");
        std::cout << s.rfind("Like"); // prints 20


        to get the position of the last character in the last match you just have to add the length of the string:



        std::string s("I Like C++ Tutorial Like");
        std::string s2("Like");
        std::cout << s.rfind(s2) + s2.length(); // prints 24





        share|improve this answer













        std::find_last_of




        Finds the last character equal to one of characters in str




        this means when searching the string "I Like C++ Tutorial" for the string "Like" the last character that apears in both strings is "i" wich is at position 16.



        when searching for a complete string use std::find



        std::string s("I Like C++ Tutorial");
        std::cout << s.find("Like"); // prints 2


        if you want to find the last occurence of the string use std::rfind



        std::string s("I Like C++ Tutorial Like");
        std::cout << s.rfind("Like"); // prints 20


        to get the position of the last character in the last match you just have to add the length of the string:



        std::string s("I Like C++ Tutorial Like");
        std::string s2("Like");
        std::cout << s.rfind(s2) + s2.length(); // prints 24






        share|improve this answer












        share|improve this answer



        share|improve this answer










        answered Mar 22 at 9:55









        peterzugerpeterzuger

        1113




        1113



























            draft saved

            draft discarded
















































            Thanks for contributing an answer to Stack Overflow!


            • Please be sure to answer the question. Provide details and share your research!

            But avoid


            • Asking for help, clarification, or responding to other answers.

            • Making statements based on opinion; back them up with references or personal experience.

            To learn more, see our tips on writing great answers.




            draft saved


            draft discarded














            StackExchange.ready(
            function ()
            StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f55296650%2fhow-stdfind-last-of-actually-works%23new-answer', 'question_page');

            );

            Post as a guest















            Required, but never shown





















































            Required, but never shown














            Required, but never shown












            Required, but never shown







            Required, but never shown

































            Required, but never shown














            Required, but never shown












            Required, but never shown







            Required, but never shown







            Popular posts from this blog

            Kamusi Yaliyomo Aina za kamusi | Muundo wa kamusi | Faida za kamusi | Dhima ya picha katika kamusi | Marejeo | Tazama pia | Viungo vya nje | UrambazajiKuhusu kamusiGo-SwahiliWiki-KamusiKamusi ya Kiswahili na Kiingerezakuihariri na kuongeza habari

            Swift 4 - func physicsWorld not invoked on collision? The Next CEO of Stack OverflowHow to call Objective-C code from Swift#ifdef replacement in the Swift language@selector() in Swift?#pragma mark in Swift?Swift for loop: for index, element in array?dispatch_after - GCD in Swift?Swift Beta performance: sorting arraysSplit a String into an array in Swift?The use of Swift 3 @objc inference in Swift 4 mode is deprecated?How to optimize UITableViewCell, because my UITableView lags

            Access current req object everywhere in Node.js ExpressWhy are global variables considered bad practice? (node.js)Using req & res across functionsHow do I get the path to the current script with Node.js?What is Node.js' Connect, Express and “middleware”?Node.js w/ express error handling in callbackHow to access the GET parameters after “?” in Express?Modify Node.js req object parametersAccess “app” variable inside of ExpressJS/ConnectJS middleware?Node.js Express app - request objectAngular Http Module considered middleware?Session variables in ExpressJSAdd properties to the req object in expressjs with Typescript