다음을 통해 공유


Visual C++에서 목록::remove, list::remove_if STL 함수 사용

이 문서에서는 Visual C++에서 STL 함수를 list::remove_if 사용하는 list::remove방법에 대한 정보를 제공합니다.

원래 제품 버전: Visual C++
원래 KB 번호: 168047

요약

아래 샘플 코드는 Visual C++에서 STL 함수를 list::remove_if 사용하는 list::remove방법을 보여 줍니다.

참고 항목

Visual C++ 버전 4.2에서 표준 C++ 라이브러리 구성 요소 구현과 이후 버전에 대한 몇 가지 차이점이 있습니다. 아래 코드의 관련 섹션은 값 _MSC_VER에 따라 조건부로 컴파일됩니다.

필수 헤더

<list>
<string>
<iostream>

프로토타입

void remove(const T& x);
void remove_if(binder2nd< not_equal_to<T> > pr);

참고 항목

프로토타입의 클래스/매개 변수 이름이 헤더 파일의 버전과 일치하지 않을 수 있습니다. 일부는 가독성을 개선하기 위해 수정되었습니다.

설명

이 예제에서는 사용 list::remove 방법 및 list::remove_if. 또한 사용자 고유의 함수와 함께 사용하는 list::remove_if 방법도 보여 줍니다.

샘플 코드

//////////////////////////////////////////////////////////////////////
// Compile options needed: -GX
// remove.cpp : This example shows how to use list::remove and
// list::remove_if. It also shows how to use
// list::remove_if with your own function.
// Functions:
// list::remove
// list::remove_if
// Copyright (c) 1996 Microsoft Corporation. All rights reserved.
//////////////////////////////////////////////////////////////////////

#pragma warning(disable:4786) // disable spurious C4786 warnings

#include <list>
#include <string>
#include <iostream>
using namespace std;

#if _MSC_VER > 1020 // if later than revision 4.2
    using namespace std; // std c++ libs are implemented in std
#endif

typedef list<string, allocator<string> > LISTSTR;

// Used to customize list::remove_if()
class is_four_chars
    : public not_equal_to<string>
{
    bool operator()(const string& rhs, const string&) const
    { return rhs.size() == 4; }
};

void main()
{
    LISTSTR test;
    LISTSTR::iterator i;

    test.push_back("good");
    test.push_back("bad");
    test.push_back("ugly");

    // good bad ugly
    for (i = test.begin(); i != test.end(); ++i)
        cout << *i << " ";
    cout << endl;

    test.remove("bad");

    // good ugly
    for (i = test.begin(); i != test.end(); ++i)
        cout << *i << " ";
    cout << endl;

    // remove any not equal to "good"
    test.remove_if(binder2nd<not_equal_to<string> >
        (not_equal_to<string>(), "good"));

    // good
    for (i = test.begin(); i != test.end(); ++i)
        cout << *i << " ";
    cout << endl;

    // Remove any strings that are four characters long
    test.remove_if(binder2nd<not_equal_to<string> >
        (is_four_chars(), "useless parameter"));

    if (test.empty())
        cout << "Empty list\n";
}

프로그램 출력

good bad ugly
good ugly
good
Empty list

참조

자세한 list::remove list::remove_if내용은 다음 웹 사이트를 방문하세요.