Last active
August 29, 2015 14:12
-
-
Save MORTAL2000/e1a96fe96e0ce07e9379 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include <iostream> | |
#include <iomanip> | |
#include <algorithm> | |
#include <vector> | |
#include <cassert> | |
#include <iterator> | |
template<typename Container> | |
class Columniterator : public std::iterator<std::random_access_iterator_tag, | |
typename std::decay<decltype(std::declval<Container>()[0][0])>::type> | |
{ | |
using iterator = typename Container::iterator; | |
using type = typename std::decay<decltype(std::declval<Container>()[0][0])>::type; | |
public: | |
Columniterator(iterator _it, size_t _i) | |
: it(_it), i(_i) | |
{} | |
Columniterator& operator++() | |
{ | |
++it; | |
return *this; | |
} | |
Columniterator& operator++(int) | |
{ | |
auto result(*this); | |
++*this; | |
return result; | |
} | |
Columniterator& operator +=(std::ptrdiff_t offset) | |
{ | |
it += offset; | |
return *this; | |
} | |
Columniterator operator +(std::ptrdiff_t offset) const | |
{ | |
auto result(*this); | |
result += offset; | |
return result; | |
} | |
Columniterator& operator--() | |
{ | |
--it; | |
return *this; | |
} | |
Columniterator& operator--(int) | |
{ | |
auto result(*this); | |
--*this; return | |
result; | |
} | |
Columniterator& operator -=(std::ptrdiff_t offset) | |
{ | |
it -= offset; | |
return *this; | |
} | |
Columniterator operator -(std::ptrdiff_t offset) const | |
{ | |
auto result(*this); | |
result -= offset; | |
return result; | |
} | |
type& operator*() | |
{ | |
return (*it)[i]; | |
} | |
type* operator->() | |
{ | |
return &(it)[i]; | |
} | |
bool operator == (const Columniterator& rhs) const | |
{ | |
return (it == rhs.it && i == rhs.i); | |
} | |
bool operator != (const Columniterator& rhs) const | |
{ | |
return !(*this == rhs); | |
} | |
bool operator < (const Columniterator& rhs) const | |
{ | |
return it < rhs.it; | |
} | |
std::ptrdiff_t operator -(const Columniterator& rhs) const | |
{ | |
return (it - rhs.it); | |
} | |
void swap(Columniterator& other) | |
{ | |
using std::swap; | |
swap(it, other.it); | |
} | |
private: | |
iterator it; | |
size_t i; | |
}; | |
template<typename Container> | |
Columniterator<Container> begin(Container& container, int i) | |
{ | |
return Columniterator<Container>(container.begin(), i); | |
} | |
template<typename Container> | |
Columniterator<Container> end(Container& container, int i) | |
{ | |
return Columniterator<Container>(container.end(), i); | |
} | |
int main() | |
{ | |
const size_t SIZE = 3; | |
std::vector<std::vector<int>> matrix(SIZE, std::vector<int>(SIZE)); | |
std::cout << "******* original matrix ******\n"; | |
int value = 0; | |
for (auto&& i : matrix) | |
{ | |
for (auto&& j : i) | |
{ | |
std::cout << std::setfill(' ') << std::setw(3) << (j = value++) << ' '; | |
} | |
std::cout << '\n'; | |
} | |
//std::reverse(begin(matrix, 0), end(matrix, 0)); // passed | |
//std::sort(begin(matrix, 0), end(matrix, 0)); // passed | |
//begin(matrix, 0).swap(end(matrix, 0)); // failed | |
//std::swap(begin(matrix, 0), end(matrix, 0)); // failed | |
std::cout << "******* rotated matrix ******\n"; | |
for (const auto& i : matrix) | |
{ | |
for (const auto& j : i) | |
{ | |
std::cout << std::setfill(' ') << std::setw(3) << j << ' '; | |
} | |
std::cout << '\n'; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment