| 1 | // { dg-do run { target c++20 } } |
| 2 | // { dg-add-options no_pch } |
| 3 | |
| 4 | // Copyright (C) 2018-2025 Free Software Foundation, Inc. |
| 5 | // |
| 6 | // This file is part of the GNU ISO C++ Library. This library is free |
| 7 | // software; you can redistribute it and/or modify it under the |
| 8 | // terms of the GNU General Public License as published by the |
| 9 | // Free Software Foundation; either version 3, or (at your option) |
| 10 | // any later version. |
| 11 | |
| 12 | // This library is distributed in the hope that it will be useful, |
| 13 | // but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 14 | // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 15 | // GNU General Public License for more details. |
| 16 | |
| 17 | // You should have received a copy of the GNU General Public License along |
| 18 | // with this library; see the file COPYING3. If not see |
| 19 | // <http://www.gnu.org/licenses/>. |
| 20 | |
| 21 | #include <string> |
| 22 | |
| 23 | #ifndef __cpp_lib_erase_if |
| 24 | # error "Feature-test macro for erase_if missing in <string>" |
| 25 | #elif __cpp_lib_erase_if < 202002 |
| 26 | # error "Feature-test macro for erase_if has wrong value in <string>" |
| 27 | #endif |
| 28 | |
| 29 | #include <testsuite_hooks.h> |
| 30 | |
| 31 | void |
| 32 | test01() |
| 33 | { |
| 34 | auto is_vowel = [](const char c) |
| 35 | { |
| 36 | return c == 'a' || c == 'e' || c == 'i' || c == 'o' || c == 'u'; |
| 37 | }; |
| 38 | |
| 39 | std::string str("cute fluffy kittens"); |
| 40 | auto num = std::erase_if(str, is_vowel); |
| 41 | VERIFY( str == "ct flffy kttns" ); |
| 42 | VERIFY( num == 5 ); |
| 43 | } |
| 44 | |
| 45 | void |
| 46 | test02() |
| 47 | { |
| 48 | std::string str = "cute fluffy kittens"; |
| 49 | auto num = std::erase(str, 'f'); |
| 50 | VERIFY( str == "cute luy kittens" ); |
| 51 | VERIFY( num == 3 ); |
| 52 | num = std::erase(str, 'z'); |
| 53 | VERIFY( str == "cute luy kittens" ); |
| 54 | VERIFY( num == 0 ); |
| 55 | } |
| 56 | |
| 57 | int |
| 58 | main() |
| 59 | { |
| 60 | test01(); |
| 61 | test02(); |
| 62 | |
| 63 | return 0; |
| 64 | } |