How can I sort a vector?

I wrote this to sort a vector using a template, but it won't work correctly. When I include the return a line, it won't compile at all.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
#include <iostream>
#include <vector>
#include <fstream>
#include <string>
#include <iomanip>
using namespace std;

template<typename Obj>

const Obj & putOrder(vector<Obj> a)   //Mutator since it doesn't define a as const
      {
  int i=0, j=0;
  vector<Obj> temp(1);

  for(i=0; i<a.size(); ++i)
  {
    for(j=0; j<a.size()-1; j++)
    {
      if(a[j++] < a[j])
       {
          temp[0] = a[j];
          a[j] = a[j++];
          a[j++] = temp[0];    
      
       }              
    }                             
  }  
  return a;      
      }
      
      int main(){
  int x;
  vector <int> v(5);
  v[0] = 7;
  v[1] = 8;
  v[2] = 5;
  v[3] = 15;
  v[4] = 2;
  cout << "Unsorted: " << endl;
  for(x=0; x<v.size(); x++)
  cout << v[x] << endl;       
          
  cout << "Sorted: " << endl;
putOrder(v);        
   for(x=0; x < v.size(); x++){
  cout << v[x] << endl;        
       }
      system("pause");    
          return 0;
          }


When I try to compile this, Dev-C++ says
1
2
3
4
5
 C:\Users\Matthew\Desktop\study2.cpp In function `const Obj& putOrder(std::vector<Obj, std::allocator<_CharT> >) [with Obj = int]': 

44 C:\Users\Matthew\Desktop\study2.cpp   instantiated from here 

28 C:\Users\Matthew\Desktop\study2.cpp invalid initialization of reference of type 'const int&' from expression of type 'std::vector<int, std::allocator<int> >'  
You say that you would return an int (that you never catch, btw) but you are returning a vector.
Change your prototype to
1
2
template <class Obj>
void putOrder(vector<Obj> &a) //we will modify the object passed. Don't know what to return 


You could simply do std::sort(v.begin(), v.end());
1
2
3
4
5
6
7
8
      if(a[j++] < a[j]) //undefined behaviour
       {
          temp[0] = a[j];
          a[j] = a[j++]; //undefined behaviour
          a[j++] = temp[0];    
      
       }
//you increment j a lot. May access outside bounds 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
#include <iostream>
#include <vector>
#include <fstream>
#include <string>
#include <iomanip>
using namespace std;

template<typename Obj>

void putOrder(vector<Obj>& a)   //Mutator since it doesn't define a as const // 1., 2.
      {
  int i=0, j=0;
  Obj temp;//3.

  for(i=0; i<a.size(); ++i)
  {
    for(j=0; j<a.size()-1; j++)
    {
      if(a[j+1] < a[j])//4.a
       {
          temp = a[j];
          a[j] = a[j+1];//4.b
          a[j+1] = temp;    //4.c
      
       }              
    }                             
  }  
      }
      
      int main(){
  int x;
  vector <int> v(5);
  v[0] = 7;
  v[1] = 8;
  v[2] = 5;
  v[3] = 15;
  v[4] = 2;
  cout << "Unsorted: " << endl;
  for(x=0; x<v.size(); x++)
  cout << v[x] << endl;       
          
  cout << "Sorted: " << endl;
putOrder(v);        
   for(x=0; x < v.size(); x++){
  cout << v[x] << endl;        
       }
      system("pause");    
          return 0;
          }

1. , 2. since you dont assign the return value to anything, it should be like this, a void function with a reference input
3. why make it a vector of 1 when it can be a single value!?
4.a,b,c what you did is increment it every time, so after that you would have j+3, when you need just j, and don't get me started on what it would couse in the sorting itself...
So, when you make you code as I improved it, it should wokt, although I haven't realy tested it.
EDIT: I tested it, it works!
Last edited on
@ne555:I explained and fixed all of those bugs and more!
Thanks a lot! I know some of the things in my original code were stupid, but I had tried things like Obj temp before and couldn't get it to work, and I was stumped. Your original edited code you posted would not error, but it would not sort either. Is this because the function was defined as const? Thanks again.

Also I can't believe I set it to j++ instead of j+1. Guess I forgot that ++ is the same as j+=1. Wow.
Last edited on
Topic archived. No new replies allowed.