C++使用STL对随机生成的向量各元素进行排序

#include #include #include #include #includeusing namespace std; void Display(vector&V); int main(void) { srand(time(NULL)); vector V(10); for (int i = 0; i < 10; i++) { V[i] = rand() % 100; //随机生成向量的每个分量 } cout << "排序前:" << endl; Display(V); sort(V.begin(), V.end()); //调用sort算法排序 cout << "排序後:" << endl; Display(V); system("pause"); return 0; }void Display(vector&V) { vector::const_iterator iter; for (iter = V.begin(); iter != V.end(); iter++) { cout << *iter << " "; } cout << endl; }

C++使用STL对随机生成的向量各元素进行排序
文章图片
输出.PNG 本程序在visual studio 2017 下编译运行成功

    推荐阅读