C + + list 사용법

86394 단어 C++
C + + list 사용법
원문:http://www.cnblogs.com/lalalabi/p/5060210.html
list 는 요 소 를 링크 에 순서대로 저장 합 니 다.벡터 (vectors) 에 비해 빠 른 삽입 과 삭 제 를 허용 하지만 무 작위 접근 은 느 립 니 다.
list 는 선형 양 방향 링크 구조 로 그의 데 이 터 는 몇 개의 노드 로 구성 되 고 모든 노드 는 하나의 정보 블록 (즉, 실제 저 장 된 데이터), 전구 지침 과 백 드라이브 지침 을 포함한다.지정 한 메모리 크기 를 할당 하지 않 고 임의로 신축 할 수 있 습 니 다. 이것 은 비 연속 적 인 메모리 공간 에 저장 되 고 포인터 로 질서 있 는 요 소 를 연결 하기 때 문 입 니 다.구조 적 인 이유 로 list 무 작위 검색 의 성능 이 매우 좋 지 않 습 니 다. vector 처럼 요소 의 주 소 를 직접 찾 는 것 이 아니 라 처음부터 하나씩 찾 아야 하기 때 문 입 니 다. 그러면 목표 요소 가 뒤로 갈수 록 검색 시간 이 길 어 집 니 다.검색 시간 은 목표 요소 의 위치 와 정비례 한다.무 작위 검색 속도 가 빠 르 지 않 지만 모든 노드 에 빠르게 삽입 하고 삭제 할 수 있 습 니 다.list 의 모든 노드 는 링크 에 저 장 된 위 치 를 저장 하기 때문에 하나의 요 소 를 삽입 하거나 삭제 하 는 것 은 최대 세 개의 요소 에 만 영향 을 미 칩 니 다. vector 가 조작 점 이후 의 모든 요소 의 저장 주소 에 영향 을 미 치지 않 는 다 는 점 은 vector 와 비교 할 수 없습니다.
list 의 특징:
(1) 연속 적 인 메모리 공간 을 사용 하지 않 으 면 동적 작업 을 마음대로 할 수 있 습 니 다.(2) 내부 의 모든 위치 에 빠르게 삽입 하거나 삭제 할 수 있 으 며, 물론 양쪽 에서 push 와 pop 을 진행 할 수 있 습 니 다.(3) 내부 의 무 작위 접근 을 할 수 없습니다. 즉, [] 연산 자 와 vector. at () 를 지원 하지 않 습 니 다.(4) verctor 대비 더 많은 메모 리 를 점용 하 다.
초보 list 가 습득 해 야 할 지식:
(1) list 정의 
(2) list 에 요소 추가 
(3) list 가 비어 있 는 지 어떻게 압 니까? 
(4) list 를 옮 겨 다 니 기 위해 for 순환 을 사용 하 는 방법 
(5) STL 의 통용 알고리즘 for 를 어떻게 사용 합 니까?각각 list 옮 겨 다 니 기 
(6) list 구성원 함수 begin () 과 end () 및 의미 
(7) iterator 범위 의 개념 과 한 범위 의 마지막 위 치 는 실제로 처리 되 지 않 는 다 는 사실
구성원 함수 목록:
assign () list 에 값 부여  back () 마지막 요 소 를 되 돌려 줍 니 다.  begin () 첫 번 째 요 소 를 가리 키 는 교체 기 를 되 돌려 줍 니 다.  clear () 모든 요소 삭제  empty () list 가 비어 있 으 면 true 로 돌아 갑 니 다.  end () 끝 에 있 는 교체 기 를 되 돌려 줍 니 다.  erase () 요소 삭제  front () 첫 번 째 요 소 를 되 돌려 줍 니 다.  get_allocator () list 설정 기 되 돌려 주기  insert () 요 소 를 list 에 삽입 합 니 다.  max_size () list 가 수용 할 수 있 는 최대 요소 수 를 되 돌려 줍 니 다.  merge () 두 list 병합  pop_back () 마지막 요소 삭제  pop_front () 첫 번 째 요소 삭제  push_back () list 의 끝 에 요 소 를 추가 합 니 다.  push_front () 는 list 의 머리 에 요 소 를 추가 합 니 다.  rbegin () 은 첫 번 째 요 소 를 가리 키 는 역방향 교체 기 를 되 돌려 줍 니 다.  remove () list 에서 요소 삭제  remove_if () 지정 한 조건 에 따라 요 소 를 삭제 합 니 다.  rend () 가 list 끝 을 가리 키 는 역방향 교체 기  resize () list 크기 변경  reverse () 는 list 의 요 소 를 거꾸로 돌 립 니 다.  size () 는 list 의 요소 개 수 를 되 돌려 줍 니 다.  sort () list 정렬  splice () 두 list 통합  swap () 두 list 교환  unique () list 에서 중복 되 는 요소 삭제
list 인 스 턴 스 를 만 들 고 값 을 부여 합 니 다:
 1 //         
 2 #include 
 3 #include 
 4 using namespace std;
 5 int main () {
 6     //
 7     int myints[] = {75,23,65,42,13};
 8     list<int> mylist1(myints, myints+5);
 9     list<int> mylist2(2,100);         // 2   100   
10     //   , push_back, push_front
11     for (int i = 1; i <= 5; ++i) mylist1.push_back(i);
12     mylist2.push_front (200);
13     mylist2.push_front (300);
14     //   , assign
15     list<int> first;
16     list<int> second;
17     first.assign(7,100);                       //  first  7   100   
18     second.assign(first.begin(), first.end()); //   first second
19     int myints[] = {16, 8, 4};
20     first.assign (myints, myints + 3);         //    myints      first
21 
22     //   , insert  
23     return 0;
24 }

 
구성원 함수:
Iterator:  (list 옮 겨 다 니 기 에 사용 가능)
iterator begin();  //첫 번 째 요 소 를 가리 키 는 교체 기 를 되 돌려 줍 니 다.
iterator end();  //마지막 요 소 를 가리 키 는 교체 기 를 되 돌려 줍 니 다.
reverse_iterator rbegin();  //첫 번 째 요 소 를 가리 키 는 역방향 교체 기 를 되 돌려 줍 니 다.
reverse_rend();  //마지막 요 소 를 가리 키 는 역방향 교체 기 를 되 돌려 줍 니 다.
 1 //list   
 2 #include 
 3 #include 
 4 using namespace std;
 5 int main () {
 6     int myints[] = {75,23,65,42,13};
 7     list<int> mylist (myints,myints+5);
 8     cout << "mylist contains:";
 9     //
10     for (list<int>::iterator it = mylist.begin(); it != mylist.end(); ++it)
11         cout << ' ' << *it;
12     cout << '
'; 13 14 list.clear(); 15 // 16 for (int i = 1; i <= 5; ++i) mylist.push_back(i); 17 cout << "mylist backwards:"; 18 for (list<int>::reverse_iterator rit = mylist.rbegin(); rit != mylist.rend(); ++rit) 19 cout << ' ' << *rit; 20 cout << '
'; 21 return 0; 22 } : mylist contains: 75 23 65 42 13 mylist backwards: 5 4 3 2 1

 
용량: (list 용기 크기 정 보 를 가 져 오 는 데 사용)
bool empty() const;  //list 가 비어 있 을 때 true 로 돌아 가기
size_type size() const;  //list 용기 에 있 는 요소 의 개 수 를 되 돌려 줍 니 다.
size_type max_size() const;  //list 용기 에 최대 수용 할 수 있 는 요소 의 개 수 를 되 돌려 줍 니 다. 주로 list 의 resize () 함 수 를 호출 할 때 요청 한 size 크기 가 허용 되 는 지 확인 합 니 다.
Element access: (수미 요 소 를 가 져 오 는 데 사용)
reference front();  //첫 번 째 요소 의 인용 을 되 돌려 줍 니 다.
const_reference front() const;
reference back();  //마지막 요소 의 인용 을 되 돌려 줍 니 다.
const_reference front() const;
Modifiers:
  • asign  //용기 에 새로운 내용 추가:
  • template
    void assign(InputIterator first, InputIterator last);  //first, last 는 시퀀스 의 시작 과 끝 에 있 는 교체 기의 값 입 니 다. [first, last) 는 시퀀스 의 모든 요 소 를 포함 합 니 다.
    void assign(size_type n, const value_type& val);  //list 에 n 개의 값 을 val 로 할당 하 는 요소
     1 // list::assign
     2 #include 
     3 #include 
     4 using namespace std;
     5 int main () {
     6     list<int> first;
     7     list<int> second;
     8     first.assign(7,100);                      //  first  7   100   
     9     second.assign(first.begin(), first.end()); //   first second
    10 
    11     int myints[] = {16, 8, 4};
    12     first.assign (myints, myints + 3);            //    myints      first
    13 
    14     cout << "Size of first: " << int (first.size()) << '
    '; 15 cout << "Size of second: " << int (second.size()) << '
    '; 16 return 0; 17 } : Size of first: 3 Size of second: 7
  • push_front, pop_front, push_back, pop_back

  • void push_front(const value_type& val);  //list 헤더 에 요소 추가
    void pop_front();  //list 헤더 의 요소 삭제
    void push_back(const value_type& val);  //list 끝 에 요소 추가
    void pop_back();  //list 끝의 요소 삭제
     1 #include 
     2 #include 
     3 using namespace std;
     4 int main () {
     5     list<int> mylist (2,100);         // 2   100   
     6     // list::push_front
     7     mylist.push_front (200);
     8     mylist.push_front (300);
     9 
    10     cout << "mylist contains:";
    11     for (list<int>::iterator it = mylist.begin(); it != mylist.end(); ++it)
    12         cout << ' ' << *it;
    13     cout << '
    '; 14 15 // list::pop_front 16 cout << "Popping out the elements in mylist:"; 17 while (!mylist.empty()) { 18 cout << ' ' << mylist.front(); 19 mylist.pop_front(); 20 } 21 cout << "
    Final size of mylist is
    " << mylist.size() << '
    '; 22 23 // list::push_back 24 int myint; 25 cout << "Please enter some integers (enter 0 to end):
    "; 26 do { 27 cin >> myint; 28 mylist.push_back (myint); 29 } while (myint); 30 cout << "mylist contains:"; 31 for (list<int>::iterator it = mylist.begin(); it != mylist.end(); ++it) 32 cout << ' ' << *it; 33 cout << '
    '; 34 35 // list::pop_back 36 while (!mylist.empty()) { 37 cout << ' ' << mylist.back(); 38 mylist.pop_back(); 39 } 40 cout << "
    Final size of mylist is
    " << mylist.size() << '
    '; 41 42 return 0; 43 } : mylist contai: 300 200 100 100 Popping out the elements in mylist: 300 200 100 100 Final size of mylist is 0 Please enter some integers (enter 0 to end): 56 23 8 5 6 0 mylist contains: 56 23 8 5 6 0 0 6 5 8 23 56 Final size of mylist is 0
  • insert  //요소 삽입:
  • iterator insert (iterator position, const value_type& val);  //position 는 삽입 할 이 list 의 교체 기 입 니 다. val 은 삽입 할 값 입 니 다.
    void insert (iterator position, size_type n, const value_type& val);  //이 list 용기 의 position 위치 부터 n 개의 값 이 val 인 요 소 를 삽입 합 니 다.
    template
    void insert (iterator position, InputIterator first, InputIterator last);  //first, last 는 우리 가 선택 한 값 을 이 list 에 삽입 하 는 용기 의 교체 기 입 니 다.
     1 // inserting into a list
     2 #include 
     3 #include 
     4 #include 
     5 using namespace std;
     6 int main () {
     7     list<int> mylist;
     8     list<int>::iterator it;
     9     //    
    10     for (int i = 1; i <= 5; ++i) mylist.push_back(i); // 1 2 3 4 5
    11     it = mylist.begin();
    12     ++it;       //    it      2                      ^
    13     // i0t          10
    14     mylist.insert (it,10);                        // 1 10 2 3 4 5
    15 
    16     // "it"       2                                   ^
    17     // it            20
    18     mylist.insert (it,2,20);                      // 1 10 20 20 2 3 4 5
    19 
    20     --it;       //   it    20                             ^
    21 
    22     vector<int> myvector (2,30); //  vector  ,       2   30   
    23     // vector      list 
    24     mylist.insert (it,myvector.begin(),myvector.end());
    25                                                 // 1 10 20 30 30 20 2 3 4 5
    26     //it      20                            //               ^
    27     cout << "mylist contains:";
    28     for (it = mylist.begin(); it != mylist.end(); ++it)
    29         cout << ' ' << *it;
    30     cout << '
    '; 31 32 return 0; 33 } : mylist contains: 1 10 20 30 30 20 2 3 4 5

     
  • erase  //원소 삭제:
  • iterator erase (iterator position);  //교체 기 position 가 가리 키 는 값 을 삭제 하고 변수 없 이 반환 값 을 받 을 수 있 습 니 다.
    iterator erase (iterator first, iterator last);  //[first, last) 의 값 을 삭제 하고 반환 값 을 변수 로 받 지 않 아 도 됩 니 다.
     1 // erasing from list
     2 #include 
     3 #include 
     4 using namespace std;
     5 int main () {
     6     list<int> mylist;
     7     list<int>::iterator it1,it2;
     8 
     9     // set some values:
    10     for (int i = 1; i < 10; ++i) mylist.push_back(i*10);
    11 
    12                                 // 10 20 30 40 50 60 70 80 90
    13     it1 = it2 = mylist.begin(); // ^^
    14     advance (it2,6);            // ^                 ^
    15     ++it1;                      //    ^              ^
    16 
    17     it1 = mylist.erase (it1);   // 10 30 40 50 60 70 80 90
    18                                 //    ^           ^
    19 
    20     it2 = mylist.erase (it2);   // 10 30 40 50 60 80 90
    21                                 //    ^           ^
    22 
    23     ++it1;                      //       ^        ^
    24     --it2;                      //       ^     ^
    25     //          
    26     mylist.erase (it1,it2);     // 10 30 60 80 90
    27                                 //       ^
    28     cout << "*it1 : " << *it1 << endl;
    29     cout << "mylist contains:";
    30     for (it1 = mylist.begin(); it1 != mylist.end(); ++it1)
    31         cout << ' ' << *it1;
    32     cout << '
    '; 33 34 return 0; 35 } : it1 : 40 mylist contains: 10 30 60 80 90

     
  • swap  //두 list 의 내용 교환
  • void swap(list& x);  //교환 할 두 목록 에 저 장 된 요소 의 유형 은 같 아야 합 니 다. 목록 크기 는 다 를 수 있 습 니 다.
     1 // swap lists
     2 #include 
     3 #include 
     4 using namespace std;
     5 int main () {
     6     list<int> first (3,100);   //     100   
     7     list<int> second (5,200);  //     200   
     8 
     9     first.swap(second);
    10 
    11     cout << "first contains:";
    12     for (list<int>::iterator it = first.begin(); it != first.end(); it++)
    13         cout << ' ' << *it;
    14     cout << '
    '; 15 16 cout << "second contains:"; 17 for (list<int>::iterator it = second.begin(); it != second.end(); it++) 18 cout << ' ' << *it; 19 cout << '
    '; 20 21 return 0; 22 } : first contains: 200 200 200 200 200 second contains: 100 100 100

     
  • resize  //목록 크기 조정
  • void resize (size_type n, value_type val = value_type());  //list 크기 를 n 개의 요 소 를 수용 할 수 있 도록 조정 합 니 다. n 이 현재 list 크기 보다 크 면 list 끝 에서 val 값 을 삽입 하여 list 크기 가 n 을 만족 시 킬 때 까지 합 니 다.
     1 // resizing list
     2 #include 
     3 #include 
     4 using namespace std;
     5 int main () {
     6     list<int> mylist;
     7 
     8     //    
     9     for (int i = 1; i < 10; ++i) mylist.push_back(i);
    10 
    11     mylist.resize(5);
    12     mylist.resize(8,100);
    13     mylist.resize(12);
    14 
    15     cout << "mylist contains:";
    16     for (list<int>::iterator it = mylist.begin(); it != mylist.end(); ++it)
    17         cout << ' ' << *it;
    18     cout << '
    '; 19 20 return 0; 21 } : mylist contains: 1 2 3 4 5 100 100 100 0 0 0 0

     
  • clear  //목록 비우 기
  • void clear();  //list 의 모든 요소 삭제
     1 // clearing lists
     2 #include 
     3 #include 
     4 using namespace std;
     5 int main () {
     6     list<int> mylist;
     7     list<int>::iterator it;
     8 
     9     mylist.push_back (100);
    10     mylist.push_back (200);
    11     mylist.push_back (300);
    12 
    13     cout << "mylist contains:";
    14     for (it = mylist.begin(); it != mylist.end(); ++it)
    15         cout << ' ' << *it;
    16     cout << '
    '; 17 18 mylist.clear(); 19 mylist.push_back (1101); 20 mylist.push_back (2202); 21 22 cout << "mylist contains:"; 23 for (it = mylist.begin(); it != mylist.end(); ++it) 24 cout << ' ' << *it; 25 cout << '
    '; 26 27 return 0; 28 } : mylist contains: 100 200 300 mylist contains: 1101 2202

     
    Operations:
  • splice  //하나의 list 의 값 을 다른 list 로 이동 합 니 다
  • void splice (iterator position, list& x);  //목록 x 의 모든 요 소 를 현재 list 로 옮 깁 니 다. 현재 목록 의 position 가 가리 키 는 위치 부터 시작 합 니 다. 이 때 목록 x 는 비어 있 습 니 다.
    void splice (iterator position, list& x, iterator i);  //목록 x 에서 교체 기 i 가 가리 키 는 요 소 를 현재 list 의 position 가 가리 키 는 위치 로 옮 깁 니 다. i 가 가리 키 는 요 소 는 목록 x 에서 제거 되 기 때문에 교체 기 i 는 invalid 입 니 다. position 는 현재 목록 의 교체 기 이 고 i 는 목록 x 의 교체 기 입 니 다.
    void splice (iterator position, list& x, iterator first, iterator last);  //목록 x 의 [first, last) 요 소 를 현재 list 로 옮 기 고 position 가 가리 키 는 위치 에서 시작 합 니 다. first, last 는 목록 x 의 교체 기 입 니 다.
     1 // splicing lists
     2 #include 
     3 #include 
     4 using namespace std;
     5 int main () {
     6     list<int> mylist1, mylist2;
     7     list<int>::iterator it;
     8 
     9     //    
    10     for (int i = 1; i <= 4; ++i)
    11        mylist1.push_back(i);      // mylist1: 1 2 3 4
    12 
    13     for (int i = 1; i <= 3; ++i)
    14        mylist2.push_back(i*10);   // mylist2: 10 20 30
    15 
    16     it = mylist1.begin();
    17     ++it;                         //     2
    18 
    19     mylist1.splice (it, mylist2); // mylist1: 1 10 20 30 2 3 4
    20                                   // mylist2 (empty)
    21                                   // "it"       2
    22 
    23     mylist2.splice (mylist2.begin(),mylist1, it);
    24                                 // mylist1: 1 10 20 30 3 4
    25                                 // mylist2: 2
    26                                 // "it"        
    27     it = mylist1.begin();
    28     advance(it,3);           // "it"     30
    29 
    30     mylist1.splice ( mylist1.begin(), mylist1, it, mylist1.end());
    31                                 // mylist1: 30 3 4 1 10 20
    32 
    33     cout << "mylist1 contains:";
    34     for (it = mylist1.begin(); it != mylist1.end(); ++it)
    35         cout << ' ' << *it;
    36     cout << '
    '; 37 38 cout << "mylist2 contains:"; 39 for (it = mylist2.begin(); it != mylist2.end(); ++it) 40 cout << ' ' << *it; 41 cout << '
    '; 42 43 return 0; 44 } : mylist1 contains: 30 3 4 1 10 20 mylist2 contains: 2

     
  • remove  //list 에서 지정 한 값 삭제
  • void remove (const value_type& val);  //list 에서 모든 값 이 val 인 요 소 를 삭제 합 니 다.
     1 // remove from list
     2 #include 
     3 #include 
     4 using namespace std;
     5 int main () {
     6     int myints[]= {17, 89, 7, 89, 14};
     7     list<int> mylist (myints,myints+5);
     8 
     9     mylist.remove(89);
    10 
    11     cout << "mylist contains:";
    12     for (list<int>::iterator it = mylist.begin(); it != mylist.end(); ++it)
    13         cout << ' ' << *it;
    14     cout << '
    '; 15 16 return 0; 17 } : mylist contains: 17 7 14

     
  • remove_if  //조건 부 삭제
  • template
    void remove_if (Predicate pred);  //pred 는 함수 일 수도 있 고 class 일 수도 있 지만 하나의 인자 가 필요 합 니 다. 또한 매개 변수 유형 은 list 에 저 장 된 요소 유형 과 같 습 니 다. 조건 을 만족 시 키 면 true 로 돌아 갑 니 다.
     1 // list::remove_if
     2 #include 
     3 #include 
     4 using namespace std;
     5 // a predicate implemented as a function:
     6 bool single_digit (const int& value) { return (value < 10); }
     7 
     8 // a predicate implemented as a class:
     9 struct is_odd {
    10   //      ()
    11   bool operator() (const int& value) { return (value % 2) == 1; }
    12 };
    13 
    14 int main () {
    15     int myints[] = {15, 36, 7, 17, 20, 39, 4, 1};
    16     list<int> mylist (myints, myints + 8);   // 15 36 7 17 20 39 4 1
    17 
    18     mylist.remove_if (single_digit);           // 15 36 17 20 39
    19 
    20     mylist.remove_if (is_odd());               // 36 20
    21 
    22     cout << "mylist contains:";
    23     for (std::list<int>::iterator it=mylist.begin(); it!=mylist.end(); ++it)
    24         cout << ' ' << *it;
    25     cout << '
    '; 26 27 return 0; 28 } : mylist contains: 36 20

     
  • unique  //중복 값 삭제
  • void unique();  //인접 한 중복 요소 만 삭제 하고 첫 번 째 값 을 유지 할 수 있 기 때문에 이 함 수 는 정렬 된 list 에 만 유용 합 니 다.
    template
    void unique (BinaryPredicate binary_pred);  //binary pred 는 함수 일 수도 있 고 class 일 수도 있 지만 두 개의 인자 가 필요 합 니 다. 또한 유형 은 list 에 저 장 된 값 형식 과 같 습 니 다. 특정한 조건 을 만족 시 키 면 true 로 돌아 갑 니 다.
     1 // list::unique
     2 #include 
     3 #include 
     4 #include 
     5 using namespace std;
     6 // a binary predicate implemented as a function:
     7 bool same_integral_part (double first, double second) { return ( int(first)==int(second) ); }
     8 
     9 // a binary predicate implemented as a class:
    10 struct is_near {
    11     bool operator() (double first, double second) { return (fabs(first-second)<5.0); }
    12 };
    13 
    14 int main () {
    15     double mydoubles[] = { 12.15, 2.72, 73.0, 12.77, 3.14,
    16                        12.77, 73.35, 72.25, 15.3, 72.25 };
    17     list<double> mylist (mydoubles,mydoubles+10);
    18     cout << "mylist contains:";
    19     for (list<double>::iterator it = mylist.begin(); it != mylist.end(); ++it)
    20         cout << ' ' << *it;
    21     cout << '
    '; 22 23 mylist.unique(); 24 cout << "mylist contains:"; 25 for (list<double>::iterator it = mylist.begin(); it != mylist.end(); ++it) 26 cout << ' ' << *it; 27 cout << '
    '; 28 29 mylist.sort(); // 2.72, 3.14, 12.15, 12.77, 12.77, 30 // 15.3, 72.25, 72.25, 73.0, 73.35 31 32 mylist.unique(); // 2.72, 3.14, 12.15, 12.77 33 // 15.3, 72.25, 73.0, 73.35 34 cout << "mylist contains:"; 35 for (list<double>::iterator it = mylist.begin(); it != mylist.end(); ++it) 36 cout << ' ' << *it; 37 cout << '
    '; 38 39 mylist.unique (same_integral_part); // 2.72, 3.14, 12.15 40 // 15.3, 72.25, 73.0 41 cout << "mylist contains:"; 42 for (list<double>::iterator it = mylist.begin(); it != mylist.end(); ++it) 43 cout << ' ' << *it; 44 cout << '
    '; 45 46 mylist.unique (is_near()); // 2.72, 12.15, 72.25 47 48 cout << "mylist contains:"; 49 for (list<double>::iterator it = mylist.begin(); it != mylist.end(); ++it) 50 cout << ' ' << *it; 51 cout << '
    '; 52 53 return 0; 54 } : mylist contains: 12.15 2.72 73 12.77 3.14 12.77 73.35 72.25 15.3 72.25 mylist contains: 12.15 2.72 73 12.77 3.14 12.77 73.35 72.25 15.3 72.25 mylist contains: 2.72 3.14 12.15 12.77 15.3 72.25 73 73.35 mylist contains: 2.72 3.14 12.15 15.3 72.25 73 mylist contains: 2.72 12.15 72.25

     
  • merge  //질서 있 는 list 통합
  • void merge(list &x);  //목록 x 의 요 소 를 기본 순서 로 현재 목록 에 옮 깁 니 다. 이 때 목록 x 는 비어 있 고 현재 목록 은 시퀀스 표 입 니 다.
    template
    void merge (list& x, Compare comp);  //cop 는 하나의 함수 로 두 개의 매개 변 수 를 요구 할 수 있 으 며, 매개 변수 유형 은 list 에 저 장 된 요소 유형 과 같 습 니 다. 조건 을 만족 시 킬 때 true 로 돌아 갑 니 다. merge 는 이 조건 에 따라 두 목록 을 합 칩 니 다.
     1 // list::merge
     2 #include 
     3 #include 
     4 using namespace std;
     5 // compare only integral part:
     6 bool mycomparison (double first, double second) { return ( (first)<(second) ); }
     7 
     8 int main () {
     9     list<double> first, second;
    10 
    11     first.push_back (3.1);
    12     first.push_back (2.2);
    13     first.push_back (2.9);
    14 
    15     second.push_back (3.7);
    16     second.push_back (7.1);
    17     second.push_back (1.4);
    18 
    19     first.sort();
    20     second.sort();
    21 
    22     first.merge(second);
    23     cout << "first contains:";
    24     for (list<double>::iterator it = first.begin(); it != first.end(); ++it)
    25         cout << ' ' << *it;
    26     cout << '
    '; 27 // (second ) 28 29 second.push_back (2.1); 30 second.push_back(2.5); 31 32 first.merge(second,mycomparison); 33 cout << "first contains:"; 34 for (list<double>::iterator it = first.begin(); it != first.end(); ++it) 35 cout << ' ' << *it; 36 cout << '
    '; 37 38 return 0; 39 } : first contains: 1.4 2.2 2.9 3.1 3.7 7.1 first contains: 1.4 2.1 2.2 2.5 2.9 3.1 3.7 7.1

     
  • sort  //정렬
  • void sort();  //기본 오름차 순 정렬
    template
    void sort (Compare comp);  //cop 는 하나의 함수 일 수 있 습 니 다. 두 개의 매개 변 수 를 요구 합 니 다. 유형 은 list 의 요소 유형 과 같 습 니 다. 조건 을 만족 시 킬 때 true 로 돌아 갑 니 다. sort () 함 수 는 cop 에서 제정 한 규칙 에 따라 요 소 를 정렬 합 니 다.
     1 // list::sort
     2 #include 
     3 #include 
     4 #include <string>
     5 #include 
     6 using namespace std;
     7 // comparison, not case sensitive.
     8 bool compare_nocase (const string& first, const string& second) {
     9     unsigned int i = 0;
    10     while ((i < first.length()) && (i < second.length()) ) {
    11         //           
    12         if (tolower(first[i]) < tolower(second[i])) return true;
    13         else if (tolower(first[i]) > tolower(second[i])) return false;
    14         ++i;
    15     }
    16     return ( first.length() < second.length() );
    17 }
    18 
    19 int main () {
    20     list<string> mylist;
    21     list<string>::iterator it;
    22     mylist.push_back ("one");
    23     mylist.push_back ("two");
    24     mylist.push_back ("Three");
    25 
    26     mylist.sort();
    27 
    28     cout << "mylist contains:";
    29     for (it = mylist.begin(); it != mylist.end(); ++it)
    30         cout << ' ' << *it;
    31     cout << '
    '; 32 33 mylist.sort(compare_nocase); 34 35 cout << "mylist contains:"; 36 for (it = mylist.begin(); it != mylist.end(); ++it) 37 cout << ' ' << *it; 38 cout << '
    '; 39 40 return 0; 41 } : mylist contains: Three one two mylist contains: one Three two

     
  • reverse  //역순:
  • void reverse();  //list 에서 요소 의 순 서 를 되 돌 립 니 다.
     1 // reversing list
     2 #include 
     3 #include 
     4 using namespace std;
     5 int main () {
     6     list<int> mylist;
     7 
     8     for (int i = 1; i < 10; ++i) mylist.push_back(i);
     9 
    10     mylist.reverse();
    11 
    12     cout << "mylist contains:";
    13     for (list<int>::iterator it = mylist.begin(); it != mylist.end(); ++it)
    14         cout << ' ' << *it;
    15     cout << '
    '; 16 17 return 0; 18 } : mylist contains: 9 8 7 6 5 4 3 2 1

     
    Observers:
  • get_allocator  //이 list 와 관련 된 분배 기 대상 되 돌려 주기
  • allocator_type get_allocator() const;  //배열 의 동적 할당 공간 에 사용 할 수 있 습 니 다.
     1 // list::get_allocator
     2 #include 
     3 #include 
     4 using namespace std;
     5 int main () {
     6     list<int> mylist;
     7     int * p;
     8 
     9     // allocate an array of 5 elements using mylist's allocator:
    10     p = mylist.get_allocator().allocate(5);
    11 
    12     // assign some values to array
    13     for (int i = 0; i < 5; ++i) p[i] = i;
    14 
    15     cout << "The allocated array contains:";
    16     for (int i = 0; i < 5; ++i) cout << ' ' << p[i];
    17     cout << '
    '; 18 19 mylist.get_allocator().deallocate(p,5); 20 21 return 0; 22 } : The allocated array contains: 9 8 7 6 5 4 3 2 1

    좋은 웹페이지 즐겨찾기