Blame view

3rdparty/boost_1_81_0/libs/array/test/array3.cpp 1.57 KB
e6ccf0ce   Hu Chunming   提交三方库
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
  /* example for using class array<>
   * (C) Copyright Nicolai M. Josuttis 2001.
   * Distributed under the Boost Software License, Version 1.0. (See
   * accompanying file LICENSE_1_0.txt or copy at
   * http://www.boost.org/LICENSE_1_0.txt)
   */
  
  #include <string>
  #include <iostream>
  #include <boost/array.hpp>
  
  template <class T>
  void print_elements (const T& x);
  
  int main()
  {
      // create array of four seasons
      boost::array<std::string,4> seasons = {
          { "spring", "summer", "autumn", "winter" }
      };
  
      // copy and change order
      boost::array<std::string,4> seasons_orig = seasons;
      for (std::size_t i=seasons.size()-1; i>0; --i) {
          std::swap(seasons.at(i),seasons.at((i+1)%seasons.size()));
      }
  
      std::cout << "one way:   ";
      print_elements(seasons);
  
      // try swap()
      std::cout << "other way: ";
      std::swap(seasons,seasons_orig);
      print_elements(seasons);
  
      // try reverse iterators
      std::cout << "reverse:   ";
      for (boost::array<std::string,4>::reverse_iterator pos
             =seasons.rbegin(); pos<seasons.rend(); ++pos) {
          std::cout << " " << *pos;
      }
  
      // try constant reverse iterators
      std::cout << "reverse:   ";
      for (boost::array<std::string,4>::const_reverse_iterator pos
             =seasons.crbegin(); pos<seasons.crend(); ++pos) {
          std::cout << " " << *pos;
      }
      std::cout << std::endl;
  
      return 0;  // makes Visual-C++ compiler happy
  }
  
  template <class T>
  void print_elements (const T& x)
  {
      for (unsigned i=0; i<x.size(); ++i) {
          std::cout << " " << x[i];
      }
      std::cout << std::endl;
  }