How to sum up elements of a C++ vector?

Actually there are quite a few methods. C++03 Classic for loop: for(std::vector<int>::iterator it = vector.begin(); it != vector.end(); ++it) sum_of_elems += *it; Using a standard algorithm: #include <numeric> sum_of_elems = std::accumulate(vector.begin(), vector.end(), 0); Important Note: The last argument’s type is used not just for the initial value, but for the type of the result as well. If you … 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: