《C++面向物件程式設計》課程筆記 lessen8 程式示例
阿新 • • 發佈:2018-12-30
1 程式示例:vector
#include <iostream> #include <vector> using namespace std; template<class T> void PrintVector(T s, T e) { for(;s!=e;++s) cout << *s << " "; cout << endl; } int main() { int a[5] = {1,2,3,4,5}; vector<int> v(a,a+5); //將陣列a的元素新增到 v 中 cout << "1) " << v.end() - v.begin() << endl; //兩個隨機迭代器可以相減,輸出 1) 5 cout << "2) "; PrintVector(v.begin(),v.end()); //2) 1 2 3 4 5 v.insert(v.begin()+2,13); //在v.begin()+2 (v[2])位置插入13 cout << "3) "; PrintVector(v.begin(),v.end()); //3) 1 2 13 3 4 5 v.erase(v.begin()+2); //刪除位於 v.begin()+2 (v[2])的元素 cout << "4) "; PrintVector(v.begin(),v.end()); //4) 1 2 3 4 5 vector<int> v2 (4,100); //v2 有4個元素,都是100 v2.insert(v2.begin(),v.begin()+1,v.begin()+3); //將 v 的一段插入 v2 開頭 cout << "5) v2: "; PrintVector(v2.begin(),v2.end()); //5) v2: 2 3 100 100 100 100 v.erase(v.begin()+1,v.begin()+3); //刪除 v 上的一個區間,即 2 ,3 cout << "6) "; PrintVector(v.begin(),v.end()); //6) 1 4 5 system("pause"); return 0; }
2 用 vector 實現二維陣列
#include <iostream> #include <vector> using namespace std; int main() { vector<vector<int> > v(3); //兩個 >> 之間的空格必須要有,不然會被當成是右移符號 //v 有3個元素,每個元素都是 vector<int> 容器 for(int i=0;i<v.size();i++) for(int j=0;j<4;j++) v[i].push_back(j); for(int i=0;i<v.size();i++) { for(int j=0;j<v[i].size();j++) cout << v[i][j] << " "; cout << endl; } system("pause"); return 0; }
3 deque
- 所有使用於 vector 的操作都使用於 deque。
- deque 還有 push_front (將元素插入到前面)和 pop_front (刪除最前面的元素)操作,複雜度是 O(1)。
4 list 容器
- 在任何位置插入刪除都是常數時間,不支援隨機存取。
除了具有所有順序容器都有的成員函式之外,還支援8個成員函式:
push_front:在前面插入
pop_front:刪除前面的元素
sort:排序(list 不支援 STL 的演算法 sort)
remove:刪除和指定值相等的所有元素
unique:刪除所有和前一個元素相同的元素(要做到元素不重複,則 unique 之前還需要 sort
merge:合併兩個連結串列,並清空被合併的那個
reverse:顛倒連結串列
splice:在指定位置前面插入另一個連結串列中的一個或多個元素,並在另一連結串列中刪除被插入的元素。
#include <iostream>
#include <list>
#include <algorithm>
using namespace std;
class A
{
private:
int n;
public:
A(int n_) { n = n_; }
friend bool operator <(const A & a1, const A & a2);
friend bool operator ==(const A & a1, const A & a2);
friend ostream & operator <<(ostream & o, const A & a );
};
bool operator< (const A & a1, const A & a2)
{
return a1.n < a2.n;
}
bool operator ==(const A & a1, const A & a2)
{
return a1.n == a2.n;
}
ostream & operator <<(ostream & o, const A & a )
{
o << a.n;
return o;
}
template <class T>
void PrintList(const list<T> & lst) //不推薦的寫法,還是用兩個迭代器作為引數更好
{
typename list<T>::const_iterator i;
i = lst.begin();
for(i=lst.begin();i!=lst.end();i++)
cout << *i << ",";
cout << endl;
}//typename 用來說明 list<T>::const_iterator 是個型別。在 vs 中不寫也可以。
int main()
{
list<A> lst1,lst2;
lst1.push_back(1);
lst1.push_back(2);
lst1.push_back(3);
lst1.push_back(4);
lst1.push_back(2);
lst2.push_back(10);
lst2.push_front(20);
lst2.push_back(30);
lst2.push_back(30);
lst2.push_back(30);
lst2.push_front(40);
lst2.push_back(40);
cout << "1) ";
PrintList(lst1); //1) 1,2,3,4,2,
cout << "2) ";
PrintList(lst2); //2) 40,20,10,30,30,30,40,
lst2.sort();
cout << "3) ";
PrintList(lst2);//3) 10,20,30,30,30,40,40,
lst2.pop_front();
cout << "4) ";
PrintList(lst2); //4) 20,30,30,30,40,40,
lst1.remove(2);//刪除所有和A(2)相等的元素
cout << "5) ";
PrintList(lst1); //5) 1,3,4,
lst2.unique(); //刪除所有和前一個元素相等的元素
cout << "6) ";
PrintList(lst2); //6) 20,30,40,
lst1.merge(lst2); //合併 lst2 到 lst1 並清空 lst2
cout << "7) ";
PrintList(lst1);//7) 1,3,4,20,30,40,
cout << "8) ";
PrintList(lst2); //8)
lst1.reverse();
cout << "9) ";
PrintList(lst1);//9) 40,30,20,4,3,1,
lst2.push_back(100);
lst2.push_back(200);
lst2.push_back(300);
lst2.push_back(400);
list<A>::iterator p1,p2,p3;
p1 = find(lst1.begin(),lst1.end(),3);
p2 = find(lst2.begin(),lst2.end(),200);
p3 = find(lst2.begin(),lst2.end(),400);
lst1.splice(p1,lst2,p2,p3);
//將 [p2,p3) 插入 p1 之前,並從 lst2 中刪除 [p2,p3)
cout << "10) ";
PrintList(lst1); //10) 40,30,20,4,200,300,3,1,
cout << "11) ";
PrintList(lst2);//11) 100,400,
system("pause");
return 0;
}
5 函式物件
若一個類過載了運算子 “()” ,則該類的物件就成為函式物件。