Created
May 3, 2013 04:51
-
-
Save kw0006667/5507271 to your computer and use it in GitHub Desktop.
C++ STL list 的簡單使用
將實體丟入list 後,即使在 list 中移除,原來的實體還是會在
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include <iostream> | |
#include <list> | |
#include <algorithm> | |
#include <string> | |
using namespace std; | |
class Sprite | |
{ | |
public: | |
Sprite(); | |
Sprite(const char* name); | |
~Sprite(); | |
const char* GetName(); | |
private: | |
const char* m_spriteName; | |
}; | |
Sprite::Sprite() | |
{ | |
} | |
Sprite::Sprite(const char* name) | |
{ | |
this->m_spriteName = name; | |
} | |
Sprite::~Sprite() | |
{ | |
} | |
const char* Sprite::GetName() | |
{ | |
return this->m_spriteName; | |
} | |
void printName(Sprite* sp) | |
{ | |
cout << sp->GetName() << endl; | |
} | |
int main () | |
{ | |
list<Sprite*> alive; | |
list<Sprite*>::iterator alive_it; | |
list<Sprite*> dead; | |
list<Sprite*>::iterator dead_it; | |
// 產生三個實體 | |
Sprite* back = new Sprite("I'm Back"); | |
Sprite* middle = new Sprite("I'm Middle"); | |
Sprite* front = new Sprite("I'm Front"); | |
// 使用 iterator,指到開頭 | |
alive_it = alive.begin(); | |
dead_it = dead.begin(); | |
// 從後面丟 | |
alive.push_back(back); // ->back<- | |
// 從前面丟 | |
alive.push_front(middle); // ->middle->back | |
alive.push_front(front); // ->front->middle->back | |
// front() or back() 只抓值 | |
cout << alive.front()->GetName() << endl; | |
cout << alive.back()->GetName() << endl; | |
cout << "=======================================" << endl; | |
// 使用 for_each迴圈不需要指位器,需 #include <algorithm> | |
for_each(alive.begin(), alive.end(), printName); | |
//for ( list<Sprite*>::iterator it = alive.begin(); it != alive.end(); it++) | |
//{ | |
// cout << (*it)->GetName() << endl; | |
//} | |
cout << "======================================" << endl; | |
// pop_front() or pop_back() 將實體從 list 內移除 | |
// 但原來的實體還在 | |
//alive.pop_front(); // ->middle->back | |
//alive.pop_back(); // ->middle<- | |
// 指定要移除的物件,該實體物件還是會存在,只是從 list 裡消失 | |
alive.remove(back); | |
// 加回去 | |
alive.push_back(back); | |
// 使用 insert 將 alive 的最後一個加到 dead 的第一個 | |
// 也可以使用 iterator | |
//dead.insert(dead.begin(), alive.back()); | |
dead.insert(dead_it, alive.back()); | |
cout << dead.front()->GetName() << endl; | |
//================================================ | |
cout << "===============================" << endl; | |
// 不宣告變數而直接產生實體的 list | |
list<Sprite*> temp; | |
list<Sprite*>::iterator temp_it; | |
temp_it = temp.begin(); | |
temp.push_front(new Sprite("1")); | |
temp.push_front(new Sprite("2")); | |
// 將值取出後,即使最後被 pop,remove 或是 clear | |
// 此值還是會在 | |
Sprite* clone = *(temp.begin()); | |
// 印出 list 的內容 | |
for_each(temp.begin(), temp.end(), printName); | |
// 移除所有的物件 | |
alive.clear(); | |
dead.clear(); | |
temp.clear(); | |
system("pause"); | |
return 0; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment