GSP
Quick Navigator

Search Site

Unix VPS
A - Starter
B - Basic
C - Preferred
D - Commercial
MPS - Dedicated
Previous VPSs
* Sign Up! *

Support
Contact Us
Online Help
Handbooks
Domain Status
Man Pages

FAQ
Virtual Servers
Pricing
Billing
Technical

Network
Facilities
Connectivity
Topology Map

Miscellaneous
Server Agreement
Year 2038
Credits
 

USA Flag

 

 

Man Pages
std::basic_string::erase(3) C++ Standard Libary std::basic_string::erase(3)

std::basic_string::erase - std::basic_string::erase


basic_string& erase( size_type
index = 0, size_type count = npos (until C++20)
);
constexpr basic_string& erase(
size_type index = 0, size_type (since C++20)
count = npos );
iterator erase( iterator position (until C++11)
);
iterator erase( const_iterator (since C++11)
position ); (1) (until C++20)
constexpr iterator erase( (since C++20)
const_iterator position );
iterator erase( iterator first, (2) (until C++11)
iterator last );
iterator erase( const_iterator (since C++11)
first, const_iterator last ); (3) (until C++20)
constexpr iterator erase(
const_iterator first, (since C++20)
const_iterator last );


Removes specified characters from the string.


1) Removes min(count, size() - index) characters starting at index.
2) Removes the character at position.
3) Removes the characters in the range [first, last).


index - first character to remove
count - number of characters to remove
position - iterator to the character to remove
first, last - range of the characters to remove


1) *this
2) iterator pointing to the character immediately following the character erased, or
end() if no such character exists
3) iterator pointing to the character last pointed to before the erase, or end() if
no such character exists


1) std::out_of_range if index > size().
2-3) Throws nothing.


In any case, if an exception is thrown for any reason, this function has no effect
(strong exception guarantee).
(since C++11)

// Run this code


#include <algorithm>
#include <iostream>
#include <iterator>
#include <string>


int main()
{
std::string s = "This Is An Example";
std::cout << "1) " << s << '\n';


s.erase(7, 3); // erases " An" using overload (1)
std::cout << "2) " << s << '\n';


s.erase(std::find(s.begin(), s.end(), ' ')); // erases first ' '; overload (2)
std::cout << "3) " << s << '\n';


s.erase(s.find(' ')); // trims from ' ' to the end of the string; overload (1)
std::cout << "4) " << s << '\n';


auto it = std::next(s.begin(), s.find('s')); // obtains iterator to the first 's'
s.erase(it, std::next(it, 2)); // erases "sI"; overload (3)
std::cout << "5) " << s << '\n';
}


1) This Is An Example
2) This Is Example
3) ThisIs Example
4) ThisIs
5) This


clear clears the contents
(public member function)

2022.07.31 http://cppreference.com

Search for    or go to Top of page |  Section 3 |  Main Index

Powered by GSP Visit the GSP FreeBSD Man Page Interface.
Output converted with ManDoc.