迭代列表以查找元素的出现.代码问题
Iterating over list to find occurrences of element. Issue with code
有人知道此代码怎么了吗?我遇到以下汇编错误。目的是找到字符串" P"的出现,然后我从Stroustrup P57中获取了这个想法。我的假设是,我可以将迭代器递增以查找其他出现,但这不起作用。谢谢
find.cc: In function ‘int main(int, char**)’:
find.cc:34:16: error: no match for ‘operator+’ (operand types are ‘LI {aka std::_List_const_iterator<Ent>}’ and ‘int’)
i = find(i + 1, l.end(), e1);
#include <iostream>
#include <algorithm>
#include <list>
#include <string>
using namespace std;
struct Ent {
string name;
Ent(const string& name) : name(name) { }
bool operator== (const Ent& right) const {
return name == right.name;
}
};
int main(int argc, char *argv[])
{
list<Ent> l;
for (char c = 'a'; c <= 'z'; c++) {
Ent e(string(1, c));
l.push_back(e);
}
Ent e1("p");
typedef list<Ent>::const_iterator LI;
LI i = find(l.begin(), l.end(), e1);
int n = 0;
while (i != l.end()) {
++n;
i = find(i + 1, l.end(), e1);
}
cout << "find(" << e1.name << ") = " << n << endl;
return 0;
}
列表迭代器是双向迭代器,而不是随机访问迭代器。因此,它们没有operator+
,而只有operator++
。你可以写
++i;
i = find(i , l.end(), e1);
而不是。
相关文章:
- 在黑客地球上没有获得以下C ++代码问题的输出
- VS 代码C++问题
- 插入操作的二叉搜索树代码问题
- 如何在代码问题中解决此问题 代码强制
- 发送和十六进制消息时套接字代码C++问题
- 代码问题将字符串转换为图表执行
- C 代码问题?编译器问题?代码块
- 代码问题.使用矢量删除相同的数字,除了它的最后一个入口
- 迭代列表以查找元素的出现.代码问题
- 使用JNI从Java调用本机(C++)函数时发生访问冲突:这是我的代码问题,还是已知问题
- XCode 4.5简单的OpenGL C 代码问题
- 在VS 2010上使用boost::interprocess生成代码问题
- 简单代码问题-未定义标识符
- 基本的c++代码问题
- 识别C++代码问题
- C++角色战斗代码问题
- OpenScenegraph 示例代码问题
- PortAudio示例代码问题
- 关于cout和cin的c++简单代码问题
- 关于歧义排列的代码问题