<tfoot id='bVIHx'></tfoot>
        <legend id='bVIHx'><style id='bVIHx'><dir id='bVIHx'><q id='bVIHx'></q></dir></style></legend>

      1. <small id='bVIHx'></small><noframes id='bVIHx'>

      2. <i id='bVIHx'><tr id='bVIHx'><dt id='bVIHx'><q id='bVIHx'><span id='bVIHx'><b id='bVIHx'><form id='bVIHx'><ins id='bVIHx'></ins><ul id='bVIHx'></ul><sub id='bVIHx'></sub></form><legend id='bVIHx'></legend><bdo id='bVIHx'><pre id='bVIHx'><center id='bVIHx'></center></pre></bdo></b><th id='bVIHx'></th></span></q></dt></tr></i><div id='bVIHx'><tfoot id='bVIHx'></tfoot><dl id='bVIHx'><fieldset id='bVIHx'></fieldset></dl></div>
          <bdo id='bVIHx'></bdo><ul id='bVIHx'></ul>

        在 C++ 中使用 istringstream 将字符串拆分为整数

        Splitting a string into integers using istringstream in C++(在 C++ 中使用 istringstream 将字符串拆分为整数)
        <tfoot id='nMSHF'></tfoot>

        <small id='nMSHF'></small><noframes id='nMSHF'>

          <tbody id='nMSHF'></tbody>
      3. <i id='nMSHF'><tr id='nMSHF'><dt id='nMSHF'><q id='nMSHF'><span id='nMSHF'><b id='nMSHF'><form id='nMSHF'><ins id='nMSHF'></ins><ul id='nMSHF'></ul><sub id='nMSHF'></sub></form><legend id='nMSHF'></legend><bdo id='nMSHF'><pre id='nMSHF'><center id='nMSHF'></center></pre></bdo></b><th id='nMSHF'></th></span></q></dt></tr></i><div id='nMSHF'><tfoot id='nMSHF'></tfoot><dl id='nMSHF'><fieldset id='nMSHF'></fieldset></dl></div>
        <legend id='nMSHF'><style id='nMSHF'><dir id='nMSHF'><q id='nMSHF'></q></dir></style></legend>

              <bdo id='nMSHF'></bdo><ul id='nMSHF'></ul>

                1. 本文介绍了在 C++ 中使用 istringstream 将字符串拆分为整数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着跟版网的小编来一起学习吧!

                  问题描述

                  我正在尝试使用 istringstream 将一个简单的字符串拆分为一系列整数:

                  I'm trying to use istringstream to split a simple string into a series of integers:

                  #include <string>
                  #include <iostream>
                  #include <sstream>
                  #include <vector>
                  
                  using namespace std;
                  
                  int main(){
                  
                      string s = "1 2 3"; 
                      istringstream iss(s);   
                  
                      while (iss)
                      {
                          int n;
                          iss >> n;
                          cout << "* " << n << endl;
                      } 
                  }
                  

                  我得到:

                  * 1
                  * 2
                  * 3
                  * 3
                  

                  为什么最后一个元素总是出现两次?如何解决?

                  Why is the last element always coming out twice? How to fix it?

                  推荐答案

                  它出现了两次,因为你的循环是错误的,正如在 http://www.parashift.com/c++-faq-lite/input-output.html#faq-15.5 (在这种情况下,while (iss)while (iss.eof()) 没有什么不同.

                  It's coming out twice because your looping is wrong, as explained (indirectly) at http://www.parashift.com/c++-faq-lite/input-output.html#faq-15.5 (while (iss) is not dissimilar from while (iss.eof()) in this scenario).

                  具体来说,在第三次循环迭代中,iss >>n 成功并获取您的 3,并使流保持良好状态.由于这种良好的状态,循环然后第四次运行,直到下一次(第四次)iss>>n 随后失败,循环条件被破坏.但是在第四次迭代结束之前,您仍然输出 n... 第四次.

                  Specifically, on the third loop iteration, iss >> n succeeds and gets your 3, and leaves the stream in a good state. The loop then runs a fourth time due to this good state, and it's not until the next (fourth) iss >> n subsequently fails that the loop condition is broken. But before that fourth iteration ends, you still output n... a fourth time.

                  试试:

                  #include <string>
                  #include <iostream>
                  #include <sstream>
                  #include <vector>
                  
                  using namespace std;
                  
                  int main()
                  {
                      string s = "1 2 3"; 
                      istringstream iss(s);   
                      int n;
                  
                      while (iss >> n) {
                          cout << "* " << n << endl;
                      } 
                  }
                  

                  这篇关于在 C++ 中使用 istringstream 将字符串拆分为整数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

                  本站部分内容来源互联网,如果有图片或者内容侵犯了您的权益,请联系我们,我们会在确认后第一时间进行删除!

                  相关文档推荐

                  Unable to access non-const member functions of objects in C++ std::set(无法访问 C++ std::set 中对象的非常量成员函数)
                  How should a size-limited stl-like container be implemented?(应该如何实现大小受限的 stl 类容器?)
                  Constructing std::function argument from lambda(从 lambda 构造 std::function 参数)
                  STL BigInt class implementation(STL BigInt 类实现)
                  Sync is unreliable using std::atomic and std::condition_variable(使用 std::atomic 和 std::condition_variable 同步不可靠)
                  Move list element to the end in STL(在 STL 中将列表元素移动到末尾)
                  <tfoot id='nnSWl'></tfoot>

                    <i id='nnSWl'><tr id='nnSWl'><dt id='nnSWl'><q id='nnSWl'><span id='nnSWl'><b id='nnSWl'><form id='nnSWl'><ins id='nnSWl'></ins><ul id='nnSWl'></ul><sub id='nnSWl'></sub></form><legend id='nnSWl'></legend><bdo id='nnSWl'><pre id='nnSWl'><center id='nnSWl'></center></pre></bdo></b><th id='nnSWl'></th></span></q></dt></tr></i><div id='nnSWl'><tfoot id='nnSWl'></tfoot><dl id='nnSWl'><fieldset id='nnSWl'></fieldset></dl></div>
                    • <bdo id='nnSWl'></bdo><ul id='nnSWl'></ul>

                        <small id='nnSWl'></small><noframes id='nnSWl'>

                      • <legend id='nnSWl'><style id='nnSWl'><dir id='nnSWl'><q id='nnSWl'></q></dir></style></legend>
                            <tbody id='nnSWl'></tbody>