This is the mail archive of the
gcc@gcc.gnu.org
mailing list for the GCC project.
why cross out cout make result different?
- From: "eric lin" <fsshl at luxmail dot com>
- To: <gcc at gcc dot gnu dot org>
- Date: Fri, 2 Aug 2013 18:25:09 -0700
- Subject: why cross out cout make result different?
- Reply-to: <fsshl at luxmail dot com>
I have tried to copy QuickSort c++ programs:
-----------------------------------------------
#include <iostream>
using namespace std;
class Element
{
public:
int getKey() const { return key;};
void setKey(int k) { key=k;};
private:
int key;
// other fields
};
#define InterChange(list, i, j) t=list[j]; list[i]=list[j]; list[j]=t;
/*-------------------------------------------------------------------------------------*/
void QuickSort(Element list[], /* const */ int left, /*const */ int right)
// Sort records list[left], ..., list[right] into nondescreasing order on field key.
// Key pivot = list[left].key is arbitrarily chosen as the pivot key. Pointer i and j
// are used to partition the sublist so that at any time list[m].key <= pivot, m < i;
// and list[m].key >= pivot, m>j. It is assumed that list[left].key <=list[right+1].key.
{
Element t;
if (left<right) {
int i = left,
j=right+1,
pivot=list[left].getKey();
do {
do i++; while(list[i].getKey() < pivot);
do j--; while(list[j].getKey() > pivot);
if (i<j) InterChange(list, i, j);
} while(i<j);
InterChange(list, left, j);
cout << "---show bankaccount1[0]= " << list[0].getKey() << " bankaccount1[1]= " << list[1].getKey() << " bankaccount1[7]= " << list[7].getKey() << " its left= " << left << endl;
QuickSort(list, left, j-1);
QuickSort(list, j+1, right);
}
}
/*--------------------------------------------------------------------------------------------*/
int main() {
Element bankaccount1[10];
int l1, r1;
bankaccount1[0].setKey(26);
bankaccount1[1].setKey(5);
bankaccount1[2].setKey(37);
bankaccount1[3].setKey(1);
bankaccount1[4].setKey(61);
bankaccount1[5].setKey(11);
bankaccount1[6].setKey(59);
bankaccount1[7].setKey(15);
bankaccount1[8].setKey(48);
bankaccount1[9].setKey(19);
l1=0;
r1=9;
for (int i=0; i<10; i++)
cout << bankaccount1[i].getKey() << " " ;
cout << endl;
QuickSort(bankaccount1, l1, r1);
for (int i=0; i<10; i++)
cout << bankaccount1[i].getKey() << " " ;
cout << endl;
return 0;
}
/*---------------------------------------------------------------------------------*/
if I (or you) commnet out cout show bankaccount1 that line, it will show different results
both result s are not what I expected(accroding to books)
I am in 4.6.1
_____________________________________________________________
Luxmail.com