-6

I have been trying to initialize an array using vector in C++ and inserting values to it. When I compile the code I get the error as mentioned below.

#include <bits/stdc++.h>

using namespace std;

// Complete the hourglassSum function below.
int hourglassSum(vector<vector<int>> arr) {
  int i,j;
  int sum=0;
  vector<int> vect[16];
  vect.insert(vect.begin(),3,5);
  return 0;
}

**Solution.cpp: In function 'int hourglassSum(std::vector >)':

Solution.cpp:17:6: error: request for member 'insert' in 'vect', which is of non-class type 'std::vector [16]'

vect.insert(vect.begin(),3,5);

Solution.cpp:17:18: error: request for member 'begin' in 'vect', which is of non-class type 'std::vector [16]'

vect.insert(vect.begin(),3,5);** ^~~~~

Abhishek Thakur
  • 379
  • 1
  • 3
  • 9
  • 1
    Why did you declare an array of vectors vector vect[16];? What are you trying to achieve? – Vlad from Moscow Jul 04 '19 at 16:10
  • Basically I am trying to declare an array of length 16 and then insert 5 three times from the front into the array. – Abhishek Thakur Jul 04 '19 at 16:11
  • Then declare an array of integers. – Vlad from Moscow Jul 04 '19 at 16:12
  • vect is not a vector. It's an array of vectors. You can use insert method on any of its items. – Pharaz Fadaei Jul 04 '19 at 16:12
  • 1
    Please read [Why should I not #include ?](https://stackoverflow.com/questions/31816095/why-should-i-not-include-bits-stdc-h) – Some programmer dude Jul 04 '19 at 16:13
  • What is the actual problem you try to solve? Why do you need to do this? What is the purpose of the vector, or a possible array of vectors? Please read about [how to ask good questions](http://stackoverflow.com/help/how-to-ask), as well as [this question checklist](https://codeblog.jonskeet.uk/2012/11/24/stack-overflow-question-checklist/). And of course read about [the XY problem](https://meta.stackexchange.com/questions/66377/what-is-the-xy-problem) since your question is one. – Some programmer dude Jul 05 '19 at 00:00
  • Thanks @Someprogrammerdude for your answer below. It helped. – Abhishek Thakur Jul 06 '19 at 06:41

2 Answers2

3

With

vector<int> vect[16];

you define vect to be an array of 16 different (and empty) vectors. If you want a single vector with 16 elements you should do

vector<int> vect(16);

Note that then if you use vect.insert(...) you will add elements to the vector, changing its size from 16. To solve this either don't use more than the 16 elements you created and use e.g. vect[i] (for a valid index i), or create an empty vector and use vect.emplace_back(...).


If you really want an array, with a fixed size known at compile-time, then maybe use std::array instead:

array<int, 16> vect;  // Creates an array of 16 int elements
Some programmer dude
  • 400,186
  • 35
  • 402
  • 621
  • Please can you elaborate on your Note. I wanted to insert elements into vector sequentially using for loop. – Abhishek Thakur Jul 04 '19 at 16:41
  • @AbhishekThakur With `vector vect(16);` you define a vector of 16 `int` elements, all initialized to `0`. Inserting another element will make the vector contain 17 elements. If you want to "sequentially add elements" start with a single empty vector (i.e. `vector vect;`). – Some programmer dude Jul 04 '19 at 16:45
  • I wanted to add elements to the index of vector like we insert in array indexes. – Abhishek Thakur Jul 04 '19 at 16:50
0

This answer is in response to the clarification by the OP in the comments:

what you need is a vector/array of int you can declare one with :

std::vector<int> v(16, 0);

v is a vector with 16 elements initialized to 0. then you want to change the first 3 elements to 5, you can use std::fill for this:

std::fill(v.begin(), v.begin() + 3, 5);

including <bits/stdc++.h> and using namespace std; are bad practice. please avoid them.

and the godbolt

Oblivion
  • 7,176
  • 2
  • 14
  • 33