查看單個文章
舊 2003-12-21, 11:46 PM   #1
lin0821
註冊會員
榮譽勳章

勳章總數0
UID - 1344
在線等級: 級別:4 | 在線時長:40小時 | 升級還需:5小時級別:4 | 在線時長:40小時 | 升級還需:5小時級別:4 | 在線時長:40小時 | 升級還需:5小時級別:4 | 在線時長:40小時 | 升級還需:5小時
註冊日期: 2002-12-06
VIP期限: 2007-03
住址: 桃園
文章: 27
精華: 0
現金: 41 金幣
資產: 41 金幣
Unhappy 拜託!!請C++高手幫我解題!

最近學校出了個程式題,但C++白痴的我瞪著程式兩個鐘頭還是沒頭緒:blink: .
請高手幫我以註解的方式指導一下好嗎!感恩!!
程式如下:
------------------------------------------------------------------------------------
#include <iostream>
#include <string>
#include <vector>

using namespace std;

template <typename T>
T& findmin(const vector <T> &x)
{
int minIndex = 0;
for(int ix=1; ix < x.size(); ix++)
if(x[minIndex] > x[ix])
minIndex = ix;
return x[minIndex];
}

void display(const vector <int> &vec)
{
for (int ix=0; ix < vec.size(); ++ix)
cout<<vec[ix]<<',';
cout<<endl;
}

void bubble_sort(vector <int> &vec)
{
for(int ix = vec.size(); ix > 1; ix--)
for(int jx=0 ;jx < ix-1; jx++)
if(vec[jx] > vec[jx+1])
swap(vec[jx] ,vec[jx+1]);

}



void swap(int &x ,int &y)
{
int temp;
temp = x;
x = y;
y = temp;
}





int main()
{
int ia[8]={8,34,3,13,1,21,5,-3};
vector <int> vec(ia,ia+8);
cout<<"vector before sort"<<endl;
display(vec);
bubble_sort(vec);
cout<<"vector after sort"<<endl;
display(vec);
}
==============================================
==============================================
==============================================
==============================================
==============================================
==============================================
執行結果:
------------------------------------------------------------------------------------
vector before sort
8,34,3,13,1,21,5,-3,
vector after sort
-3,1,3,5,8,13,21,34,
Press any key to continue
==============================================
lin0821 目前離線  
送花文章: 1, 收花文章: 0 篇, 收花: 0 次
回覆時引用此帖