| 944 | // Erase while iterating for map containers (Pattern 2) |
| 945 | template<typename Map> |
| 946 | void test_map_erase_while_iterating() { |
| 947 | Map m; |
| 948 | // Insert multiple elements |
| 949 | for (int i = 1; i <= 5; ++i) { |
| 950 | m.insert(fl::make_pair(i, i * 10)); |
| 951 | } |
| 952 | |
| 953 | FL_CHECK(m.size() == 5); |
| 954 | |
| 955 | // Erase every other element while iterating |
| 956 | int erased_count = 0; |
| 957 | for (auto it = m.begin(); it != m.end(); ) { |
| 958 | if (it->first % 2 == 0) { |
| 959 | it = m.erase(it); // erase returns next iterator |
| 960 | erased_count++; |
| 961 | } else { |
| 962 | ++it; |
| 963 | } |
| 964 | } |
| 965 | |
| 966 | FL_CHECK(erased_count == 2); // Keys 2 and 4 were erased |
| 967 | FL_CHECK(m.size() == 3); // Keys 1, 3, 5 remain |
| 968 | |
| 969 | // Verify remaining elements |
| 970 | FL_CHECK(m.count(1) > 0); |
| 971 | FL_CHECK(m.count(3) > 0); |
| 972 | FL_CHECK(m.count(5) > 0); |
| 973 | FL_CHECK(m.count(2) == 0); |
| 974 | FL_CHECK(m.count(4) == 0); |
| 975 | |
| 976 | // Verify values |
| 977 | auto it = m.find(1); |
| 978 | FL_CHECK(it != m.end()); |
| 979 | FL_CHECK(it->second == 10); |
| 980 | } |
| 981 | |
| 982 | // ============================================================================ |
| 983 | // PATTERN 3: CONTAINER FACTORY (Complex Initialization) |