|
68 | 68 |
|
69 | 69 | </head>
|
70 | 70 | <body>
|
71 |
| -<header data-kunai-mdinfo="{"meta": {"header": ["sstream"], "namespace": ["std"], "class": ["basic_istringstream"], "id-type": ["function"]}, "sources": [], "page_id": ["reference", "sstream", "basic_istringstream", "str"]}"> |
| 71 | +<header data-kunai-mdinfo="{"meta": {"header": ["sstream"], "namespace": ["std"], "class": ["basic_istringstream"], "id-type": ["function"]}, "sources": [{"id": "8ac20eab784310a3051a39eb0c9ea1c8b4bd7f99", "source": "#include <iostream>\n#include <sstream>\n#include <string>\n\nint main()\n{\n // \u6587\u5b57\u5217\u306e\u53d6\u5f97\n std::istringstream iss(\"Hello World 123 45.67\");\n std::cout << \"Content: \" << iss.str() << std::endl;\n\n // \u4e00\u90e8\u3092\u8aad\u307f\u53d6\u3063\u3066\u3082\u3001str()\u306f\u5168\u4f53\u3092\u8fd4\u3059\n std::string word;\n iss >> word;\n std::cout << \"Read word: \" << word << std::endl;\n std::cout << \"Full content: \" << iss.str() << std::endl;\n\n // \u65b0\u3057\u3044\u6587\u5b57\u5217\u3092\u8a2d\u5b9a\n iss.str(\"New content 999\");\n iss.clear(); // \u30a8\u30e9\u30fc\u30d5\u30e9\u30b0\u3092\u30af\u30ea\u30a2\n\n // \u65b0\u3057\u3044\u5185\u5bb9\u304b\u3089\u8aad\u307f\u53d6\u308a\n std::string s1, s2;\n int num;\n iss >> s1 >> s2 >> num;\n std::cout << \"New read: \" << s1 << \" \" << s2 << \" \" << num << std::endl;\n\n // \u7a7a\u6587\u5b57\u5217\u3092\u8a2d\u5b9a\u3057\u3066\u30ea\u30bb\u30c3\u30c8\n iss.str(\"\");\n std::cout << \"Empty? \" << (iss.str().empty() ? \"yes\" : \"no\") << std::endl;\n}\n"}], "page_id": ["reference", "sstream", "basic_istringstream", "str"]}"> |
72 | 72 | <nav class="navbar navbar-default" role="navigation">
|
73 | 73 | <div class="container-fluid">
|
74 | 74 | <div class="navbar-header">
|
|
200 | 200 |
|
201 | 201 | <p class="text-right"><small>
|
202 | 202 | 最終更新日時(UTC):
|
203 |
| - <span itemprop="datePublished" content="2025-06-05T03:37:16"> |
204 |
| - 2025年06月05日 03時37分16秒 |
| 203 | + <span itemprop="datePublished" content="2025-06-05T03:56:00"> |
| 204 | + 2025年06月05日 03時56分00秒 |
205 | 205 | </span>
|
206 | 206 | <br/>
|
207 | 207 | <span itemprop="author" itemscope itemtype="http://schema.org/Person">
|
@@ -251,6 +251,46 @@ <h2>効果</h2>
|
251 | 251 | <li>(5) : <code>rdbuf()->str(s)</code> と等価</li>
|
252 | 252 | <li>(6) : <code>rdbuf()->str(std::move(s))</code> と等価</li>
|
253 | 253 | </ul>
|
| 254 | +<h2>例</h2> |
| 255 | +<p><div class="yata" id="8ac20eab784310a3051a39eb0c9ea1c8b4bd7f99"><div class="codehilite"><pre><span></span><code><span class="cp">#include</span> <span class="cpf"><a href="../../iostream.html"><iostream></a></span><span class="cp"></span> |
| 256 | +<span class="cp">#include</span> <span class="cpf"><a href="../../sstream.html"><sstream></a></span><span class="cp"></span> |
| 257 | +<span class="cp">#include</span> <span class="cpf"><a href="../../string.html"><string></a></span><span class="cp"></span> |
| 258 | + |
| 259 | +<span class="kt">int</span> <span class="nf">main</span><span class="p">()</span> |
| 260 | +<span class="p">{</span> |
| 261 | + <span class="c1">// 文字列の取得</span> |
| 262 | + <span class="n"><span href="https://cpprefjp.github.io/reference/sstream/basic_istream.html">std::istringstream</span></span> <span class="n">iss</span><span class="p">(</span><span class="s">"Hello World 123 45.67"</span><span class="p">);</span> |
| 263 | + <span class="n"><a href="../../iostream/cout.html">std::cout</a></span> <span class="o"><<</span> <span class="s">"Content: "</span> <span class="o"><<</span> <span class="n">iss</span><span class="p">.</span><span class="n">str</span><span class="p">()</span> <span class="o"><<</span> <span class="n"><a href="../../ostream/endl.html">std::endl</a></span><span class="p">;</span> |
| 264 | + |
| 265 | + <span class="c1">// 一部を読み取っても、str()は全体を返す</span> |
| 266 | + <span class="n"><a href="../../string/basic_string.html">std::string</a></span> <span class="n">word</span><span class="p">;</span> |
| 267 | + <span class="n">iss</span> <span class="o">>></span> <span class="n">word</span><span class="p">;</span> |
| 268 | + <span class="n"><a href="../../iostream/cout.html">std::cout</a></span> <span class="o"><<</span> <span class="s">"Read word: "</span> <span class="o"><<</span> <span class="n">word</span> <span class="o"><<</span> <span class="n"><a href="../../ostream/endl.html">std::endl</a></span><span class="p">;</span> |
| 269 | + <span class="n"><a href="../../iostream/cout.html">std::cout</a></span> <span class="o"><<</span> <span class="s">"Full content: "</span> <span class="o"><<</span> <span class="n">iss</span><span class="p">.</span><span class="n">str</span><span class="p">()</span> <span class="o"><<</span> <span class="n"><a href="../../ostream/endl.html">std::endl</a></span><span class="p">;</span> |
| 270 | + |
| 271 | + <span class="c1">// 新しい文字列を設定</span> |
| 272 | + <span class="n">iss</span><span class="p">.</span><span class="n">str</span><span class="p">(</span><span class="s">"New content 999"</span><span class="p">);</span> |
| 273 | + <span class="n">iss</span><span class="p">.</span><span class="n">clear</span><span class="p">();</span> <span class="c1">// エラーフラグをクリア</span> |
| 274 | + |
| 275 | + <span class="c1">// 新しい内容から読み取り</span> |
| 276 | + <span class="n"><a href="../../string/basic_string.html">std::string</a></span> <span class="n">s1</span><span class="p">,</span> <span class="n">s2</span><span class="p">;</span> |
| 277 | + <span class="kt">int</span> <span class="n">num</span><span class="p">;</span> |
| 278 | + <span class="n">iss</span> <span class="o">>></span> <span class="n">s1</span> <span class="o">>></span> <span class="n">s2</span> <span class="o">>></span> <span class="n">num</span><span class="p">;</span> |
| 279 | + <span class="n"><a href="../../iostream/cout.html">std::cout</a></span> <span class="o"><<</span> <span class="s">"New read: "</span> <span class="o"><<</span> <span class="n">s1</span> <span class="o"><<</span> <span class="s">" "</span> <span class="o"><<</span> <span class="n">s2</span> <span class="o"><<</span> <span class="s">" "</span> <span class="o"><<</span> <span class="n">num</span> <span class="o"><<</span> <span class="n"><a href="../../ostream/endl.html">std::endl</a></span><span class="p">;</span> |
| 280 | + |
| 281 | + <span class="c1">// 空文字列を設定してリセット</span> |
| 282 | + <span class="n">iss</span><span class="p">.</span><span class="n">str</span><span class="p">(</span><span class="s">""</span><span class="p">);</span> |
| 283 | + <span class="n"><a href="../../iostream/cout.html">std::cout</a></span> <span class="o"><<</span> <span class="s">"Empty? "</span> <span class="o"><<</span> <span class="p">(</span><span class="n">iss</span><span class="p">.</span><span class="n">str</span><span class="p">().</span><span class="n">empty</span><span class="p">()</span> <span class="o">?</span> <span class="s">"yes"</span> <span class="o">:</span> <span class="s">"no"</span><span class="p">)</span> <span class="o"><<</span> <span class="n"><a href="../../ostream/endl.html">std::endl</a></span><span class="p">;</span> |
| 284 | +<span class="p">}</span> |
| 285 | +</code></pre></div> |
| 286 | +</div></p> |
| 287 | +<h3>出力</h3> |
| 288 | +<p><pre><code>Content: Hello World 123 45.67 |
| 289 | +Read word: Hello |
| 290 | +Full content: Hello World 123 45.67 |
| 291 | +New read: New content 999 |
| 292 | +Empty? yes |
| 293 | +</code></pre></p> |
254 | 294 | <h2>関連項目</h2>
|
255 | 295 | <ul>
|
256 | 296 | <li><code><a href="../basic_stringbuf/str.html">basic_stringbuf::str</a></code></li>
|
|
0 commit comments