1. 程式人生 > 其它 >演算法中的STL(1)

演算法中的STL(1)

2022.03.14

    為了準備藍橋杯最近學習了演算法中的部分知識,今天學習了C++中的STL模板庫。

今天學習的內容有vector容器,其中常用的函式有

push_back():在容器的末尾新增一個數據

pop_back():彈出容器中一個數據

size():返回容器的大小

clear():清空容器

insert():在指定位置新增資料

erase():刪除指定位置的資料

下面是兩種vector容器的輸出方式:

vector<int> num;

int m;
cin >> m;
for (int i = 0; i < m; i++) {

int temp;
cin >> temp;


num.push_back(temp);

}
//vector的第一種輸出方式num[].
for (int i = 0; i < num.size(); i++) {

int temp = num[i];
cout << temp << endl;

}

sort(num.begin(), num.end());

for (int i = 0; i < num.size(); i++) {

int temp = num[i];
cout << temp << " 1" << endl;

}

//迭代器的輸出方式iterator


vector<int>::iterator it;
for (it = num.begin(); it != num.end(); it++) {

cout << *it << " 2" << endl;

}

 其中的迭代器相當與C++中的指標。