【发布时间】:2015-01-13 10:50:20
【问题描述】:
这是我的代码的一部分:
std::vector<std::string> syntax_words;
//...
//Currently the above vector contains few strings
std::vector<std::string>::iterator sy_iterator = syntax_words.begin();
while (sy_iterator != syntax_words.end(){
if (*sy_iterator[4] == 'X'){
//...
基本上我想访问当前字符串中的第五个字符。不幸的是,上面的代码在编译过程中抛出了一个错误:
error: no match for ‘operator*’ (operand type is ‘std::basic_string<char>’)
if (*sy_iterator[4] == 'X'){
我也试过这个:
if (sy_iterator[4] == 'X'){
但它也抛出一个错误:
error: no match for ‘operator==’ (operand types are ‘std::basic_string<char>’ and ‘char’)
if (sy_iterator[sit] == 'X'){
我应该怎么做才能让它工作?
【问题讨论】:
-
优先,优先,优先……你的意思是
(*sy_iterator)[4](或者,等效地,sy_iterator->operator[](4))。 -
运算符优先级,试试
if ((*sy_iterator)[4] == 'X'){) -
@TheParamagneticCroissant 哦,谢谢。我忘记了。
-
@user3125731 即使您忘记了它(因为谁能记住所有优先级?)从编译器错误消息中推断出来也很简单。
-
@TheParamagneticCroissant 你完全正确。我不知道为什么我没有自己想出解决方案。也许是因为我有点累了。
标签: c++ string vector iterator