【问题标题】:How to insert elements in an array of vectors?如何在向量数组中插入元素?
【发布时间】:2018-10-20 07:40:09
【问题描述】:

如何在其中插入元素?

vector <int> arr[10];

那么如何在需要时在向量中的 arr[i] 位置插入更多元素?

【问题讨论】:

  • 向量 variable_name[size];是向量数组的语法。添加更多信息以获得更好的洞察力
  • 我使用了正确的语法,但这里没有显示我使用了 vector arr[10];
  • vector arr[10] 是一个包含 10 个向量的数组。你知道吗?顺便提一句。如果 vectorstd::vector 它是一个模板。您必须将其与模板参数一起使用,否则将无法编译。

标签: c++ arrays vector


【解决方案1】:
#include <iostream>
using namespace std;

int main()
{
   vector<int> v[3];  // here statically i have mentioned size,
                      // create 3 contiguous vectors of type int
                        //remember that it's is 2d kind
   v[0].push_back(2);

  v[0].push_back(3);


  v[1].push_back(3);


   // This is how you can add more elements than the
   // specified size-dynamically can change the size

   // This is the advantage of vector as we can 
   // dynamically add more elements

   cout<<v[0].front();  //2
   cout<<v[0].back();   //3
   cout<<v[1].at(0)  ;   //3

  //it's structure is something like this{{2,3},{3},{\0}}

   return 0;
}

【讨论】:

  • 为了完整起见,at 可能会抛出,front()end() 如果容器为空,则为 UB。
  • 我在问如何在向量数组中输入元素,而不仅仅是向量。
  • @D_VsH_01 向量数组的语法为 vectorvariable_name(size) ;即大小由我们决定,法线向量是 vectorvariable_name,因此它具有动态大小。不是向量数组也具有动态大小。
  • stackoverflow.com/questions/35501439/…看这篇文章快速了解
  • 同样的事情我想问如何在向量中输入元素 arr[10];不是向量 arr(10);
猜你喜欢
  • 1970-01-01
  • 2010-12-02
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2018-11-26
  • 2012-04-30
  • 1970-01-01
相关资源
最近更新 更多