ottengo questo erroredereferencing rendimenti stringa iteratore int
comparison between pointer and integer ('int' and 'const char *')
Per il seguente codice di
#include <iostream>
#include <sstream>
#include <string>
using namespace std;
int main()
{
std::string s("test string");
for(auto i = s.begin(); i != s.end(); ++i)
{
cout << ((*i) != "s") << endl;
}
}
Perché il dereferencing all'iteratore stringa di cedere un int
e non std::string
?
Si dovrebbe scrivere 'cout << ((* i)! = 'S') << endl;' (notare le virgolette singole). –
Si sta confrontando un carattere ('* i') con un puntatore a una stringa (' "s" '). Questo non può funzionare. –
se si usa già 'auto', perché non usare range-for come in' for (auto const & c: s) {cout << (c! = 'S') << '\ n'; } '? (nota che raramente vuoi usare 'endl' e molto spesso' \ n') – TemplateRex