codepad
[
create a new paste
]
login
|
about
Language:
C
C++
D
Haskell
Lua
OCaml
PHP
Perl
Plain Text
Python
Ruby
Scheme
Tcl
#include <algorithm> #include <functional> #include <vector> #include <iterator> #include <locale> #include <iostream> template <typename T> struct finder : public std::unary_function<T, bool> { typedef typename std::iterator_traits<typename T::value_type::first_type>::value_type value_type; finder(value_type v) : v_(v) {} bool operator()(const typename T::value_type& v) const { return *v.first == v_; } const value_type v_; }; template <typename T> void mutate_sequance(T begin, T end) { typedef typename std::iterator_traits<T>::value_type value_type; typedef std::vector<std::pair<T, T> > V; V v; T curr = begin, next; for(; (next = std::adjacent_find(curr, end, std::not2(std::equal_to<value_type>()))) != end; curr = ++next) { v.push_back(std::make_pair(curr, next+1)); } v.push_back(std::make_pair(curr, next)); typename V::iterator _1 = std::find_if(v.begin(), v.end(), finder<V>(2)); typename V::iterator _2 = std::find_if(v.begin(), v.end(), finder<V>(5)); std::iter_swap(_1, _2); std::cout << "После:\t"; for (typename V::const_iterator it = v.begin(); it != v.end(); ++it) std::copy(it->first, it->second, std::ostream_iterator<value_type>(std::cout, " ")); std::cout << std::endl; } int main() { setlocale(LC_ALL, ""); int arr[] = {1,1,1,2,3,3,4,4,4,5,5,5,6}; const int N = sizeof(arr) / sizeof(arr[0]); std::vector<std::pair<const int*, const int*> > v; std::cout << "До:\t"; std::copy(arr, arr+N, std::ostream_iterator<int>(std::cout, " ")); std::cout << std::endl; mutate_sequance(arr, arr+N); return 0; }
Private
[
?
]
Run code
Submit