ref:http://blog.naver.com/wono77/140041557617
#include <stdio.h>
#include <list>
#include <iostream>
#include <algorithm> //find()를 위해 필요
using namespace std;
//STL LIST 테스트를 위한 함수
void stl()
{
list<char> list1;
//리스트의 반복을 가리키는 반복자 선언
list<char>::iterator itor;// = list1.begin(); // 따로, 또는 이렇게 ..
itor=list1.begin(); //시작을 가리키도록 한다.
//이 부분이 없으면 동작안함.
//-----------------------------------------
//값 삽입
//-----------------------------------------
cout<< "--- c, b, a 값 삽입 ---" << endl;
list1.push_front('c');
list1.push_back('b');
list1.push_back('a');
//처음부터 끝까지 값 출력
for(itor=list1.begin(); itor != list1.end(); itor++)
{
cout<< *itor << endl;
}
//-----------------------------------------
//포인터 rewind 연습
//-----------------------------------------
/*
itor=list1.begin(); //앞으로 다시 감기
//끝에 도달할때까지 데이터 출력
while(list1.end()!=itor)
{
cout<< *itor << endl;
itor++;
}
*/
//-----------------------------------------
//find -처음부터 끝까지 뒤져서 index를 던지고 있나 본다.
//단 include <algorithm>
//반환값은 반복자, 즉 *itor이다.
//-----------------------------------------
itor=find(list1.begin(), list1.end(), 'a');
cout<< "----- find (a를 찾아 그 위치에 d를 삽입한다.) ----" << endl;
//찾아낸 값이 있는 앞 위치에 집어 넣기
//없으면 맨 뒤에 들어간다.
list1.insert(itor,'d');
//처음부터 끝까지 값 출력
for(itor=list1.begin(); itor != list1.end(); itor++)
{
cout<< *itor << endl;
}
//-----------------------------------------
//소팅
//-----------------------------------------
cout<< "----- sorting ----- " << endl;
list1.sort();
//처음부터 끝까지 값 출력
for(itor=list1.begin(); itor != list1.end(); itor++)
{
cout<< *itor << endl;
}
//-----------------------------------------
//b 삭제하기
//-----------------------------------------
cout<< "----- remove b ----- " << endl;
itor=find(list1.begin(), list1.end(), 'b');
list1.erase(itor);
for(itor=list1.begin(); itor != list1.end(); itor++)
{
cout<< *itor << endl;
}
//-----------------------------------------
//STL에서 해당 index 위치에 값을 넣는 방법
//STL의 index는 맨처음이 0이다.
//-----------------------------------------
int i=0;
int index=2;
cout<< "----- 해당 index 위치(2)에 값 f 넣기 ----- " << endl;
for(itor=list1.begin(); itor!= list1.end(); itor++, i++)
{
if(i==index) break;
//cout<< *itor << endl;
}
//삽입
list1.insert(itor,'f');
//전체 출력
for(itor=list1.begin(); itor != list1.end(); itor++)
{
cout<< *itor << endl;
}
//-----------------------------------------
// 갯수 세기
//-----------------------------------------
cout<< "----- 전체 리스트의 갯수 세기 ----- " << endl;
cout<< list1.size()<< endl;;
//-----------------------------------------
// 모두 삭제하기
//-----------------------------------------
cout<< "----- 리스트 모두 삭제하기 ----- " << endl;
for(itor=list1.begin(); itor != list1.end();)
{
//if(list1.empty()) break;
list1.erase(itor++); //erase의 경우 후위형 연산자로만 증가시켜야함
}
list1.clear();
//전체 출력
for(itor=list1.begin(); itor != list1.end(); itor++)
{
//if(list1.empty()) break;
cout<< *itor << endl;
}
}//End of stl()
[출처] STL List 사용법 총정리 (by wono77)|작성자 블랙독
'Programming > C,C++' 카테고리의 다른 글
Vector2D 예제 (0) | 2017.02.25 |
---|---|
function pointer 예제 (0) | 2017.02.25 |
Template 예제 (0) | 2017.01.24 |
class와 OOP 예제3 virtual function (0) | 2017.01.20 |
class와 OOP 예제2 (0) | 2017.01.20 |