| 909 | // Erase while iterating for sequential containers (Pattern 1) |
| 910 | template<template<typename> class ContainerTemplate> |
| 911 | void test_erase_while_iterating() { |
| 912 | using Container = ContainerTemplate<int>; |
| 913 | Container c; |
| 914 | // Insert multiple elements |
| 915 | for (int i = 1; i <= 5; ++i) { |
| 916 | c.push_back(i); |
| 917 | } |
| 918 | |
| 919 | FL_CHECK(c.size() == 5); |
| 920 | |
| 921 | // Erase every other element while iterating |
| 922 | int erased_count = 0; |
| 923 | for (auto it = c.begin(); it != c.end(); ) { |
| 924 | if (*it % 2 == 0) { |
| 925 | it = c.erase(it); // erase returns next iterator |
| 926 | erased_count++; |
| 927 | } else { |
| 928 | ++it; |
| 929 | } |
| 930 | } |
| 931 | |
| 932 | FL_CHECK(erased_count == 2); // Elements 2 and 4 were erased |
| 933 | FL_CHECK(c.size() == 3); // Elements 1, 3, 5 remain |
| 934 | |
| 935 | // Verify remaining elements |
| 936 | int count = 0; |
| 937 | for (auto it = c.begin(); it != c.end(); ++it) { |
| 938 | count++; |
| 939 | FL_CHECK(*it % 2 == 1); // All remaining should be odd |
| 940 | } |
| 941 | FL_CHECK(count == 3); |
| 942 | } |
| 943 | |
| 944 | // Erase while iterating for map containers (Pattern 2) |
| 945 | template<typename Map> |