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

    <legend id='7T4yo'><style id='7T4yo'><dir id='7T4yo'><q id='7T4yo'></q></dir></style></legend>

    • <bdo id='7T4yo'></bdo><ul id='7T4yo'></ul>

      <small id='7T4yo'></small><noframes id='7T4yo'>

      <tfoot id='7T4yo'></tfoot>
    1. vector::push_back 与 vector::operator[]

      时间:2023-10-07
          <tbody id='GfwIy'></tbody>

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

        <tfoot id='GfwIy'></tfoot>
          <bdo id='GfwIy'></bdo><ul id='GfwIy'></ul>

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

              1. 本文介绍了vector::push_back 与 vector::operator[]的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

                问题描述

                c++程序下面,

                include<iostream>
                #include<vector>
                using namespace std;
                
                int main()
                {
                     vector<int> numbers;
                
                    numbers.push_back(2);
                    numbers.push_back(10);
                    numbers.push_back(5);
                    numbers.push_back(3);
                    numbers.push_back(7);
                
                    numbers[3] = 8;
                    numbers[5] = 11;
                
                    for(int i=0; i<numbers.size(); ++i)
                    {
                            cout<<" "<<numbers[i];
                    }
                }    
                

                在 ideone 上查看.

                在这里,numbers[3] 正在工作,但 numbers[5].
                看起来,vector::operator[] 不会像 vector::push_back 那样增加向量的大小.
                那么,这是这两者之间的唯一区别还是有其他区别?

                here, numbers[3] is working but numbers[5].
                It looks like, vector::operator[] doesn't increase the size of vector like vector::push_back.
                so, is this the only difference between these two or something else is there?

                推荐答案

                push_back 在背面创建一个具有指定值的新元素.operator[] 要求元素在那里;它只是访问它.[5] 不起作用的原因是因为你有 5 个元素,所以你的索引范围从 0 到 4.

                push_back creates a new element on the back with the value specified. operator[] requires the element to be there; it just accesses it. The reason [5] doesn't work is because you have 5 elements, so your indices range from 0 to 4.

                通常,在添加新元素时,push_back 优先于 resize,其次是 operator[].但是只有一个可以用于读取,并且还需要operator[]来维护正常的数组语法.

                Generally, when adding new elements, push_back is preferred over resize, followed by operator[]. Only one can be used for reading, though, and operator[] is also needed to maintain normal array syntax.

                这篇关于vector::push_back 与 vector::operator[]的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持html5模板网!

                上一篇:重载向量&lt;T&gt;的输出流操作符 下一篇:比较 vector<T>::iterator 和 vector<T>::reverse

                相关文章

                最新文章

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

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

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