Finden, ob ein String ein Zeichen in C++ enthält (Boost erlaubt)

Finden, ob ein String ein Zeichen in C++ enthält (Boost erlaubt)


Angenommen, ich habe eine Zeichenfolge und ich möchte herausfinden, ob ein bestimmtes Zeichen (wie '|') vorhanden ist oder nicht, was ist die beste und schnellste Methode, dies zu tun? Ich kenne die String-Find-Implementierung. Ich bitte um eine noch schnellere Implementierung als diese.


Einige Code-Antworten


if (str.find('|') != std::string::npos) {
// ... }
if(strchr(str.c_str(), '|')) {
\\found }
size_t pos = strchr(str.c_str(),'|') - str.c_str();
string::size_type pos=string::npos;
for(string::size_type i=0;
i<s.size();
++i) { if(s[i] == key) {
pos=i;
break;
} } if(pos != string::npos) { // key was found } else { // not found }
string::iterator pos=s.end();
for(string::iterator i=s.begin();
i!=s.end();
++i) { if(*i == key) {
pos=i;
break;
} } if(pos != s.end()) { // found } else { // not found }
   string s1 = "Hello";
string s2 = "el";
if(strstr(s1.c_str(),s2.c_str())) {
cout <<
" S1 Contains S2";
}