How can I make a function works for both array and vector parameter

I would like to pass both array and vector parameters in this function .
i.e. vector<string> &li

How could I adjust it? Thanks!

1
2
3
4
5
6
7
  void sort(int i,vector<string> &li){
	//sort alphabetically
	for(int y=0;y<i-1;y++){
		for(int j=y+1;j<i;j++)
			if (li[y]>li[j]) swap(li[y],li[j]);
		}
}
Last edited on
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
#include <iterator> // std::begin, std::end
#include <algorithm> // std::swap 

template < typename RANDOM_ACCESS_SEQUENCE > void sort( RANDOM_ACCESS_SEQUENCE& seq )
{
    const std::size_t sz = std::end(seq) - std::begin(seq) ; // http://en.cppreference.com/w/cpp/iterator/begin
    
    for( std::size_t i = 0 ; i < sz ; ++i )
    {
        for( std::size_t j = i+1 ; j < sz ; ++j )
        {
            using std::swap ;
            if( seq[j] < seq[i] ) swap( seq[j], seq[i] ) ;
        }
    }
}

http://coliru.stacked-crooked.com/a/abd8da8803d825bc
 
  void sort(int i ,vector<string> &li, string names[], int name_count)


IF the array names has the same number of items you don't need name_count.
Topic archived. No new replies allowed.