簡體   English   中英

使用類型為string的dereferenced Vector迭代器作為函數參數

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

我正在迭代一個向量

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

我需要在某個時候找出此向量中的字符串是否是使用該函數的運算符

bool IsOperator(const std::string s);

當我按下面的方式調用函數時,

if(IsOperator(*ritr))

日食抱怨!

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

(我有一個重載函數,接受char而不是std::string )但是,它允許將deferenced迭代器存儲在字符串中的操作

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)

暫無
暫無

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

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