33,311
社区成员
发帖
与我相关
我的任务
分享
void display(const vector <int>&, ostream &= cout);
#include <iostream>
#include <vector>
#include <fstream>
#include "NumericSeq.h"
using namespace std;
void display(const vector<int> &vec, ostream &os)
{
for(int ix = 0; ix < vec.size(); ++ix)
os<<vec[ix]<<' ';
os<<endl;
}
void swap(int &val1, int &val2)
{
int temp = val1;
val1 = val2;
val2 = temp;
}
void bubble_sort(vector<int> &vec, ofstream *ofil = 0)
{
for(int ix = 0; ix < vec.size(); ++ix)
for(int jx = ix+1; jx < vec.size(); ++jx)
if(vec[ix] > vec[jx])
{
if(ofil != 0)
{
(*ofil)<<"about to call swap!"
<<" ix: "<<ix<<" jx: "<<jx<<'\t'
<<" swapping: "<<vec[ix]
<<" with "<<vec[jx]<<endl;
}
swap(vec[ix], vec[jx]);
}
}
int main()
{
int ia[8] = {8, 34, 3, 13, 1, 21, 5, 2};
vector<int> vec(ia, ia+8);
cout<<"vector before sort: ";
display(vec);
bubble_sort(vec);
cout<<"vector after sort: ";
display(vec);
ofstream ofil("data.txt");
bubble_sort(vec, &ofil);
display(vec, ofil);
return 0;
}
#pragma once
#include <vector>
using namespace std;
void display(const vector <int>&, ostream &= cout);
这一行代码