Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

remove double quotes from a string in c++

I am stripping off double quotes from a string, but I keep getting this error from the following function. What is the problem here?

void readCSVCell(stringstream& lineStream, string& s) {
    std::getline(lineStream,s,',');
    s.erase(remove( s.begin(), s.end(), '\"' ), s.end());
}

[ERROR]

c.cpp: In function void readCSVCell(std::stringstream&, std::string&):
c.cpp:11: error: cannot convert __gnu_cxx::__normal_iterator<char*, std::basic_string<char, std::char_traits<char>, std::allocator<char> > > to const char* for argument 1 to int remove(const char*)

like image 207
user236215 Avatar asked Nov 29 '22 04:11

user236215


2 Answers

Don't you want something like:

s.erase(remove( s.begin(), s.end(), '\"' ),s.end());

As remove returns "A forward iterator pointing to the new end of the sequence, which now includes all the elements with a value other than value" rather than removing the values.

It compiles fine for me though (with gcc 4.4), so perhaps you just need to include <algorithm> and make sure you are either using namespace std or qualify the name.

like image 183
Jeff Foster Avatar answered Dec 05 '22 12:12

Jeff Foster


Do you have stdio.h included? Then there could be a conflict with remove. This is the reason why you always should prefix std-calls with, well, std::.

like image 25
Björn Pollex Avatar answered Dec 05 '22 12:12

Björn Pollex