Some problem in using references

R

Reetesh Mukul

I wrote following code for insertion sort. The code was compiled using
gcc-4.1.1-30 on my fedora core 6 system. For this code output is not
correct {output is 2,5,76,76,76}. It seems, "line #2" is not working.
Meanwhile no problem occurs when
1. line #1 is changed to T key = arr; or
2. line #2 is changed to T& q = arr[j+1]; q = key.

Am I missing something ?

Regards,
Reetesh Mukul

-------------------------------------------------------------------------------------------------------------------

//insertion_sort.cpp
#include <iostream>
#include <algorithm>
#include <boost/lambda/lambda.hpp>
#include <boost/array.hpp>

using namespace boost;
using namespace boost::lambda;
using namespace std;

template<typename T, size_t N>void insertion_sort(array<T,N>& arr)
{
for(size_t i = 2; i < N ; ++i)
{
T& key = arr; //line #1
for(int j = i - 1; j >=0; --j)
{
if( key < arr[j] )
arr[j+1] = arr[j];
else{
arr[j+1] = key; //line #2
break;
}
}
}
}


int main()
{
array<int,5>a = {2,5,76,11,3};
insertion_sort(a);
for_each(a.begin(),a.end(),cout << _1<<" ");
return 0;
}
 
B

Barry

Reetesh said:
I wrote following code for insertion sort. The code was compiled using
gcc-4.1.1-30 on my fedora core 6 system. For this code output is not
correct {output is 2,5,76,76,76}. It seems, "line #2" is not working.
Meanwhile no problem occurs when
1. line #1 is changed to T key = arr; or


Are you sure "or" here is right?
2. line #2 is changed to T& q = arr[j+1]; q = key.

Am I missing something ?

You can never have a reference(T&) to hold a "tmp". Always use T as the
type of the "tmp".
 
R

Reetesh Mukul

Reetesh said:
I wrote following code for insertion sort. The code was compiled using
gcc-4.1.1-30 on my fedora core 6 system. For this code output is not
correct {output is 2,5,76,76,76}. It seems, "line #2" is not working.
Meanwhile no problem occurs when
1. line #1 is changed to T key = arr; or


Are you sure "or" here is right?
2. line #2 is changed to T& q = arr[j+1]; q = key.
Am I missing something ?

You can never have a reference(T&) to hold a "tmp". Always use T as the
type of the "tmp".


Yes it is correct that T& cannot hold "tmp". But where it is happening
inside the code? Please explain further.

With Regards,
Reetesh Mukul
 
R

Reetesh Mukul

Reetesh said:
I wrote following code for insertion sort. The code was compiled using
gcc-4.1.1-30 on my fedora core 6 system. For this code output is not
correct {output is 2,5,76,76,76}. It seems, "line #2" is not working.
Meanwhile no problem occurs when
1. line #1 is changed to T key = arr; or

Are you sure "or" here is right?
2. line #2 is changed to T& q = arr[j+1]; q = key.
Am I missing something ?
You can never have a reference(T&) to hold a "tmp". Always use T as the
type of the "tmp".

Yes it is correct that T& cannot hold "tmp". But where it is happening
inside the code? Please explain further.

With Regards,
Reetesh Mukul


Oh! I got it. Sorry for this innocent looking line,
T& key = arr; //line #1
The "key" is changing with arr, which I overlooked. Sorry again.

Regards,
Reetesh Mukul
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,781
Messages
2,569,619
Members
45,316
Latest member
naturesElixirCBDGummies

Latest Threads

Top