Created
November 5, 2010 12:57
-
-
Save yuttie/664098 to your computer and use it in GitHub Desktop.
This file contains hidden or 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
| // 環境 | |
| // gcc version 4.5.1 (Gentoo 4.5.1 p1.1, pie-0.4.5) | |
| // boost 1.44.0 | |
| #include <boost/iterator/iterator_facade.hpp> | |
| #include <boost/utility.hpp> | |
| #include <boost/range/sub_range.hpp> | |
| template <class Range> | |
| struct slide_range_iterator | |
| : public boost::iterator_facade< | |
| slide_range_iterator<Range>, | |
| boost::sub_range<Range> const, | |
| boost::forward_traversal_tag | |
| > | |
| { | |
| slide_range_iterator() | |
| : cur_rng_(), end_it_() | |
| {} | |
| slide_range_iterator(Range rng, int length) | |
| : cur_rng_(boost::begin(rng), boost::next(boost::begin(rng), length)), | |
| end_it_(boost::end(rng)) | |
| {} | |
| private: | |
| friend class boost::iterator_core_access; | |
| typedef typename boost::range_iterator<Range>::type inner_iterator; | |
| boost::sub_range<Range> const& dereference() const { | |
| return cur_rng_; | |
| } | |
| bool equal(slide_range_iterator const& other) const { | |
| return this->cur_rng_ == other.cur_rng_; | |
| } | |
| void increment() { | |
| if (cur_rng_.end() == end_it_) { | |
| cur_rng_ = boost::sub_range<Range>(); | |
| end_it_ = inner_iterator(); | |
| } | |
| else { | |
| cur_rng_.advance_begin(1); | |
| cur_rng_.advance_end(1); | |
| } | |
| } | |
| private: | |
| boost::sub_range<Range> cur_rng_; | |
| inner_iterator end_it_; | |
| }; | |
| #include <iostream> | |
| #include <vector> | |
| #include <string> | |
| #include <boost/range/algorithm/equal.hpp> | |
| #include <boost/assign.hpp> | |
| #include <boost/foreach.hpp> | |
| using namespace std; | |
| using namespace boost::assign; | |
| int main(int argc, char *argv[]) { | |
| vector<int> const rng = {9, 8, 7, 6, 5, 4, 3, 2, 1, 0}; | |
| //string const rng = "abcdefghij"; | |
| auto answer = rng; | |
| typedef slide_range_iterator<decltype(rng)> sri; | |
| sri it(rng, 10); | |
| // なぜか false になる。 | |
| cout << std::boolalpha << boost::equal(*it, answer) << endl; | |
| // なぜか 0076543210 と表示される。(正解は 9876543210) | |
| // vector<short> だと 0000543210 になります。 | |
| for (; it != sri(); ++it) { | |
| BOOST_FOREACH (auto x, *it) { | |
| cout << x; | |
| } | |
| cout << endl; | |
| } | |
| return 0; | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment