Basically, replace
replaces a character with another and ''
is not a character. What you’re looking for is erase
.
See this question which answers the same problem. In your case:
#include <algorithm> str.erase(std::remove(str.begin(), str.end(), 'a'), str.end());
Or use boost
if that’s an option for you, like:
#include <boost/algorithm/string.hpp> boost::erase_all(str, "a");
All of this is well-documented on reference websites. But if you didn’t know of these functions, you could easily do this kind of things by hand:
std::string output; output.reserve(str.size()); // optional, avoids buffer reallocations in the loop for(size_t i = 0; i < str.size(); ++i) if(str[i] != 'a') output += str[i];
Related Posts:
- C++ Double Address Operator? (&&)
- How to sum up elements of a C++ vector?
- Initializing a static std::map
in C++ - What is the easiest way to initialize a std::vector with hardcoded elements?
- How do I erase an element from std::vector<> by index?
- Is it more efficient to copy a vector by reserving and copying, or by creating and swapping? [duplicate]
- How do I reverse a C++ vector?
- A proper way to create a matrix in c++
- A proper way to create a matrix in c++
- Why does the C++ STL not provide any “tree” containers?
- How to iterate through a list of objects in C++?
- push_back vs emplace_back
- How to find if a given key exists in a C++ std::map
- Parsing a comma-delimited std::string
- How to ensure that a std::map is ordered?
- What is the difference between set and hashset in C++ STL?
- push_back vs emplace_back
- Remove spaces from std::string in C++
- What is the difference between const_iterator and non-const iterator in the C++ STL?
- Remove spaces from std::string in C++
- How to check that an element is in a std::set?
- What’s the most efficient way to erase duplicates and sort a vector?
- Best way to extract a subvector from a vector?
- Implementation of Vector in C++
- Appending a vector to a vector
- Determine if map contains a value for a key?
- C++ Erase vector element by value rather than by position?
- How to get current time in milliseconds?
- Use the auto keyword in C++ STL
- std::string length() and size() member functions
- sorting in std::map where key is a std::string
- How to do std::string indexof in C++ that returns index of matching string?
- Why can’t I make a vector of references?
- C++ equivalent of StringBuffer/StringBuilder?
- Displaying contents of a vector container in C++
- List iterator not dereferencable?
- Why in C++ do we use DWORD rather than unsigned int? [duplicate]
- Sleep for milliseconds
- C++ Cout & Cin & System “Ambiguous” [closed]
- How to track down a “double free or corruption” error
- The Definitive C++ Book Guide and List
- C++ code file extension? What is the difference between .cc and .cpp
- undefined reference to `WinMain@16′
- What is C# equivalent of
- How can I clear console
- How to go from fopen to fopen_s
- Are vectors passed to functions by value or by reference in C++
- How does the modulus operator work?
- Single class has a Class Redefinition Error
- Networking with C++
- Error a function-definition is not allowed here before ‘{‘ token
- invalid new-expression of abstract class type error
- How can I create objects while adding them into a vector?
- what does “error : a nonstatic member reference must be relative to a specific object” mean?
- “Symbol(s) not found for architecture x86_64” on QtCreator project
- How to write C++ getters and setters
- std::out_of_range error?
- Checking cin input stream produces an integer
- Why do C++ objects have a default destructor?
- Where do “pure virtual function call” crashes come from?
- How to get current timestamp in milliseconds since 1970 just the way Java gets
- What is the best open XML parser for C++?
- *** No rule to make target ‘class.cpp’, needed by `build/….x86/class.o` Stop. error in Ubuntu
- What’s the difference between opening a file with ios::binary or ios::out or both?
- Case-insensitive string comparison in C++
- Hash function for a string
- CMake error at CMakeLists.txt:30 (project): No CMAKE_C_COMPILER could be found
- How to run valgrind with basic c example?
- initial value of reference to non-const must be an lvalue
- Typedef function pointer?
- Strange error C2131: expression did not evaluate to a constant in VC 2015
- error: called object type ‘int’ is not a function or function pointer
- Hash function for a string
- Is there a replacement for unistd.h for Windows (Visual C)?
- How to call on a function found on another file?
- Double pointer array in c++
- Don’t understand static boolean behavior
- How to write std::string to file?
- Member declaration not found
- What is the difference between a .cpp file and a .h file?
- Stable Cotangent
- How to change string into QString?
- Including .cpp files
- Class template inheritance C++
- error: ISO C++ forbids in-class initialization of non-const static member
- Function call missing argument list to create pointer
- cannot specify explicit initializer for arrays
- What does ** mean in C++?
- Incomplete type is not allowed: stringstream
- Initializing default values in a struct
- Warning: comparison of distinct pointer types
- Two decimal places using printf( )
- error C2244 unable to match function definition to an existing declaration
- clearing a vector of pointers [duplicate]
- Evaluate a string with a switch in C++ [duplicate]
- C++ JSON Serialization
- stack around the variable…was corrupted
- Creating a list to hold objects in C++
- What does “warning: not all control paths return a value” mean? (C++)
- Array of Linked Lists C++