C++ vector 容器淺析[03] ~ 排序

C++ vector 容器淺析[03] ~ 排序

C++ vector 容器淺析[03] ~ 排序


資料來源: https://www.runoob.com/w3cnote/cpp-vector-container-analysis.html

純C++線上編譯測試: https://www.tutorialspoint.com/compile_cpp_online.php

純C 線上編譯測試: https://www.tutorialspoint.com/compile_c_online.php


#include <string>
#include <vector>
#include <iostream>
#include <algorithm>
using namespace std;

int main()
{
    vector<int>obj;

    obj.push_back(1);
    obj.push_back(3);
    obj.push_back(0);
	
	cout<<"init:"<<endl;
    for(int i=0;i<obj.size();i++)
    {
        cout<<obj[i]<<",";  
    } 
	cout<<"\n"<<endl;
	
    sort(obj.begin(),obj.end());//从小到大

    cout<<"从小到大:"<<endl;
    for(int i=0;i<obj.size();i++)
    {
        cout<<obj[i]<<",";  
    } 

    cout<<"\n"<<endl;

    cout<<"从大到小:"<<endl;
    reverse(obj.begin(),obj.end());//从大到小 
    for(int i=0;i<obj.size();i++)
    {
        cout<<obj[i]<<",";
    }
    return 0;
}

發表迴響

你的電子郵件位址並不會被公開。 必要欄位標記為 *