Главная страница


ru.algorithms

 
 - RU.ALGORITHMS ----------------------------------------------------------------
 From : Dmitriy Gerasimenko                  2:5020/400     12 Aug 2002  14:59:49
 To : Ruslan Teluk
 Subject : Re: Реверс.
 -------------------------------------------------------------------------------- 
 
 
 RT> Есть ли алгоритмы для быстрого реверса массива?
 
 С++ библиотека STL
 файл - #include <algorithm>..:)
 //--------------------------------------------------------------------------
 Summary
 
 Reverses the order of elements in a collection.
 
 Synopsis
 
 #include <algorithm>
 template <class BidirectionalIterator>
 void reverse (BidirectionalIterator first,
               BidirectionalIterator last);
 
 Description
 
 The algorithm reverse reverses the elements in a sequence so that the
 last element becomes the new first element, and the first element
 becomes the new last. For each non-negative integer i <= (last - first)/2,
 reverse applies swap to all pairs of iterators first + I, (last - I) - 1.
 
 Because the iterators are assumed to be bidirectional, reverse does
 not return anything.
 
 Complexity
 
 reverse performs exactly (last - first)/2 swaps.
 
 Example
 
 //
 // reverse.cpp
 //
  #include <algorithm>
  #include <vector>
  #include <iostream>
  using namespace std;
  int main()
  {
    //Initialize a vector with an array of ints
    int arr[10] = {1,2,3,4,5,6,7,8,9,10};
    vector<int> v(arr, arr+10);
    //Print out elements in original (sorted) order
    cout << "Elements before reverse: " << endl << "     ";
    copy(v.begin(),v.end(),
         ostream_iterator<int,char>(cout," "));
    cout << endl << endl;
    //Reverse the ordering
 
    reverse(v.begin(), v.end());
    //Print out the reversed elements
    cout << "Elements after reverse: " << endl << "     ";
    copy(v.begin(),v.end(),
         ostream_iterator<int,char>(cout," "));
    cout << endl;
    return 0;
  }
 
 Program Output
 
 Elements before reverse:
      1 2 3 4 5 6 7 8 9 10
 Elements after reverse:
      10 9 8 7 6 5 4 3 2 1
 A reverse_copy to cout:
      1 2 3 4 5 6 7 8 9 10
 
 Warnings
 
 If your compiler does not support default template parameters,
 then you always need to supply the Allocator template argument.
 For instance, you need to write:
 
 vector<int, allocator<int> >
 
 instead of:
 
 vector<int>
 
 If your compiler does not support namespaces,
 then you do not need the using declaration for std.
 
 See Also
 
 reverse_copy, swap
 //--------------------------------------------------------------------------
 -
 --- ifmail v.2.15dev5
  * Origin: UNKNOWN (2:5020/400)
 
 

Вернуться к списку тем, сортированных по: возрастание даты  уменьшение даты  тема  автор 

 Тема:    Автор:    Дата:  
 Реверс.   Ruslan Teluk   11 Aug 2002 15:10:52 
 Re: Реверс.   Ruslan Teluk   11 Aug 2002 17:15:38 
 Ревеpс.   Kirill Lukjanov   12 Aug 2002 01:12:01 
 Ревеpс.   Alex Malashonok   12 Aug 2002 03:57:59 
 Re: Ревеpс.   Ruslan Teluk   12 Aug 2002 14:10:33 
 Ревеpс.   Alex Malashonok   12 Aug 2002 19:41:17 
 Ревеpс.   Sergey Popkov   13 Aug 2002 14:32:02 
 Ревеpс.   Alex Malashonok   14 Aug 2002 00:25:09 
 Re: Ревеpс.   Ruslan Teluk   15 Aug 2002 10:27:05 
 Re: Ревеpс.   Alexander Golovlev   06 Sep 2002 20:07:17 
 Re: Реверс.   Dmitriy Gerasimenko   12 Aug 2002 14:59:49 
Архивное /ru.algorithms/896806a4ad66.html, оценка 2 из 5, голосов 10
Яндекс.Метрика
Valid HTML 4.01 Transitional