拜託!!請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
==============================================
|