A std::string
resembles pretty much the functionality of a std::vector
. If you want to erase all the elements of your std::string
you could use std::string::clear
#include <iostream>
#include <string>
int main() {
std::string str("testing");
std::cout << str << std::endl;
str.clear();
std::cout << str << std::endl;
}
If you want to delete a particular character from your string (e.g., the 1st charater) you could use std::string::erase
:
#include <iostream>
#include <string>
int main() {
std::string str("testing");
std::cout << str << std::endl;
str.erase(str.begin());
std::cout << str << std::endl;
}
If you want to remove particular characters from your string you could, as in the case of std::vector
, use the erase-remove idiom:
#include <iostream>
#include <string>
#include <algorithm>
int main() {
std::string str("testing");
std::cout << str << std::endl;
str.erase(std::remove_if(str.begin(), str.end(), [](char const &c){ return c == 't'; }), str.end());
std::cout << str << std::endl;
}