test_reference.cpp 921 B

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455
  1. #include <iostream>
  2. #include <vector>
  3. #include <fstream>
  4. using namespace std;
  5. void display(const vector<int>& v, ostream& os = cout);
  6. void display(const vector<int>& v, ostream& os) {
  7. for (vector<int>::const_iterator iter = v.begin(); iter != v.end(); ++iter) {
  8. os << *iter << " ";
  9. }
  10. os << endl;
  11. }
  12. void swap(int& value_a, int& value_b) {
  13. int temp = value_a;
  14. value_a = value_b;
  15. value_b = temp;
  16. }
  17. void buble_sort(vector<int>& v, ostream* ofil=0) {
  18. vector<int>::iterator ix, iy;
  19. for (ix = v.begin(); ix != v.end(); ++ix) {
  20. for (iy = ix + 1; iy != v.end(); ++iy) {
  21. if (*ix > *iy) {
  22. if (ofil) {//need include #include <fstream>
  23. (*ofil) << *ix << " " << *iy << endl;
  24. }
  25. swap(*ix, *iy);
  26. }
  27. }
  28. }
  29. }
  30. int main() {
  31. vector<int> v = { 0, 1, 0,5,4,3 };
  32. display(v);
  33. //swap(v[0], v[1]);
  34. //display(v);
  35. ofstream fout("test.txt");
  36. buble_sort(v, &fout);
  37. display(v);
  38. return 0;
  39. }