2017-02-15 2 views
0

I ont la fonction suivante qui fonctionne très bien que j'utiliser pour trouver la position d'une chaîne dans un vecteur de chaîne:erreur dans une fonction utilisée pour trouver la position d'un élément dans un vecteur de chaîne (C++)

int FindIndexString(vector <string> *Names, string *name) 
{ 
    auto it = find(Names->begin(), Names->end(), name->c_str()); 
    if (it == Names->end()) 
    { 
     error("FindIndexString: %s not found",name->c_str()); 
    }else{ 
     auto index = distance(Names->begin(), it); 
     return((int)index); 
    } 
} 

J'ai besoin d'utiliser cette fonction dans un programme R qui se plaint du spécificateur "auto" et je ne peux pas compiler avec C++ 11.

J'ai changé la fonction

int FindIndexString(vector <string> *Names, string *name) 
{ 
    vector<string>::iterator it; 
    it = find(Names->begin(), Names->end(), name->c_str()); 

    int pos = (int)distance(Names->begin(), it); 

    if(it==Names->end()) 
    { 
      error("FindIndexString: %s not found",name->c_str());; 
    }else{ 
     return(pos); 
    }  
    } 

mais il ne fonctionne pas et je reçois l'erreur suivante

En fonction « int FindIndexString (std :: vecteur, std :: allocateur>, std :: allocateur, std :: allocateur>>, std :: string) ': F_Utils.cpp: 92: erreur: aucune fonction correspondante pour l'appel de' find (__ gnu_cxx :: __ normal_iterator, std :: allocator> * , std :: vecteur, std :: allocateur>, std :: allocateur, std :: allocateur>>>>, __gnu_cxx :: __ normal_iterator, std :: allocateur> *, std :: vecteur, std :: allocateur >, std :: allocateur, std :: allocateur>>>>, const char *) '

Une aide?

+0

Etes-vous sûr que votre compilateur a mis en place 'std :: find()'? – Andy

Répondre

1

Vous devez #include <algorithm> pour std::find

+0

Ok, merci. C'était une question stupide – niandra82

+0

Ce n'est pas une question stupide, spécifiquement parce qu'avec votre compilateur C++ 11 vous pourriez avoir accès à std :: find via #include . Mais ne me demande pas pourquoi tu le fais. – fm0