0% found this document useful (0 votes)
21 views

C++ Notes

The document discusses several std::string member functions: 1. std::string::npos returns an unsigned integral value used by find functions to indicate no position was found. It is defined as -1 for signed representation. 2. std::string::find_first_not_of searches for the first character not matching given characters, returning the position or npos if none found. 3. std::string::find_last_not_of searches for the last character not matching given characters, returning the position or npos if none found. 4. std::string::substr returns a substring from the given position with the given length or to the end of the string. It throws

Uploaded by

amithabhm
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
21 views

C++ Notes

The document discusses several std::string member functions: 1. std::string::npos returns an unsigned integral value used by find functions to indicate no position was found. It is defined as -1 for signed representation. 2. std::string::find_first_not_of searches for the first character not matching given characters, returning the position or npos if none found. 3. std::string::find_last_not_of searches for the last character not matching given characters, returning the position or npos if none found. 4. std::string::substr returns a substring from the given position with the given length or to the end of the string. It throws

Uploaded by

amithabhm
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 4

12/8/15 1:18 PM

1) std::string::npos
Actually string::find() returns the position of the found string, but if it
doesn't find the given string, it returns string::npos, where npos means no
position.
npos is an unsigned integral value, Standard defines it to be -1 (signed
representation) which denotes no position.
//npos is unsigned, that is why cast is needed to make it signed! cout <<
(signed int) string::npos <<endl;
Output:
-1
2) std::string::find_first_not_of
Searches the string for the first character that does not match any of the
characters specified in its arguments.
Return Value
The position of the first character that does not match.
If no such characters are found, the function returns string::npos.
size_t is an unsigned integral type (the same as member
type string::size_type).
3) std::string::find_last_not_of
Searches the string for the last character that does not match any of the
characters specified in its arguments.
Return Value
The position of the first character that does not match.
If no such characters are found, the function returns string::npos.
4) std::string::substr
string substr (size_t pos = 0, size_t len = npos) const;
Generate substring
Returns a newly constructed string object with its value initialized to a copy
of a substring of this object.
The substring is the portion of the object that starts at character position pos
and spans len characters (or until the end of the string, whichever comes
first).

Parameters
pos
Position of the first character to be copied as a substring.
If this is equal to the string length, the function returns an empty string.
If this is greater than the string length, it throws out_of_range.
Note: The first character is denoted by a value of 0 (not 1).
len
Number of characters to include in the substring (if the string is shorter, as
many characters as possible are used).
A value of string::npos indicates all characters until the end of the string.
size_t is an unsigned integral type (the same as member type
string::size_type).
Return Value
A string object with a substring of this object.
5)

12/8/15 1:18 PM

12/8/15 1:18 PM

You might also like