1. <tfoot id='ffgEK'></tfoot>
      <legend id='ffgEK'><style id='ffgEK'><dir id='ffgEK'><q id='ffgEK'></q></dir></style></legend>

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

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

    3. 如何在循环中向空向量添加元素?

      How do I add elements to an empty vector in a loop?(如何在循环中向空向量添加元素?)
      • <small id='wj2Hd'></small><noframes id='wj2Hd'>

              <tbody id='wj2Hd'></tbody>

          1. <tfoot id='wj2Hd'></tfoot>
              <bdo id='wj2Hd'></bdo><ul id='wj2Hd'></ul>

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

                本文介绍了如何在循环中向空向量添加元素?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着跟版网的小编来一起学习吧!

                问题描述

                我正在尝试在循环内创建一个空向量,并且希望在每次将某些内容读入该循环时向该向量添加一个元素.

                I am trying to create an empty vector inside a loop, and want to add an element to the vector each time something is read in to that loop.

                #include <iostream>
                #include <vector>
                
                using namespace std;
                
                int main()
                {
                   std::vector<float> myVector();
                
                   float x;
                   while(cin >> x)
                      myVector.insert(x);
                
                   return 0;
                }
                

                但这给了我错误信息.

                推荐答案

                您需要使用 std::vector::push_back() 代替:

                You need to use std::vector::push_back() instead:

                while(cin >> x)
                  myVector.push_back(x);
                //         ^^^^^^^^^
                

                而不是 std::vector::insert(),正如你在链接中看到的,它需要一个迭代器来指示你想要插入元素的位置.

                and not std::vector::insert(), which, as you can see in the link, needs an iterator to indicate the position where you want to insert the element.

                另外,作为 @Joel 评论了什么,您应该删除向量变量定义中的括号.

                Also, as what @Joel has commented, you should remove the parentheses in your vector variable's definition.

                std::vector<float> myVector;
                

                不是

                std::vector<float> myVector();
                

                通过执行后者,您会遇到 C++ 的最烦人的解析问题.

                By doing the latter, you run into C++'s Most Vexing Parse problem.

                这篇关于如何在循环中向空向量添加元素?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

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

                相关文档推荐

                Consistent pseudo-random numbers across platforms(跨平台一致的伪随机数)
                Vary range of uniform_int_distribution(改变uniform_int_distribution的范围)
                What is a seed in terms of generating a random number?(就生成随机数而言,种子是什么?)
                Is 1.0 a valid output from std::generate_canonical?(1.0 是 std::generate_canonical 的有效输出吗?)
                Getting big random numbers in C/C++(在 C/C++ 中获取大随机数)
                What is the best way to generate random numbers in C++?(在 C++ 中生成随机数的最佳方法是什么?)

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

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

                      1. <legend id='QqW1N'><style id='QqW1N'><dir id='QqW1N'><q id='QqW1N'></q></dir></style></legend>
                          <tbody id='QqW1N'></tbody>

                        • <tfoot id='QqW1N'></tfoot>