Computer >> Hướng Dẫn Máy Tính >  >> Lập Trình >> C++

Hàm forward_list::remove() trong C++ STL


Trong bài viết này, chúng ta sẽ thảo luận về hoạt động, cú pháp và ví dụ của hàm forward_list::remove() và forward_list::remove_if() trong C++.

forward_list::remove() là gì?

forward_list::remove() là hàm tích hợp trong C++ STL khai báo trong header. Hàm này xóa tất cả các phần tử có giá trị bằng giá trị cho trước khỏi forward_list. Kích thước container giảm đi số phần tử bị xóa.

Cú pháp

flist_container1.remove(const value_type& value);

Hàm chỉ nhận một tham số: giá trị cần xóa.

Giá trị trả về

Hàm không trả về giá trị.

Ví dụ

#include <forward_list>
#include <iostream>
using namespace std;

int main() {
    forward_list<int> forwardList = {2, 3, 1, 1, 1, 6, 7};
    cout << "list before applying remove operation: ";
    for (auto i = forwardList.begin(); i != forwardList.end(); ++i) cout << ' ' << *i;
    forwardList.remove(1);
    cout << "\nlist after applying remove operation: ";
    for (auto i = forwardList.begin(); i != forwardList.end(); ++i) cout << ' ' << *i;
}

Đầu ra

list before applying remove operation: 2 3 1 1 1 6 7
list after applying remove operation: 2 3 6 7