Search code examples
c++stringvectoriteratordereference

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

std::string str= *ritr;

What am I missing here?


Solution

  • 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.

    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.


    Also, a few remarks:

    I have a function of type std::vector<std::string>

    A function cannot not have type std::vector<std::string>. It can return a value of type std::vector<std::string>, or it can accept one. 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:

    bool IsOperator(std::string const& s)