簡體   English   中英

用於在字符串向量中查找元素位置的函數中的錯誤(C ++)

[英]Error in a function used to find the position of an element in a vector of string (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);
    }
}

我需要在抱怨“自動”說明符的R程序中使用此函數,並且無法使用C ++ 11進行編譯。

我將功能更改為

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);
     }    
  }

但它不起作用,並且出現以下錯誤

In function 'int FindIndexString(std::vector<std::basic_string<char, std::char_traits<char>, std::allocator<char> >, std::allocator<std::basic_string<char, std::char_traits<char>, std::allocator<char> > > >*, std::string*)':
F_Utils.cpp:92: error: no matching function for call to 'find(__gnu_cxx::__normal_iterator<std::basic_string<char, std::char_traits<char>, std::allocator<char> >*, std::vector<std::basic_string<char, std::char_traits<char>, std::allocator<char> >, std::allocator<std::basic_string<char, std::char_traits<char>, std::allocator<char> > > > >, __gnu_cxx::__normal_iterator<std::basic_string<char, std::char_traits<char>, std::allocator<char> >*, std::vector<std::basic_string<char, std::char_traits<char>, std::allocator<char> >, std::allocator<std::basic_string<char, std::char_traits<char>, std::allocator<char> > > > >, const char*)'

有什么幫助嗎?

您需要為std::find #include <algorithm>

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM