小编Zhi*_*Zou的帖子

带向量的 C++ 模板函数

我有以下代码行和编译错误。应该是我对模板函数,或者c++泛型,或者其他什么的理解错误。预先感谢您指出这一点。

\n\n
#include <iostream>\n#include <vector>\n\nusing namespace std;\n\ntemplate <typename T>\nT* find(vector<T> &vec, T value)\n{\n  vector<T>::iterator first = vec.begin();\n  vector<T>::iterator last = vec.end();\n  for(; first != last; first ++)\n      if(*first == value)\n          return first;\n  return 0;\n}\n
Run Code Online (Sandbox Code Playgroud)\n\n

控制台中的编译错误

\n\n
debug.cpp: In function \xe2\x80\x98T* find(std::vector<T, std::allocator<_CharT> >&, T)\xe2\x80\x99:\ndebug.cpp:9: error: expected `;' before \xe2\x80\x98first\xe2\x80\x99\ndebug.cpp:10: error: expected `;' before \xe2\x80\x98last\xe2\x80\x99\ndebug.cpp:11: error: \xe2\x80\x98first\xe2\x80\x99 was not declared in this scope\ndebug.cpp:11: error: \xe2\x80\x98last\xe2\x80\x99 was not declared in this scope\n
Run Code Online (Sandbox Code Playgroud)\n

c++ generics template-function

1
推荐指数
1
解决办法
2万
查看次数

标签 统计

c++ ×1

generics ×1

template-function ×1