Vlad from Moscow
2013-06-24 21:05:40 UTC
In most cases standard algorithms (and some containers' member functions)
std::sort and std:;reverse are called over a whole container. For example
[code]
std::forward_list<int> f;
// filling the list
f.sort();
// or
f.sort( std::greater<int>() );
std::vector<int> v;
//filling the vector
std::sort( v.begin(), v.end() );
// or
std::sort( v.begin(), v.end(), std::greater<int>() );
[/code]
So I suggest to introduce general functions std::reverse and std::sort that
will accept the reference to a container.
Below a demostrative example that illustrates the proposal
[code]
#include <iostream>
#include <algorithm>
#include <iterator>
#include <functional>
#include <vector>
#include <forward_list>
#include <list>
namespace N1 // std
{
template <typename T>
void reverse( T &c )
{
std::reverse( std::begin( c ), std::end( c ) );
}
template <typename T, typename Allocator>
void reverse( std::forward_list<T, Allocator> &f )
{
f.reverse();
}
template <typename T, typename Allocator>
void reverse( std::list<T, Allocator> &l )
{
l.reverse();
}
template <typename T>
void sort( T &c )
{
std::sort( std::begin( c ), std::end( c ) );
}
template <typename T, typename Allocator>
void sort( std::forward_list<T, Allocator> &f )
{
f.sort();
}
template <typename T, typename Allocator>
void sort( std::list<T, Allocator> &l )
{
l.sort();
}
template <typename T, typename Compare>
void sort( T &c, Compare comp )
{
std::sort( std::begin( c ), std::end( c ), comp );
}
template <typename T, typename Allocator, typename Compare>
void sort( std::forward_list<T, Allocator> &f, Compare comp )
{
f.sort( comp );
}
template <typename T, typename Allocator, typename Compare>
void sort( std::list<T, Allocator> &l, Compare comp )
{
l.sort( comp );
}
} // end of N1
int main()
{
int a[3] = { 1, 2, 3 };
std::forward_list<int> f( std::begin( a ), std::end( a ) );
std::list<int> l( std::begin( a ), std::end( a ) );
std::vector<int> v( std::begin( a ), std::end( a ) );
std::cout << "a:\t";
for ( int x : a ) std::cout << x << ' ';
std::cout << std::endl;
std::cout << "f:\t";
for ( int x : f ) std::cout << x << ' ';
std::cout << std::endl;
std::cout << "l:\t";
for ( int x : l ) std::cout << x << ' ';
std::cout << std::endl;
std::cout << "v:\t";
for ( int x : v ) std::cout << x << ' ';
std::cout << std::endl;
N1::reverse( a );
N1::reverse( f );
N1::reverse( l );
N1::reverse( v );
std::cout << "a:\t";
for ( int x : a ) std::cout << x << ' ';
std::cout << std::endl;
std::cout << "f:\t";
for ( int x : f ) std::cout << x << ' ';
std::cout << std::endl;
std::cout << "l:\t";
for ( int x : l ) std::cout << x << ' ';
std::cout << std::endl;
std::cout << "v:\t";
for ( int x : v ) std::cout << x << ' ';
std::cout << std::endl;
N1::sort( a );
N1::sort( f );
N1::sort( l );
N1::sort( v );
std::cout << "a:\t";
for ( int x : a ) std::cout << x << ' ';
std::cout << std::endl;
std::cout << "f:\t";
for ( int x : f ) std::cout << x << ' ';
std::cout << std::endl;
std::cout << "l:\t";
for ( int x : l ) std::cout << x << ' ';
std::cout << std::endl;
std::cout << "v:\t";
for ( int x : v ) std::cout << x << ' ';
std::cout << std::endl;
N1::sort( a, std::greater<int>() );
N1::sort( f, std::greater<int>() );
N1::sort( l, std::greater<int>() );
N1::sort( v, std::greater<int>() );
std::cout << "a:\t";
for ( int x : a ) std::cout << x << ' ';
std::cout << std::endl;
std::cout << "f:\t";
for ( int x : f ) std::cout << x << ' ';
std::cout << std::endl;
std::cout << "l:\t";
for ( int x : l ) std::cout << x << ' ';
std::cout << std::endl;
std::cout << "v:\t";
for ( int x : v ) std::cout << x << ' ';
std::cout << std::endl;
}
[/code]
std::sort and std:;reverse are called over a whole container. For example
[code]
std::forward_list<int> f;
// filling the list
f.sort();
// or
f.sort( std::greater<int>() );
std::vector<int> v;
//filling the vector
std::sort( v.begin(), v.end() );
// or
std::sort( v.begin(), v.end(), std::greater<int>() );
[/code]
So I suggest to introduce general functions std::reverse and std::sort that
will accept the reference to a container.
Below a demostrative example that illustrates the proposal
[code]
#include <iostream>
#include <algorithm>
#include <iterator>
#include <functional>
#include <vector>
#include <forward_list>
#include <list>
namespace N1 // std
{
template <typename T>
void reverse( T &c )
{
std::reverse( std::begin( c ), std::end( c ) );
}
template <typename T, typename Allocator>
void reverse( std::forward_list<T, Allocator> &f )
{
f.reverse();
}
template <typename T, typename Allocator>
void reverse( std::list<T, Allocator> &l )
{
l.reverse();
}
template <typename T>
void sort( T &c )
{
std::sort( std::begin( c ), std::end( c ) );
}
template <typename T, typename Allocator>
void sort( std::forward_list<T, Allocator> &f )
{
f.sort();
}
template <typename T, typename Allocator>
void sort( std::list<T, Allocator> &l )
{
l.sort();
}
template <typename T, typename Compare>
void sort( T &c, Compare comp )
{
std::sort( std::begin( c ), std::end( c ), comp );
}
template <typename T, typename Allocator, typename Compare>
void sort( std::forward_list<T, Allocator> &f, Compare comp )
{
f.sort( comp );
}
template <typename T, typename Allocator, typename Compare>
void sort( std::list<T, Allocator> &l, Compare comp )
{
l.sort( comp );
}
} // end of N1
int main()
{
int a[3] = { 1, 2, 3 };
std::forward_list<int> f( std::begin( a ), std::end( a ) );
std::list<int> l( std::begin( a ), std::end( a ) );
std::vector<int> v( std::begin( a ), std::end( a ) );
std::cout << "a:\t";
for ( int x : a ) std::cout << x << ' ';
std::cout << std::endl;
std::cout << "f:\t";
for ( int x : f ) std::cout << x << ' ';
std::cout << std::endl;
std::cout << "l:\t";
for ( int x : l ) std::cout << x << ' ';
std::cout << std::endl;
std::cout << "v:\t";
for ( int x : v ) std::cout << x << ' ';
std::cout << std::endl;
N1::reverse( a );
N1::reverse( f );
N1::reverse( l );
N1::reverse( v );
std::cout << "a:\t";
for ( int x : a ) std::cout << x << ' ';
std::cout << std::endl;
std::cout << "f:\t";
for ( int x : f ) std::cout << x << ' ';
std::cout << std::endl;
std::cout << "l:\t";
for ( int x : l ) std::cout << x << ' ';
std::cout << std::endl;
std::cout << "v:\t";
for ( int x : v ) std::cout << x << ' ';
std::cout << std::endl;
N1::sort( a );
N1::sort( f );
N1::sort( l );
N1::sort( v );
std::cout << "a:\t";
for ( int x : a ) std::cout << x << ' ';
std::cout << std::endl;
std::cout << "f:\t";
for ( int x : f ) std::cout << x << ' ';
std::cout << std::endl;
std::cout << "l:\t";
for ( int x : l ) std::cout << x << ' ';
std::cout << std::endl;
std::cout << "v:\t";
for ( int x : v ) std::cout << x << ' ';
std::cout << std::endl;
N1::sort( a, std::greater<int>() );
N1::sort( f, std::greater<int>() );
N1::sort( l, std::greater<int>() );
N1::sort( v, std::greater<int>() );
std::cout << "a:\t";
for ( int x : a ) std::cout << x << ' ';
std::cout << std::endl;
std::cout << "f:\t";
for ( int x : f ) std::cout << x << ' ';
std::cout << std::endl;
std::cout << "l:\t";
for ( int x : l ) std::cout << x << ' ';
std::cout << std::endl;
std::cout << "v:\t";
for ( int x : v ) std::cout << x << ' ';
std::cout << std::endl;
}
[/code]
--
---
You received this message because you are subscribed to the Google Groups "ISO C++ Standard - Future Proposals" group.
To unsubscribe from this group and stop receiving emails from it, send an email to std-proposals+***@isocpp.org.
To post to this group, send email to std-***@isocpp.org.
Visit this group at http://groups.google.com/a/isocpp.org/group/std-proposals/.
---
You received this message because you are subscribed to the Google Groups "ISO C++ Standard - Future Proposals" group.
To unsubscribe from this group and stop receiving emails from it, send an email to std-proposals+***@isocpp.org.
To post to this group, send email to std-***@isocpp.org.
Visit this group at http://groups.google.com/a/isocpp.org/group/std-proposals/.