简体   繁体   English

使用类型为string的dereferenced Vector迭代器作为函数参数

[英]Using dereferenced Vector iterator of type string as function argument

I am iterating through a vector with 我正在迭代一个向量

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

I need to at some point find out if a string in this vector is an operator using the function 我需要在某个时候找出此向量中的字符串是否是使用该函数的运算符

bool IsOperator(const std::string s);

When I call the function following way, 当我按下面的方式调用函数时,

if(IsOperator(*ritr))

eclipse complains! 日食抱怨!

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

(I have an overloaded function with accepts char instead of std::string ) However, it allows the operation of storing the deferenced iterator in a string (我有一个重载函数,接受char而不是std::string )但是,它允许将deferenced迭代器存储在字符串中的操作

std::string str= *ritr;

What am I missing here? 我在这里错过了什么?

Your code is fine. 你的代码很好。 Disambiguating between isOperator(char) and isOperator(string) is not a problem, because a string cannot be implicitly converted to a char . 消除isOperator(char)isOperator(string)之间的歧义不是问题,因为string不能隐式转换为char

I expect your program to compile (if it doesn't, there's something else you are not showing), while the IDE complaining with red squiggles just means that your IDE has a bug. 我希望你的程序能够编译(如果没有,你还有其他东西没有显示),而IDE抱怨红色曲线只是意味着你的IDE有一个bug。


Also, a few remarks: 还有一些评论:

I have a function of type std::vector<std::string> 我有一个类型为std::vector<std::string>的函数

A function cannot not have type std::vector<std::string> . 函数不能没有类型std::vector<std::string> It can return a value of type std::vector<std::string> , or it can accept one. 它可以返回 std::vector<std::string>类型的值,也可以接受一个值。 I guess you meant the latter. 我想你的意思是后者。

bool IsOperator(const std::string s)

This function signature doesn't make much sense. 这个函数签名没有多大意义。 You probably meant accepting a constant reference to an std::string object: 您可能意味着接受对std::string对象的常量引用

bool IsOperator(std::string const& s)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM