我有一个来自于
代码如下:
如果我使用
为什么我不能使用这个迭代器的
std::list<std::string>
的迭代器,但是当我尝试使用 +=
来移动它时,编译出错了。代码如下:
#include <list>
#include <iostream>
#include <string>
int main() {
std::list<std::string> x;
x.push_front("British");
x.push_back("character");
x.push_front("Coding is unco");
x.push_back("Society");
x.push_back("City Hole");
auto iter = x.begin();
iter += 3;
//std::advance(iter, 3);
x.erase(iter);
for (auto &e: x) {
std::cout << e << "\n";
}
}
如果我使用
clang++ -std=c++11 -o li li.cpp
进行编译,结果如下:li.cpp:13:10: error: no viable overloaded '+='
iter += 3;
~~~~ ^ ~
1 error generated.
为什么我不能使用这个迭代器的
+=
操作符?
iter += 3
的意思是iter = iter + 3
,这是不可行的。 - Mudiiter += 3
的意思是“调用重载运算符+=
”。与数值类型不同,它与+
没有固有的联系。 - Pete Becker