| Topic: |
DEVELOP > c-Plus-Plus |
| User: |
"Reetesh Mukul" |
| Date: |
03 Feb 2008 01:41:12 AM |
| Object: |
Some problem in using references |
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[i]; 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[i]; //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;
}
.
|
|
| User: "Barry" |
|
| Title: Re: Some problem in using references |
03 Feb 2008 05:26:07 AM |
|
|
Reetesh Mukul wrote:
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[i]; 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".
--
Best Regards
Barry
.
|
|
|
| User: "Reetesh Mukul" |
|
| Title: Re: Some problem in using references |
03 Feb 2008 05:40:58 AM |
|
|
On Feb 3, 4:26 pm, Barry <dhb2...@gmail.com> wrote:
Reetesh Mukul wrote:
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[i]; 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".
--
Best Regards
Barry
Yes it is correct that T& cannot hold "tmp". But where it is happening
inside the code? Please explain further.
With Regards,
Reetesh Mukul
.
|
|
|
| User: "Reetesh Mukul" |
|
| Title: Re: Some problem in using references |
03 Feb 2008 05:44:06 AM |
|
|
On Feb 3, 4:40 pm, Reetesh Mukul <reetesh.mu...@gmail.com> wrote:
On Feb 3, 4:26 pm, Barry <dhb2...@gmail.com> wrote:
Reetesh Mukul wrote:
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[i]; 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".
--
Best Regards
Barry
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[i]; //line #1
The "key" is changing with arr[i], which I overlooked. Sorry again.
Regards,
Reetesh Mukul
.
|
|
|
|
|
|

|
Related Articles |
|
|