• <small id='BjArH'></small><noframes id='BjArH'>

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

      1. 将整行整数读入向量

        Reading line of integers into a vector(将整行整数读入向量)

            <tbody id='0E8m9'></tbody>
            <tfoot id='0E8m9'></tfoot>

              <small id='0E8m9'></small><noframes id='0E8m9'>

              • <bdo id='0E8m9'></bdo><ul id='0E8m9'></ul>
              • <i id='0E8m9'><tr id='0E8m9'><dt id='0E8m9'><q id='0E8m9'><span id='0E8m9'><b id='0E8m9'><form id='0E8m9'><ins id='0E8m9'></ins><ul id='0E8m9'></ul><sub id='0E8m9'></sub></form><legend id='0E8m9'></legend><bdo id='0E8m9'><pre id='0E8m9'><center id='0E8m9'></center></pre></bdo></b><th id='0E8m9'></th></span></q></dt></tr></i><div id='0E8m9'><tfoot id='0E8m9'></tfoot><dl id='0E8m9'><fieldset id='0E8m9'></fieldset></dl></div>
                <legend id='0E8m9'><style id='0E8m9'><dir id='0E8m9'><q id='0E8m9'></q></dir></style></legend>
                1. 本文介绍了将整行整数读入向量的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着跟版网的小编来一起学习吧!

                  问题描述

                  请看下面的代码

                  int main()
                  {
                      cout << "Enter numbers (-1 to stop entering; -2 to exit)" << endl;
                  
                      while(cin>>enterNumber)
                      {
                       numbers.push_back(enterNumber);
                      }
                  
                  
                      for(size_t size=0;size<numbers.size();size++)
                      {
                          cout << numbers[size] << endl;
                      }
                  }
                  

                  我在这里尝试做的是这样的

                  What I am trying to do here is something like this

                  1. 输入数字列表(例如:1 2 3 4 5 6 7 8 9 0 11)
                  2. 将它们全部读入向量
                  3. 打印它们

                  在这里,当我按回车键时,什么也没有发生!似乎循环没有退出.当我按下回车键时如何打印值?

                  In here, when I hit enter, nothing is happening! Seems like the loops didn't exit. How to print the values when I hit the enter?

                  更新

                  我按照答案中给出的建议编辑了代码.

                  I edited the code as advises given in answers.

                  int main()
                  {
                      cout << "Enter numbers (-1 to stop entering; -2 to exit)" << endl;
                  
                      std::string line;
                  getline(std::cin, line);
                  std::istringstream iss(line);
                  while (iss >> enterNumber)
                  {
                      numbers.push_back(enterNumber);
                  }
                  
                  
                      for(size_t size=0;size<numbers.size();size++)
                      {
                          cout << numbers[size] << endl;
                      }
                  }
                  

                  但它现在给出了另一个错误

                  but it gives another error now

                  "/usr/bin/make" -f nbproject/Makefile-Debug.mk QMAKE= SUBPROJECTS= .build-conf
                  make[1]: Entering directory `/cygdrive/c/Users/yohan/Documents/NetBeansProjects/Excersice 6'
                  "/usr/bin/make"  -f nbproject/Makefile-Debug.mk dist/Debug/Cygwin-Windows/excersice_6.exe
                  make[2]: Entering directory `/cygdrive/c/Users/yohan/Documents/NetBeansProjects/Excersice 6'
                  mkdir -p build/Debug/Cygwin-Windows
                  rm -f build/Debug/Cygwin-Windows/Multiple.o.d
                  g++    -c -g -MMD -MP -MF build/Debug/Cygwin-Windows/Multiple.o.d -o build/Debug/Cygwin-Windows/Multiple.o Multiple.cpp
                  Multiple.cpp: In function `int main()':
                  Multiple.cpp:22: error: variable `std::istringstream iss' has initializer but incomplete type
                  Multiple.cpp:60:3: warning: no newline at end of file
                  make[2]: *** [build/Debug/Cygwin-Windows/Multiple.o] Error 1
                  make[1]: *** [.build-conf] Error 2
                  make: *** [.build-impl] Error 2
                  nbproject/Makefile-Debug.mk:78: recipe for target `build/Debug/Cygwin-Windows/Multiple.o' failed
                  make[2]: Leaving directory `/cygdrive/c/Users/yohan/Documents/NetBeansProjects/Excersice 6'
                  nbproject/Makefile-Debug.mk:61: recipe for target `.build-conf' failed
                  make[1]: Leaving directory `/cygdrive/c/Users/yohan/Documents/NetBeansProjects/Excersice 6'
                  nbproject/Makefile-impl.mk:39: recipe for target `.build-impl' failed
                  
                  
                  BUILD FAILED (exit value 2, total time: 1s)
                  

                  推荐答案

                  使用 getline.然后将该字符串放入istringstream.然后从 istringstream 中读取,而不是你现在使用 cin 的地方.

                  Read in a line from cin into a string using getline. Then put that string into an istringstream. Then read from that istringstream in place of where you're using cin now.

                  std::string line;
                  getline(std::cin, line);
                  std::istringstream iss(line);
                  while (iss >> enterNumber)
                  {
                      numbers.push_back(enterNumber);
                  }
                  

                  这篇关于将整行整数读入向量的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

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

                  相关文档推荐

                  What is inside .lib file of Static library, Statically linked dynamic library and dynamically linked dynamic library?(静态库、静态链接动态库和动态链接动态库的 .lib 文件里面是什么?)
                  How do I load a C DLL from the SXS in Python?(如何从 Python 中的 SXS 加载 C DLL?)
                  Can Cython code be compiled to a dll so C++ application can call it?(Cython 代码可以编译成 dll 以便 C++ 应用程序可以调用它吗?)
                  Delay Loading DLLs(延迟加载 DLL)
                  Throwing C++ exceptions across DLL boundaries(跨 DLL 边界抛出 C++ 异常)
                  Loading a dll from a dll?(从 dll 加载 dll?)

                  <legend id='uS048'><style id='uS048'><dir id='uS048'><q id='uS048'></q></dir></style></legend>
                    <bdo id='uS048'></bdo><ul id='uS048'></ul>

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

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