Why use string::iterator rather than index?

The index can only be used for containers that support random access – direct access to a given position. The iterator offers a unified way to access any collection/data structure. The flexibility when refactoring your code is immense.

How do I tokenize a string in C++?

C++ standard library algorithms are pretty universally based around iterators rather than concrete containers. Unfortunately this makes it hard to provide a Java-like split function in the C++ standard library, even though nobody argues that this would be convenient. But what would its return type be? std::vector<std::basic_string<…>>? Maybe, but then we’re forced to perform (potentially redundant and costly) … Read more

How to remove all the occurrences of a char in c++ string

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: Or use boost if that’s an option for you, like: 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:

Is there a tab equivalent of std::endl within the standard library?

No. std::endl isn’t a newline constant. It’s a manipulator which, in addition to inserting a newline, also flushes the stream. If you just want to add a newline, you’re supposed to just insert a ‘\n’. And if you just want to add a tab, you just insert a ‘\t’. There’s no std::tab or anything because inserting a tab plus flushing the stream is not exactly a common operation.

How to convert a std::string to const char* or char*

If you just want to pass a std::string to a function that needs const char* you can use If you want to get a writable copy, like char *, you can do that with this: Edit: Notice that the above is not exception safe. If anything between the new call and the delete call throws, you will leak memory, as nothing will call delete for you … Read more