• <bdo id='nkQTc'></bdo><ul id='nkQTc'></ul>

    <legend id='nkQTc'><style id='nkQTc'><dir id='nkQTc'><q id='nkQTc'></q></dir></style></legend>

      1. <tfoot id='nkQTc'></tfoot>

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

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

      3. 我要求用户输入大小和数组,但是当我打印矢量时,它仅显示“0"作为输出

        时间:2023-08-28

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

                <tbody id='vrUnw'></tbody>
              <legend id='vrUnw'><style id='vrUnw'><dir id='vrUnw'><q id='vrUnw'></q></dir></style></legend>

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

              <tfoot id='vrUnw'></tfoot>

                  本文介绍了我要求用户输入大小和数组,但是当我打印矢量时,它仅显示“0"作为输出的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着跟版网的小编来一起学习吧!

                  问题描述

                  我声明了一个向量并尝试放置大小和值并打印它

                  I declared a vector and trying to put size and values and printing it

                  #include<iostream>
                  #include<vector>
                  using namespace std;
                  int main()
                  {
                      int s;          
                      cin>>s;                   //taking size of vector
                      vector <int> arr(s);
                      int input;
                      while (cin >> input)
                         {arr.push_back(input);}     //inserting the values in array
                      for(int i=0;i<s;i++)
                          cout<<" "<<arr[i];         //printing the values
                  }
                  

                  我的输入5

                  1 2 3 4 5

                  预期输出

                  1 2 3 4 5

                  电流输出0 0 0 0 0

                  Current output 0 0 0 0 0

                  推荐答案

                  这一行:

                  vector <int> arr(s);
                  

                  使 arr 的大小为 s.它将具有默认初始化为 0 的 s 元素.然后您正在对这个向量执行 push_back,这将 additional 元素添加到矢量.

                  makes arr have the size s. It will have s elements that have been default-initialized to 0. Then you are doing push_back on this vector, which adds additional elements into the vector.

                  当您打印出第一个 s 元素时,您看不到从 cin 中读取的值,而是从 sarr 声明中创建的初始值.

                  When you print out the first s elements, you are not seeing the values that were read from cin, but the s number of initial values created in the declaration of arr.

                  要解决这个问题,要么在声明 arr 时不要给出大小,要么只使用 arr[i] = input; 而不是 push_back() 在循环中.

                  To fix this, either don't give a size when you declare arr, or else just use arr[i] = input; instead of push_back() in the loop.

                  这篇关于我要求用户输入大小和数组,但是当我打印矢量时,它仅显示“0"作为输出的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

                  上一篇:为什么 while (true) 在收到无效输入时会跳过 cin? 下一篇:当 cin 的输入是一个“点"时,while 循环到无穷大

                  相关文章

                1. <tfoot id='kBGLC'></tfoot>
                  <legend id='kBGLC'><style id='kBGLC'><dir id='kBGLC'><q id='kBGLC'></q></dir></style></legend>
                  • <bdo id='kBGLC'></bdo><ul id='kBGLC'></ul>

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

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