mirror of
https://github.com/boostorg/array.git
synced 2025-07-03 07:46:31 +02:00
initial checkin of Nico's array library
[SVN r7678]
This commit is contained in:
48
array3.cpp
Normal file
48
array3.cpp
Normal file
@ -0,0 +1,48 @@
|
||||
/* example for using class array<>
|
||||
*/
|
||||
#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 (unsigned i=seasons.size()-1; i>0; --i) {
|
||||
swap(seasons.at(i),seasons.at((i+1)%seasons.size()));
|
||||
}
|
||||
|
||||
std::cout << "one way: ";
|
||||
print_elements(seasons);
|
||||
|
||||
// try swap()
|
||||
std::cout << "other way: ";
|
||||
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;
|
||||
}
|
||||
std::cout << std::endl;
|
||||
}
|
||||
|
||||
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;
|
||||
}
|
||||
|
Reference in New Issue
Block a user