**The code I tried to implement is : **
#include<iostream>
using namespace std;
int main()
{
int n, k;
cout << "Enter the number of elements: \n";
cin >> n;
cout << "Enter the array: \n";
int *arr = new int[n];
for (int i = 0; i < n; i++)
cin >> arr[i];
cout << "Enter k: " << endl;
cin >> k;
cout << "Elements less than k: " << endl;
for (int x : arr)
{
if (x < k)
cout << x << endl;
}
delete[] arr;
return 0;
}
**The error it's throwing is :** error: 'begin' was not declared in this scope for (int x : arr) ^
The array is dynamically allocated. Takes elements and var 'k' as input and ideally returns the elements which are less than k. first loop is traditional for loop second one is range based, and the problem is in that
I tried taking reference '&x' in there to but the problem persists
problem :
error: 'begin' was not declared in this scope
for (int x : arr)
^
arris a pointer. It is not an array. There is no "size information" associated with a pointer -- it is just a pointer, no more, no less.int *arr = new int[n];-->std::vector<int> arr(n);begin()andend()method that return iterators to the begin and end of your container. As you can see you only have anint*and that has no way of nowing what its "end" is, it is just a pointer into memory. That's why people tell you to usestd::vector, it solves multiple things : you will have no new/delete needed (new/delete hardly ever should appear in current C++ code), You will not have an owning raw pointer (also not recommmended in C++) and you will get yourbegin()andend()fromstd::vector