使用字符串类型的取消引用矢量迭代器作为函数参数

Using dereferenced Vector iterator of type string as function argument

本文关键字:迭代器 参数 函数 引用 字符串 类型 取消      更新时间:2023-10-16

我正在用迭代一个向量

std::vector<std::string>::reverse_iterator ritr; 

我需要在某个时刻找出这个向量中的字符串是否是使用函数的运算符

bool IsOperator(const std::string s);

当我按照以下方式调用函数时,

if(IsOperator(*ritr))

eclipse抱怨!

Candidates are: bool IsOperator(char) bool IsOperator(std::basic_string<char,std::char_traits<char>,std::allocator<char>>)

(我有一个重载函数,它接受char而不是std::string)然而,它允许将延迟迭代器存储在字符串中的操作

std::string str= *ritr;

我在这里错过了什么?

您的代码很好。消除isOperator(char)isOperator(string)之间的歧义不是问题,因为string不能隐式转换为char

我希望你的程序能够编译(如果没有,那就说明你没有显示其他东西),而IDE抱怨红色的歪歪扭扭只意味着你的IDE有一个bug。


此外,还有几点意见:

我有一个std::vector<std::string> 类型的函数

函数不能具有类型std::vector<std::string>。它可以返回类型为std::vector<std::string>的值,也可以接受一个值。我猜你是指后者。

bool IsOperator(const std::string s)

这个函数签名没有多大意义。您的意思可能是接受对std::string对象的常量引用

bool IsOperator(std::string const& s)