📄 value_type.html
字号:
<HTML><!-- -- Copyright (c) 1996-1999 -- Silicon Graphics Computer Systems, Inc. -- -- Permission to use, copy, modify, distribute and sell this software -- and its documentation for any purpose is hereby granted without fee, -- provided that the above copyright notice appears in all copies and -- that both that copyright notice and this permission notice appear -- in supporting documentation. Silicon Graphics makes no -- representations about the suitability of this software for any -- purpose. It is provided "as is" without express or implied warranty. -- -- Copyright (c) 1994 -- Hewlett-Packard Company -- -- Permission to use, copy, modify, distribute and sell this software -- and its documentation for any purpose is hereby granted without fee, -- provided that the above copyright notice appears in all copies and -- that both that copyright notice and this permission notice appear -- in supporting documentation. Hewlett-Packard Company makes no -- representations about the suitability of this software for any -- purpose. It is provided "as is" without express or implied warranty. -- --><Head><Title>value_type</Title><!-- Generated by htmldoc --></HEAD><BODY BGCOLOR="#ffffff" LINK="#0000ee" TEXT="#000000" VLINK="#551a8b" ALINK="#ff0000"> <IMG SRC="CorpID.gif" ALT="SGI" HEIGHT="43" WIDTH="151"> <!--end header--><BR Clear><H1>value_type</H1><Table CellPadding=0 CellSpacing=0 width=100%><TR><TD Align=left><Img src = "iterators.gif" Alt="" WIDTH = "194" HEIGHT = "38" ></TD><TD Align=right><Img src = "function.gif" Alt="" WIDTH = "194" HEIGHT = "38" ></TD></TR><TR><TD Align=left VAlign=top><b>Category</b>: iterators</TD><TD Align=right VAlign=top><b>Component type</b>: function</TD></TR></Table><h3>Prototype</h3><tt>Value_type</tt> is overloaded; it is in fact five different functions.<pre>template <class T, class Distance> inline T* value_type(const <A href="input_iterator.html">input_iterator</A><T, Distance>&);template <class T, class Distance> inline T* value_type(const <A href="forward_iterator.html">forward_iterator</A><T, Distance>&);template <class T, class Distance> inline T* value_type(const <A href="bidirectional_iterator.html">bidirectional_iterator</A><T, Distance>&);template <class T, class Distance> inline T* value_type(const <A href="random_access_iterator.html">random_access_iterator</A><T, Distance>&);template <class T> inline T* value_type(const T*);</pre><h3>Description</h3><tt>Value_type</tt> is an <A href="iterator_tags.html">iterator tag</A> function: it is used todetermine the value type associated with an iterator. An iterator'svalue type is the type of object returned when the iterator isdereferenced; <A href="OutputIterator.html">Output Iterators</A> do not have value types (<A href="OutputIterator.html">Output Iterators</A> may only be used for storing values, not foraccessing values), but <A href="InputIterator.html">Input Iterators</A>, <A href="ForwardIterator.html">Forward Iterators</A>,<A href="BidirectionalIterator.html">Bidirectional Iterators</A>, and <A href="RandomAccessIterator.html">Random Access Iterators</A> do. <A href="#1">[1]</A>In some cases, such as an algorithm that mustdeclare a local variable that holds a value returned fromdereferencing an iterator, it is necessary to find out an iterator'svalue type. Accordingly, <tt>value_type(Iter)</tt> returns <tt>(T*) 0</tt>, where <tt>T</tt> is<tt>Iter</tt>'s value type.<P>Although <tt>value_type</tt> looks like a single function whosereturn type depends on its argument type, in reality it is a setof functions; the name <tt>value_type</tt> is overloaded. The function <tt>value_type</tt> must be overloaded for every iterator type <A href="#1">[1]</A>.<P>In practice, ensuring that <tt>value_type</tt> is defined requiresessentially no work at all. It is already defined for pointers, andfor the base classes <tt><A href="input_iterator.html">input_iterator</A></tt>,<tt><A href="forward_iterator.html">forward_iterator</A></tt>, <tt><A href="bidirectional_iterator.html">bidirectional_iterator</A></tt>, and<tt><A href="random_access_iterator.html">random_access_iterator</A></tt>. If you are implementing a new type offorward iterator, for example, you can simply derive it from the baseclass <tt><A href="forward_iterator.html">forward_iterator</A></tt>; this means that <tt>value_type</tt>(along with <tt><A href="iterator_category.html">iterator_category</A></tt> and <tt><A href="distance_type.html">distance_type</A></tt>) will automatically be defined for your iterator. These base classes areempty: they contain no member functions or member variables, butonly type information. Using them should therefore incur no overhead.<P>Note that, while the function <tt>value_type</tt> was present in theoriginal STL, it is no longer present in the most recent draft C++standard: it has been replaced by the <tt><A href="iterator_traits.html">iterator_traits</A></tt> classAt present both mechanisms are supported <A href="#2">[2]</A>, but eventually<tt>value_type</tt> will be removed.<h3>Definition</h3>Defined in the standard header <A href="iterator">iterator</A>, and in thenonstandard backward-compatibility header <A href="iterator.h">iterator.h</A>.This function is no longer part of the C++ standard, althoughit was present in early drafts of the standard. It is retainedin this implementation for backward compatibility.<h3>Requirements on types</h3>The argument of <tt>value_type</tt> must be an <A href="InputIterator.html">Input Iterator</A>, <A href="ForwardIterator.html">Forward Iterator</A>, <A href="BidirectionalIterator.html">Bidirectional Iterator</A>, or<A href="RandomAccessIterator.html">Random Access Iterator</A>. <A href="#1">[1]</A><h3>Preconditions</h3>None. <tt>Value_type</tt>'s argument is even permitted to bea singular iterator.<h3>Complexity</h3>At most amortized constant time. In many cases, a compiler should beable to optimize away <tt>value_type</tt> entirely.<h3>Example</h3>This example uses the <tt><A href="value_type.html">value_type</A></tt> iterator tag function in order todeclare a temporary variable of an iterator's value type.<pre>template <class <A href="ForwardIterator.html">ForwardIterator</A>1, class <A href="ForwardIterator.html">ForwardIterator</A>2, class ValueType>inline void __iter_swap(ForwardIterator1 a, ForwardIterator2 b, ValueType*) { T tmp = *a; *a = *b; *b = tmp;}template <class <A href="ForwardIterator.html">ForwardIterator</A>1, class <A href="ForwardIterator.html">ForwardIterator</A>2>inline void iter_swap(ForwardIterator1 a, ForwardIterator2 b) { __iter_swap(a, b, <A href="value_type.html">value_type</A>(a));}</pre><h3>Notes</h3><P><A name="1">[1]</A>Note that <tt>distance_type</tt> is not defined for <A href="OutputIterator.html">Output Iterators</A>or for <A href="trivial.html">Trivial Iterators</A>. In the case of <A href="OutputIterator.html">Output Iterators</A>,this is because an <A href="OutputIterator.html">Output Iterator</A> does not have a value type:it is not possible to dereference an <A href="OutputIterator.html">Output Iterator</A> and obtaina value. In the case of <A href="trivial.html">Trivial Iterators</A>, this is because the concept was introduced only for conceptual clarity,in order to separate the axioms related to an object that refersto another object from those related to iteration over a range.In fact, the STL does not define any types that are <A href="trivial.html">Trivial Iterators</A>.Although built-in C pointers may be <A href="trivial.html">Trivial Iterators</A>, the Ctype system does not allow a distinction between pointers that are<A href="trivial.html">Trivial Iterators</A> and pointers that are <A href="RandomAccessIterator.html">Random Access Iterators</A>into C arrays. Consequently, there is no <A href="trivial.html">Trivial Iterator</A> categorytag or iterator base.<P><A name="2">[2]</A>The <tt><A href="iterator_traits.html">iterator_traits</A></tt> classrelies on a C++ feature known as <i>partial specialization</i>. Many oftoday's compilers don't implement the complete standard; inparticular, many compilers do not support partial specialization. Ifyour compiler does not support partial specialization, then you willnot be able to use <tt><A href="iterator_traits.html">iterator_traits</A></tt>, and you will have to continue using the functions <tt><A href="iterator_category.html">iterator_category</A></tt>, <tt><A href="distance_type.html">distance_type</A></tt>,and <tt>value_type</tt>. This is one reason that those functions havenot yet been removed.<h3>See also</h3>The <A href="iterator_tags.html">Iterator Tags</A> overview, <tt><A href="iterator_traits.html">iterator_traits</A></tt>,<tt><A href="iterator_category.html">iterator_category</A></tt>, <tt><A href="distance_type.html">distance_type</A></tt>,<tt><A href="output_iterator_tag.html">output_iterator_tag</A></tt>, <tt><A href="input_iterator_tag.html">input_iterator_tag</A></tt>,<tt><A href="forward_iterator_tag.html">forward_iterator_tag</A></tt>, <tt><A href="bidirectional_iterator_tag.html">bidirectional_iterator_tag</A></tt>,<tt><A href="random_access_iterator_tag.html">random_access_iterator_tag</A></tt><!--start footer--> <HR SIZE="6"><A href="http://www.sgi.com/"><IMG SRC="surf.gif" HEIGHT="54" WIDTH="54" ALT="[Silicon Surf]"></A><A HREF="index.html"><IMG SRC="stl_home.gif" HEIGHT="54" WIDTH="54" ALT="[STL Home]"></A><BR><FONT SIZE="-2"><A href="http://www.sgi.com/Misc/sgi_info.html" TARGET="_top">Copyright © 1999 Silicon Graphics, Inc.</A> All Rights Reserved.</FONT><FONT SIZE="-3"><a href="http://www.sgi.com/Misc/external.list.html" TARGET="_top">TrademarkInformation</A></FONT><P></BODY></HTML>
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -