참고사이트: http://bcbjournal.org/articles/vol3/9910/Using_STL_containers.htm
예제 소스
#include <vcl.h>
#include <stdlib>
#include <vector>
#include <list>
#include <map>
#include <string>
#pragma hdrstop
#include "Unit1.h"
#include "WrkObjs.h"
using namespace std;
#pragma package(smart_init)
#pragma resource "*.dfm"
TForm1 *Form1;
__fastcall TForm1::TForm1(TComponent* Owner)
: TForm(Owner)
{
}
void __fastcall
TForm1::QuitButtonClick(TObject *Sender)
{
exit(0);
}
void __fastcall
TForm1::VectorButtonClick(TObject *Sender)
{
vector<int> myInts;
ListBox1->Clear();
ListBox1->Items->Add("Vector Example");
ListBox1->Items->Add("==============");
// Add numbers 1-9 to myInts
for( int y = 1; y < 10; y++ )
myInts.push_back(y);
// Add them to ListBox1 using array subscripting
ListBox1->Items->Add(
"After adding 1 through 9:");
for(int y = 0; y < 9; y++)
ListBox1->Items->Add(String(myInts[y]));
// Add 10 to the end of the vector
myInts.push_back(10);
// Insert a 0 at the front of the list
myInts.insert(myInts.begin(), 0);
// Show the contents of the vector
ListBox1->Items->Add("After adding 0 and 10:");
for( int y = 0; y < 11; y++ )
ListBox1->Items->Add(String(myInts[y]));
// For practice, remove the 10 from the end
// and place a 25 in the there.
myInts.pop_back();
myInts.push_back(25);
// Show the contents of the vector again
ListBox1->Items->
Add("After deleting 10 and adding 25:");
for( int y = 0; y < 11; y++ )
ListBox1->Items->Add(String(myInts[y]));
myInts.clear();
ListBox1->Items->Add(
"Vector example complete!");
}
//----------------------------------
void __fastcall
TForm1::ListButtonClick(TObject *Sender)
{
list<wisdom*> myWisdom;
list<wisdom*>::iterator wisIt;
char sentence[50];
char* myWords[] = {
"a ", "nice ", "day ", "Have "};
ListBox1->Clear();
ListBox1->Items->Add("List Example");
ListBox1->Items->Add("============");
// Add all words to the list from the array.
for( int x = 0; x < 4; x++ ) {
char* wordMemory =
new char[strlen(myWords[x]+1)];
strcpy(wordMemory, myWords[x]);
myWisdom.push_back(new Wisdom(wordMemory));
}
// Form a sentence
ListBox1->Items->Add("My Words of wisdom:");
*sentence = NULL;
for(wisIt = myWisdom.begin();
wisIt != myWisdom.end(); wisIt++)
strcat(sentence, (*wisIt)->say());
ListBox1->Items->Add(sentence);
// Move "Have" to the beginning of the sentence
wisIt = myWisdom.end();
Wisdom* tmpWis = (*(--wisIt));
myWisdom.remove(tmpWis);
myWisdom.push_front(tmpWis);
ListBox1->Items->
Add("My Words of wisdom (corrected):");
*sentence = NULL;
// Rebuild the sentence again and delete the
// Wisdom objects as we go.
for(list<wisdom*<::iterator li =
myWisdom.begin();
li != myWisdom.end(); li++) {
strcat(sentence, (*li)->say());
delete (*li);
}
ListBox1->Items->Add(sentence);
}
void __fastcall
TForm1::MapButtonClick(TObject *Sender)
{
map<string, person=""> anyBody;
map<string, person="">::iterator persIter;
ListBox1->Clear();
ListBox1->Items->Add("Multimap Example");
ListBox1->Items->Add("================");
// Add some people to the map
anyBody["honest"] = Person("Abe Lincoln", 98);
anyBody["cherry tree"] =
Person("George Washington", 100);
anyBody["watergate"] =
Person("Richard Nixon", 74);
anyBody["pardon"] = Person("Gerald Ford", 76);
anyBody["peanuts"] = Person("Jimmy Carter", 80);
anyBody["scandal"] = Person("Bill Clinton", 92);
// Find a couple of people and show them
Person who = anyBody["honest"];
ListBox1->Items->Add((who.getName()).c_str());
who = anyBody["watergate"];
ListBox1->Items->Add((who.getName()).c_str());
// Delete Gerald Ford by calling erase function
anyBody.erase("pardon");
// The find function is a better way to look
// for entries in the list.
persIter = anyBody.find("pardon");
if (persIter != anyBody.end())
ListBox1->Items->Add((who.getName()).c_str());
else
ListBox1->Items->Add("No such president!");
}
|
LIST
'Borland' 카테고리의 다른 글
| Installing LEADTOOLS VCL(Visual Component Library) (0) | 2014.07.15 |
|---|---|
| SetWindowPos() 함수를 이용해서 Dialog가 뒤로 사라지는 문제 수정 (0) | 2014.04.29 |
| 사각형 회전후 좌표 값 구하기 (0) | 2012.09.10 |
| Inserting Images into PowerPoint 2007 (0) | 2011.04.09 |
| The new and delete Operators with multi-dimensional arrays (다차배열) (0) | 2011.01.18 |