vector是動態可變陣列,可以新增int、double、自定義的類
1.int示例:
3.自定義類#incldue
vector
a;a.push_back(1);
a.push_back(2);
a.push_back(3);
for(vector
::iterator iter = a.begin();iter != a.end(); ++iter)
for(int i=0;icout
<< a[i]《使用iterator和使用下標效果是一樣的。
2.基本資料型別
使用物件名取位址,加入vector中
vector
a;string str = "hello";
a.push_back(&str);
上面的方法有個弊端,就是必須寫不同的物件名,才能區分物件。如果想要在迴圈裡不斷新建和消除物件,就不方便。
解決方法:
假設我們有個類叫test_class,想要每次新建乙個類,構造函式引數分別為x,y,並加入叫a的vector裡
vector
a;//該類的指標
test_class *tes = null;//宣告乙個test_class型別的指標
tes = new test_class(x,y);//申請一塊新的記憶體空間,並初始化該物件,指標指向該物件
a.push_back(tes);
//在用到該物件時,應該使用指標的->,而不是用物件名.
tes->x;//✔
tes.x//✖
Behavioral模式之Iterator模式
提供一種方法順序訪問乙個集合物件中各個元素,而不需要暴露該物件的內部表示。cursor 游標 乙個聚合物件,如列表 list 應該提供一種方法來讓別人可以訪問它的元素,而又不需要暴露它的內部結構。資料庫中最常用的iterator模式。以下情況使用iterator模式 顧名思義,迭代器模式就是順序訪問...
c ,vector的 和at區別
std vector a std cout 上面輸出是0.容器初始化什麼都不做,大小為0 std vector a std couta.at 0 1 下標賦值會顯示sigsegv段錯誤,越界錯誤.at賦值會顯示terminate called after throwing an instance o...
ES6 新內容(二)Proxy和Iterator
1.proxy和reflect攔截 2.iterator遍歷器 proxy 攔截作用。var proxy new proxy target,handler proxy.revocable方法返回乙個可取消的proxy例項。在 proxy 的情況下,目標物件內部的this關鍵字會指向 proxy 修改...