///////////////////////////////////////////////////////////////////////////////
//STL( list )
//이중 링크드 리스트처럼 임의의 위치에 삽입과 삭제가 빠름
//임의의 위치에 엑세스 할 수 없으며, 순차적으로 엑세스 하여 검색할 수 있다.
//순차 컨테이너
///////////////////////////////////////////////////////////////////////////////
#include <iostream>
#include <list>
using namespace std;
void main()
{
list<int> List;
list<int>::iterator it;
List.push_back(10);
List.push_back(9);
List.push_back(8);
List.push_back(7);
List.push_back(6);
cout << "List크기" << List.size() << endl;
for( it = List.begin(); it != List.end(); it++ )
{
cout << *it << endl;
}
List.push_front(11);
List.push_front(12);
cout << "List크기" << List.size() << endl;
for( it = List.begin(); it != List.end(); it++ )
{
cout << *it << endl;
}
}
///////////////////////////////////////////////////////////////////////////////
//STL( list )
//list를 이용하여 링크드 리스트 구현하기.
///////////////////////////////////////////////////////////////////////////////
#include <iostream>
#include <list>
#include <algorithm>
using namespace std;
enum { INPUT = 1, DELETE, OUTPUT, EXIT };
void main()
{
int select;
int val;
list<int> list1;
list<int>::iterator it;
while(1)
{
cout << "선택하세요( [1]:입력, [2]:삭제, [3]:출력, [4]:나가기 ) : ";
cin >> select;
switch( select )
{
case INPUT:
cout << "입력할 값을 입력하세요 : ";
cin >> val;
list1.push_back(val);
break;
case DELETE:
cout << "삭제할 값을 입력하세요 : ";
cin >> val;
it = find( list1.begin(), list1.end(), val ); //입력한 값을 찾아서 이터레이터를 리턴.
list1.erase(it);
break;
case OUTPUT:
for( it = list1.begin(); it != list1.end(); it++ )
{
cout << *it << endl;
}
break;
case EXIT:
cout << "Bye~" << endl;
return;
}
}
}
///////////////////////////////////////////////////////////////////////////////
//STL( list )
//구조체 변수를 사용하기.
///////////////////////////////////////////////////////////////////////////////
#include <iostream>
#include <list>
using namespace std;
typedef struct tag_Number {
char name[10];
int num;
}NUMBER;
NUMBER *pNum = NULL;
list<NUMBER*> list1;
list<NUMBER*>::iterator it;
void Input();
void Output();
void main()
{
Input();
Output();
delete pNum;
}
void Input()
{
int sel;
while(1)
{
cout << "[1] : 입력, [5] : 나가기";
cin >> sel;
if( sel == 5 ) break;
else if( sel == 1 )
{
pNum = new NUMBER;
cout << "이름을 입력하세요 : ";
cin >> pNum->name;
cout << "전화번호를 입력하세요 : ";
cin >> pNum->num;
list1.push_back( pNum ); //리스트가 동적으로 할당하는 pNum을 가지고 있는다.
//구조체를 가지게 하지말고 주소값을 가지게 할것.
}
}
}
void Output()
{
for( it = list1.begin(); it != list1.end(); it++ )
{
cout << (*it)->name << endl;
cout << (*it)->num << endl;
}
}
'Programming > STL' 카테고리의 다른 글
map - map의 index에 의해 정렬되어 값이 출력 (0) | 2010.11.16 |
---|---|
set - 랜덤하게 값을 집어 넣으면서 중복값을 빼고 소팅 (0) | 2010.11.16 |
stack (0) | 2010.11.16 |
vector - 가변길이의 순차적인 배열에 대한 임의의 접근 가능 (0) | 2010.11.16 |