如何从C ++中的迭代器中检索值类型? [英] How to retrieve value type from iterator in C++?

查看:103
本文介绍了如何从C ++中的迭代器中检索值类型?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的问题肯定是一个简单的任何人熟悉的C ++语法。我只是学习c ++,这是一些 作业。

  template< typename Iter> 
void quickSort(Iter begin,Iter end)
{
// ..
auto pivot = *(begin +(end - begin)/ 2);
// ..
}

pivot应该包含中心的[begin,end]间隔。 Ite
我在那里写的代码有效,但 auto 是来自新语言标准的关键字。如何做到老路?

解决方案

typename std :: iterator_traits< Iter> :: value_type



如果你的模板被实例化,这将工作 Iter 作为指针类型。



顺便说一句, typename 类型本身。它告诉编译器 value_type 真的是一个类型。如果它是函数或静态数据成员的名称,那么会影响语法。编译器不一定知道它是什么,因为 iterator_traits Iter 的专业化可能不可见时模板已编译。


My question is sure a simple one for anybody familiar with C++ syntax. I'm just learning c++ and this is some sort of homework.

template<typename Iter>
void quickSort(Iter begin, Iter end)
{        
    //..
    auto pivot = * ( begin + (end - begin)/2 );
    //..
}

pivot is supposed to contain value from the center of [begin, end] interval. Ite The code i wrote there works, but auto is a keyword from the new standart of language. How to do it the old-way? What to write instead of auto?

解决方案

typename std::iterator_traits<Iter>::value_type

This will work if your template is instantiated with Iter as a pointer type.

By the way, typename isn't part of the type itself. It tells the compiler that value_type really is a type. If it was the name of a function or a static data member, then that affects the syntax. The compiler doesn't necessarily know what it is, since the specialization of iterator_traits for Iter might not be visible when the template is compiled.

这篇关于如何从C ++中的迭代器中检索值类型?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆