Talk About Network

Google


Register and Login
Nick
Password
Register create new account Sign up is FREE and you can post replies, new topics, bookmark posts and more!
Recover lost password


Programming > C++ > Some problem in...
Latest [ Topics | Posts ] Archive Post A New Topic Post a Reply
<< Topic < Post Post 1 of 4 Topic 43067 of 48011
Post > Topic >>

Some problem in using references

by Reetesh Mukul <reetesh.mukul@[EMAIL PROTECTED] > Feb 2, 2008 at 11:41 PM

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;
}
 




 4 Posts in Topic:
Some problem in using references
Reetesh Mukul <reetesh  2008-02-02 23:41:12 
Re: Some problem in using references
Barry <dhb2000@[EMAIL   2008-02-03 19:26:07 
Re: Some problem in using references
Reetesh Mukul <reetesh  2008-02-03 03:40:58 
Re: Some problem in using references
Reetesh Mukul <reetesh  2008-02-03 03:44:06 

Post A Reply:
  Go here to Signup

AddThis Feed Button


About - Advertising - Contact - Frequently Asked Questions - Privacy Policy - Terms of Use - Signup

Contact
tan12V112 Mon Oct 13 4:00:00 CDT 2008.