2000-08-02 18:15:32 +00:00
|
|
|
/* 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) {
|
2002-01-23 18:07:11 +00:00
|
|
|
std::swap(seasons.at(i),seasons.at((i+1)%seasons.size()));
|
2000-08-02 18:15:32 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
std::cout << "one way: ";
|
|
|
|
print_elements(seasons);
|
|
|
|
|
|
|
|
// try swap()
|
|
|
|
std::cout << "other way: ";
|
2002-01-23 18:07:11 +00:00
|
|
|
std::swap(seasons,seasons_orig);
|
2000-08-02 18:15:32 +00:00
|
|
|
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;
|
2001-08-23 20:33:35 +00:00
|
|
|
|
|
|
|
return 0; // makes Visual-C++ compiler happy
|
2000-08-02 18:15:32 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
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;
|
|
|
|
}
|
|
|
|
|