迭代器 C++ 的类型
type of iterator c++
我正在尝试编写自己的排序函数,可以对向量容器进行排序。
我希望以以下方式调用它:
this:sort(arr.begin(( , arr.end(( - 1(
arr.end(( - 1 - 因为 arr.end(( 返回下一个元素,实际上不是元素,返回迭代器到 end。
所以我想在任何类型上使用迭代器调用我的函数。我尝试运行此代码:
#include<iostream>
#include<vector>
using namespace std;
template<typename T1>
void func(vector<T1>::iterator it) {
cout << "n" << *it;
}
int main() {
vector<int> arr(10);
for(int i = 0;i < 10;++i)
arr[i] = i+1;
func<int>(arr.begin());
}
但是编译器说:
main.cpp:10:23: error: variable or field ‘func’ declared void void func(vector<T1>::iterator it)
main.cpp:10:32: error: expected ‘)’ before ‘it’ void func(vector<T1>::iterator it)
main.cpp: In function ‘int main()’: main.cpp:26:5: error: ‘func’ was not declared in this scope
func<int>(arr.begin());
func main.cpp:26:10: error: expected primary-expression before ‘int’
func<int>(arr.begin());
请解释我的错误。谢谢
编译器
不知道迭代器是内部的一种类型vector<T1>
template<typename T1> void func(typename vector<T1>::iterator it) {
cout << "n" << *it; }
在模板内部,某些构造具有可能不同的语义 从一个实例化到另一个实例化。这样的构造取决于 模板参数。
您应该为此类依赖类型编写typename
。
相关文章:
- ArduinoJson 6.15.2:JsonObject没有命名类型
- 防止主数据类型C++的隐式转换
- 大量序列中核苷酸类型的快速计数
- 如何从C++中的依赖类型中获得它所依赖的类型
- 有关插入适配器的错误。[错误]请求从 'back_insert_iterator<vector<>>' 类型转换为非标量类型
- 是否可以初始化不可复制类型的成员变量(或基类)
- 如何获取std::result_of函数的返回类型
- 从父命名空间重载类型
- 如果C++类在类方法中具有动态分配,但没有构造函数/析构函数或任何非静态成员,那么它仍然是POD类型吗
- 我想将一个对T类型的非常量左值引用绑定到一个T类型的临时值
- Openssl 1.1.1d无效使用不完整的类型"struct dsa_st"
- 访问者访问变体并返回不同类型时出错
- 在VS2010-VS2015下编译时,如何使用decltype作为较大类型表达式的LHS
- 处理小于cpu数据总线的数据类型.(c++转换为机器代码)
- C++ 雷神库 - 使用资源加载器类时出现问题(不命名类型)
- 模板元程序查找相似的连续类型名称
- 是否可以从int转换为enum类类型
- 构造函数正在调用一个使用当前类类型的函数
- 我应该使用什么来代替void作为变体中的替代类型之一
- 类中的字符串不命名类型