2017-06-11 3 views
0

std :: string :: find renvoie toujours string :: npos même s'il est supposé trouver quelque chose. Dans ce cas, j'essaie de trouver un {suivi d'une nouvelle ligne. Mais peu importe la corde que je mets là, elle ne le trouvera pas.std :: string :: find toujours retourne string :: npos even

pos=0; 
while(pos!=string::npos) 
{ 
    ko=input.find("{\n"); //here is the problem!!! 
    if(ko!=string::npos && input[ko]=='\n') 
    { 
     input.erase(ko, 3); 
     kc=input.find("}", ko); 
     for(pos=input.find("\",", ko); pos<kc; pos=input.find("\",\n", pos)) 
     { 
      input.erase(pos, 4); 
      input.insert(pos, " | "); 
     } 
     pos=input.find("\"\n", ko); 
     input.erase(pos, 3); 
    } 
    else 
    { 
     break; 
    } 
} 
pos=0; 
cn=1; 
for(pos=input.find("\"", pos); pos!=string::npos; pos=input.find("\"", pos)) 
{ 
    input.erase(pos,1); 
    if(cn) 
    { 
     input.insert(pos,"R"); 
    } 
    cn=1-cn; 
} 

Voici un morceau de ce que l'entrée a:

-- declaring identifiers of state and final states 
Detecting_SIDS = { 
    "Detecting", 
    "Detecting_CleanAir", 
    "Detecting_GasPresent" 
} 

-- declaring identifiers of transitions 
Detecting_TIDS = { 
    "__NULLTRANSITION__", 
    "Detecting_t2", 
    "Detecting_t3", 
    "Detecting_t4", 
    "Detecting_t5" 
} 

Ce code est censé tourner cette entrée ci-dessus pour les éléments suivants:

-- declaring identifiers of state and final states 
datatype Detecting_SIDS = RDetecting | RDetecting_CleanAir | RDetecting_GasPresent 

-- declaring identifiers of transitions 
datatype Detecting_TIDS = RNT | RDetecting_t2 | RDetecting_t3 | RDetecting_t4 | RDetecting_t5 
+1

Si elle trouve quelque chose, 'entrée [ko]' sera égal '{', 'pas \ n'. Vous lui donnez une condition toujours fausse. – krzaq

+0

Assomption on doit toujours être "j'ai probablement fait quelque chose de mal". Et en effet vous l'avez fait. Lisez ce que renvoie 'std :: find'. –

+0

Oops, && input [ko] == '\ n' était le coupable. Je mets au débogage et oublie de l'enlever. Merci @krzaq –

Répondre

0

Je pense que vous devriez traverser l'entrée avec l'algorithme std::find_first_of. Le retour de ceci est un intégrateur donc votre boucle devrait sortir sur cette valeur égale à std :: end (input). En utilisant cet itérateur, vous pouvez voir si le caractère suivant est votre \ n.

Ceci est du code non compilé à titre indicatif:

auto ptr = std::begin(input); 
auto end = std::end(input); 

while(ptr != end) 
{ 
    ptr = std::find_first_of(ptr, end, '{'); 

    if (ptr == end) 
     break; 

    else if (++ptr == end) 
     break; 

    else if (*ptr == '\n) 
    { 
     //Do Processing// 
    } 
}