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

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

        <bdo id='LOEkw'></bdo><ul id='LOEkw'></ul>
    2. <i id='LOEkw'><tr id='LOEkw'><dt id='LOEkw'><q id='LOEkw'><span id='LOEkw'><b id='LOEkw'><form id='LOEkw'><ins id='LOEkw'></ins><ul id='LOEkw'></ul><sub id='LOEkw'></sub></form><legend id='LOEkw'></legend><bdo id='LOEkw'><pre id='LOEkw'><center id='LOEkw'></center></pre></bdo></b><th id='LOEkw'></th></span></q></dt></tr></i><div id='LOEkw'><tfoot id='LOEkw'></tfoot><dl id='LOEkw'><fieldset id='LOEkw'></fieldset></dl></div>
    3. <legend id='LOEkw'><style id='LOEkw'><dir id='LOEkw'><q id='LOEkw'></q></dir></style></legend>
    4. 向量将所有负值转换为零

      时间:2023-09-15

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

        <tbody id='spwtc'></tbody>
      <tfoot id='spwtc'></tfoot>

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

              • 本文介绍了向量将所有负值转换为零的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

                问题描述

                我制作了一个恒定大小的向量来存储负值,然后打印出我得到的所有值都是零.我只想知道为什么它不存储负值.

                I made a vector of constant size to store negative values, and then printing the values all I got was zeroes. I just want to know why it is not storing negative values.

                #include <iostream>
                #include <vector>
                
                int main() {
                    std::vector<int> v(5);
                    v.push_back(-1);
                    v.push_back(-2);
                    v.push_back(-3);
                    v.push_back(-4);
                    v.push_back(-5);
                
                    for (int i=0; i<5; i++)
                       std::cout << v[i] << " ";  // All I got was zeroes
                }
                

                推荐答案

                那是因为 push_backnew 元素放在向量的末尾.

                That's because push_back puts new elements onto the end of the vector.

                运行i9可以看到效果:负数会占用v[5]v[9].

                You can see the effect by running i to 9: the negative numbers will occupy v[5] to v[9].

                写作

                std::vector<int> v{-1, -2, -3, -4, -5};
                

                相反,这是一个特别优雅的修复.

                instead is a particularly elegant fix.

                这篇关于向量将所有负值转换为零的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持html5模板网!

                上一篇:默认情况下,为什么 C++ 不检测何时使用 [] 运算 下一篇:为什么不对 std::vector 重载 operator+=() ?

                相关文章

                最新文章

                <tfoot id='Ska5L'></tfoot>

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

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

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