Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- std::vector<int> Index = { 3, 1, 2 };
- std::vector<std::string> Values = { "Third", "First", "Second" };
- Transformation = sort(Index);
- Index is now { 1, 2, 3};
- ... magic happens as Tranformation is applied to Values ...
- Values is now { "First", "Second", "Third" };
- typedef vector<int>::const_iterator myiter;
- vector<pair<size_t, myiter> > order(Index.size());
- size_t n = 0;
- for (myiter it = Index.begin(); it != Index.end(); ++it, ++n)
- order[n] = make_pair(n, it);
- struct ordering {
- bool operator ()(pair<size_t, myiter> const& a, pair<size_t, myiter> const& b) {
- return a.second < b.second;
- }
- };
- sort(order.begin(), order.end(), ordering());
- template <typename T>
- vector<T> sort_from_ref(
- vector<T> const& in,
- vector<pair<size_t, myiter> > const& reference
- ) {
- vector<T> ret(in.size());
- size_t const size = in.size();
- for (size_t i = 0; i < size; ++i)
- ret[i] = in[reference[i].first];
- return ret;
- }
- using namespace std;
- template< typename Iterator, typename Comparator >
- struct Index {
- vector<Iterator> v;
- Index( Iterator from, Iterator end, Comparator& c ){
- v.reserve( std::distance(from,end) );
- for( ; from != end; ++from ){
- v.push_back(from); // no deref!
- }
- sort( v.begin(), v.end(), c );
- }
- };
- template< typename Iterator, typename Comparator >
- Index<Iterator,Comparator> index ( Iterator from, Iterator end, Comparator& c ){
- return Index<Iterator,Comparator>(from,end,c);
- }
- struct mytype {
- string name;
- double number;
- };
- template< typename Iter >
- struct NameLess : public binary_function<Iter, Iter, bool> {
- bool operator()( const Iter& t1, const Iter& t2 ) const { return t1->name < t2->name; }
- };
- template< typename Iter >
- struct NumLess : public binary_function<Iter, Iter, bool> {
- bool operator()( const Iter& t1, const Iter& t2 ) const { return t1->number < t2->number; }
- };
- void indices() {
- mytype v[] = { { "me" , 0.0 }
- , { "you" , 1.0 }
- , { "them" , -1.0 }
- };
- mytype* vend = v + _countof(v);
- Index<mytype*, NameLess<mytype*> > byname( v, vend, NameLess<mytype*>() );
- Index<mytype*, NumLess <mytype*> > bynum ( v, vend, NumLess <mytype*>() );
- assert( byname.v[0] == v+0 );
- assert( byname.v[1] == v+2 );
- assert( byname.v[2] == v+1 );
- assert( bynum.v[0] == v+2 );
- assert( bynum.v[1] == v+0 );
- assert( bynum.v[2] == v+1 );
- }
- typedef std::vector<int> int_vec_t;
- typedef std::vector<std::string> str_vec_t;
- typedef std::vector<size_t> index_vec_t;
- class SequenceGen {
- public:
- SequenceGen (int start = 0) : current(start) { }
- int operator() () { return current++; }
- private:
- int current;
- };
- class Comp{
- int_vec_t& _v;
- public:
- Comp(int_vec_t& v) : _v(v) {}
- bool operator()(size_t i, size_t j){
- return _v[i] < _v[j];
- }
- }
- index_vec_t indices(3);
- std::generate(indices.begin(), indices.end(), SequenceGen(0));
- //indices are {0, 1, 2}
- int_vec_t Index = { 3, 1, 2 };
- str_vec_t Values = { "Third", "First", "Second" };
- std::sort(indices.begin(), indices.end(), Comp(Index));
- //now indices are {1,2,0}
Add Comment
Please, Sign In to add comment