std::find 不想使用自己的类包装器
std::find don't want to work with own class wrapper
#include <iostream>
#include <algorithm>
using namespace std;
template<class Iterator, class T>
Iterator find_my(Iterator first, Iterator last, const T& v)
{
while(first!=last && *first!=v)
++first;
return first;
}
struct int_node
{
int val;
int_node* next;
};
template<class Node>
struct node_wrap
{
Node* ptr;
node_wrap(Node* p = NULL) : ptr(p) { }
Node& operator*() const { return *ptr; }
Node* operator->() const { return ptr; }
node_wrap& operator++() { ptr = ptr->next; return *this; }
node_wrap& operator++(int) { node_wrap tmp = *this; ++this; return tmp; }
bool operator==(const node_wrap& i) const { return ptr == i.ptr; }
bool operator!=(const node_wrap& i) const { return ptr != i.ptr; }
};
bool operator==(const int_node& node, int n)
{
return node.val == n;
}
bool operator!=(const int_node& node, int n)
{
return node.val != n;
}
int main()
{
int_node* nod[10];
for(int i = 0; i<10; i++)
{
int b,j;
j=i;
cout << "nEnter number: ";
cin >> b;
nod[i] = new int_node;
nod[i]->val = b;
if(i==0)
nod[i]->next = NULL;
else
nod[i]->next = nod[--j];
}
int a;
cout << "nWhich number do you find: ";
cin >> a;
node_wrap<int_node> first(nod[9]);
node_wrap<int_node> search = find_my(first,node_wrap<int_node>(), a);
if(search != node_wrap<int_node>())
cout << "nAll is good: " << a;
cout << "nIf not good, then Bad";
cout << endl;
system("pause");
return 0;
}
我做了我的迭代器,它为类型 int_node 称为"结构node_wrap"。问题是标准函数从中查找不想使用此迭代器(node_wrap)。但是使用我自己的函数 find_my() 它工作得很好。我做错了什么?谢谢。
编译器发出很多错误。例如:
error C2868: 'std::iterator_traits<_Iter>::difference_type' :
illegal syntax for using- declaration;
expected qualified-name
1> with
1> [
1> _Iter=node_wrap<int_node>
1> ]
1>c:program files (x86)microsoft visual studio 11.0vcincludexutility(368):
error C2039: 'pointer' : is not a member of 'node_wrap<Node>'
1> with
1> [
1> Node=int_node
1> ]
std::find
需要更多关于您的类型的信息才能正常工作。特别是,std::iterator_traits
需要使用迭代器:
template<class Node>
struct node_wrap
{
typedef void difference_type; // you should use another difference type
typedef Node value_type;
typedef Node* pointer;
typedef Node& reference;
typedef std::forward_iterator_tag iterator_category;
// ...
};
相关文章:
- 没有为自己的结构调用列表推回方法
- 在他自己的方法中,有可能将一个对象取消引用到另一个对象吗
- 在c++中为我自己的基于指针的数组分配内存的正确方法
- C++从对象自己的类中删除对象
- 使用 std::optional,而不是自己的结构
- 子轴围绕父轴而不是他自己的轴旋转
- 这个C++编译器优化(在自身的实例上调用对象自己的构造函数)的名称是什么,它是如何工作的?
- C++ 如何为自己的迭代器类从迭代器转换为const_iterator?
- 重载 + 自己的类和 std::string 的运算符
- 类无法访问自己的私有静态 constexpr 方法 - Clang bug?
- 是否可以在不填充自己的参数的情况下将模板函数作为参数传递?
- 如何访问模板参数自己的模板参数?
- 将矩阵乘以我自己的输入的向量
- 您应该在什么时候创建自己的异常类型
- 派生类是从基类继承 v 指针并仅使用它,还是也有自己的 v 指针?
- string1 == string2 和你自己的 for 循环比较有什么区别?
- 如何正确包含我自己的标头?
- std::find 不想使用自己的类包装器
- 用我自己的例外编写一个包装器
- 我可以用我自己的类包装boost graph吗?