Standard C++ Library Copyright 1998, Rogue Wave Software, Inc. NAME ostream_iterator - Stream iterators allow for use of iterators with ostreams and istreams. They allow generic algorithms to be used directly on streams. SYNOPSIS #include <ostream> template <class T, class charT, class traits = char_traits<charT> > class ostream_iterator : public iterator<output_iterator_tag,void,void>; DESCRIPTION Stream iterators use the standard iterator interface for input and output streams. The class ostream_iterator writes elements to an output stream. If you use the constructor that has a second char * argument, then that string is written after every element (the string must be null-terminated). Since an ostream iterator is an output iterator, it is not possible to get an element out of the iterator. You can only assign to it. INTERFACE template <class T, class charT, class traits = char_traits<charT> > class ostream_iterator : public iterator<output_iterator_tag,void,void> { public: typedef T value_type; typedef charT char_type; typedef traits traits_type; typedef basic_ostream<charT,traits> ostream_type; ostream_iterator(ostream&); ostream_iterator (ostream&, const char*); ostream_iterator (const ostream_iterator<T,charT,char_traits<charT> >&); ~ostream_itertor (); ostream_iterator<T,charT,char_traits<charT> >& operator=(const T&); ostream_iterator<T,charT,char_traits<charT> >& operator* () const; ostream_iterator<T,charT,char_traits<charT> >& operator++ (); ostream_iterator<T,charT,char_traits<charT> > operator++ (int); }; TYPES value_type; Type of value to stream in. char_type; Type of character the stream is built on. traits_type; Traits used to build the stream. ostream_type; Type of stream this iterator is constructed on. CONSTRUCTORS ostream_iterator (ostream& s); Constructs an_ostream_iterator on the given stream. ostream_iterator (ostream& s, const char* delimiter); Constructs an_ostream_iterator on the given stream. The null terminated string delimiter is written to the stream after every element. ostream_iterator (const ostream_iterator<T>& x); Copy constructor. DESTRUCTORS ~ostream_iterator (); Destroys an object of class ostream_iterator. OPERATORS const T& <br>operator= (const T& value); Shift the value T onto the output stream. const T& ostream_iterator<T>& operator* (); ostream_iterator<T>& operator++(); ostream_iterator<T> operator++ (int); These operators do nothing. They simply allow the itera- tor to be used in common constructs. EXAMPLE #include <iterator> #include <numeric> #include <deque> #include <iostream> using namespace std; int main () { // // Initialize a vector using an array. // int arr[4] = { 3,4,7,8 }; int total=0; deque<int> d(arr+0, arr+4); // // stream the whole vector and a sum to cout // copy(d.begin(),d.end()-1, ostream_iterator<int,char>(cout," + ")); cout << *(d.end()-1) << " = " << accumulate(d.begin(),d.end(),total) << endl; return 0; } WARNINGS If your compiler does not support default template parame- ters, then you always need to supply the Allocator template argument. For instance, you need to write: deque<int, allocator<int> > instead of: deque<int> If your compiler does not support namespaces, then you do not need the using declaration for std. SEE ALSO istream_iterator, Iterators
Закладки на сайте Проследить за страницей |
Created 1996-2024 by Maxim Chirkov Добавить, Поддержать, Вебмастеру |