用于查找字符串向量中元素位置的函数出错 (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);
}
}
我需要在抱怨 "auto" 说明符的 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*)'
有什么帮助吗?
您需要#include <algorithm>
std::find
我有以下函数可以很好地用于查找字符串在字符串向量中的位置:
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);
}
}
我需要在抱怨 "auto" 说明符的 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*)'
有什么帮助吗?
您需要#include <algorithm>
std::find