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;
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
add a comment |
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
7
read the docs to see whyfind_last_of
behaves that way, you wantrfind
if you want to match a substring in a string from the end
– EdChum
Mar 22 at 9:36
add a comment |
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
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
c++ string
asked Mar 22 at 9:35
DamonDamon
212
212
7
read the docs to see whyfind_last_of
behaves that way, you wantrfind
if you want to match a substring in a string from the end
– EdChum
Mar 22 at 9:36
add a comment |
7
read the docs to see whyfind_last_of
behaves that way, you wantrfind
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
add a comment |
2 Answers
2
active
oldest
votes
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.
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
add a comment |
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
add a comment |
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
);
);
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
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
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.
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
add a comment |
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.
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
add a comment |
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.
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.
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
add a comment |
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
add a comment |
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
add a comment |
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
add a comment |
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
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
answered Mar 22 at 9:55
peterzugerpeterzuger
1113
1113
add a comment |
add a comment |
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.
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
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
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
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
7
read the docs to see why
find_last_of
behaves that way, you wantrfind
if you want to match a substring in a string from the end– EdChum
Mar 22 at 9:36