代码:
#include <iostream>
#include <vector>
#include <stdlib.h>
#include <time.h>
using namespace std;
void Print(vector<int>MyVector)
{
vector<int>::iterator it = MyVector.begin();
for (;it != MyVector.end();++it)
{
cout << *it << " ";
}
cout << endl;
}
void Sort(vector<int> MyVector)
{
vector<int>::iterator it = MyVector.begin();
for (int i = 0;i < MyVector.size();++i)
{
it = MyVector.begin();
for (int j = 0;j < MyVector.size()-1;++j,++it)
{
if (*it > *(it + 1))
{
int temp = *it;
*it = *(it + 1);
*(it + 1) = temp;
}
}
}
Print(MyVector);
}
int main()
{
vector<int> MyVector;
srand(time(NULL));
for (int i = 0;i < 10;++i)
{
MyVector.push_back(rand() % 100);
}
cout << "MyVector.size()=" << MyVector.size() << endl;
Print(MyVector);
Sort(MyVector);
Print(MyVector);
return 0;
}