回答編集履歴

1

加筆

2022/11/28 06:16

投稿

episteme
episteme

スコア16614

test CHANGED
@@ -1 +1,21 @@
1
1
  [std::optional](https://cpprefjp.github.io/reference/optional/optional.html) をvectorの要素にしてはいかがでしょう。
2
+ ```C++
3
+
4
+ #include <iostream>
5
+ #include <vector>
6
+ #include <string>
7
+ #include <optional>
8
+
9
+ int main() {
10
+ std::vector<std::optional<std::string>> v = { "apple", "banana", "cherry" };
11
+ v[1].reset();
12
+
13
+ for ( auto& item : v ) {
14
+ if ( item ) std::cout << *item;
15
+ else std::cout << "(null)";
16
+ std::cout << std::endl;
17
+ }
18
+ }
19
+ ```
20
+
21
+