Created
April 22, 2015 00:41
-
-
Save nekko1119/2dc65ccae3f1f7ce869b 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 <iterator> | |
#include <type_traits> | |
template <class Container> | |
auto to_index(const Container& container, typename Container::const_iterator it) | |
-> decltype(std::distance(std::begin(container), it)) | |
{ | |
static_assert(std::is_same< | |
typename std::iterator_traits< | |
typename Container::const_iterator | |
>::iterator_category, std::random_access_iterator_tag | |
>::value, ""); | |
return std::distance(std::begin(container), it); | |
} | |
template <class Container> | |
auto to_index(Container& container, typename Container::iterator it) | |
-> decltype(std::distance(std::begin(container), it)) | |
{ | |
static_assert(std::is_same< | |
typename std::iterator_traits< | |
typename Container::iterator | |
>::iterator_category, std::random_access_iterator_tag | |
>::value, ""); | |
return std::distance(std::begin(container), it); | |
} | |
template <class Container> | |
auto to_iterator(const Container& container, typename Container::size_type index) | |
-> decltype(std::next(std::begin(container), index)) | |
{ | |
static_assert(std::is_same< | |
typename std::iterator_traits< | |
typename Container::const_iterator | |
>::iterator_category, std::random_access_iterator_tag | |
>::value, ""); | |
return std::next(std::begin(container), index); | |
} | |
template <class Container> | |
auto to_iterator(Container& container, typename Container::size_type index) | |
-> decltype(std::next(std::begin(container), index)) | |
{ | |
static_assert(std::is_same< | |
typename std::iterator_traits< | |
typename Container::iterator | |
>::iterator_category, std::random_access_iterator_tag | |
>::value, ""); | |
return std::next(std::begin(container), index); | |
} | |
#include <vector> | |
#include <algorithm> | |
#include <cassert> | |
int main() | |
{ | |
using sequence = std::vector<int>;//std::array, std::basic_string, std::dequeでも動く…はず(試してない) | |
const sequence v = {10, 20, 30, 40, 50}; | |
const auto result = std::find(v.begin(), v.end(), 30); | |
const auto index = to_index(v, result); | |
const auto iter = to_iterator(v, index); | |
assert(*iter == v[index]); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment