i want to know when should we actually use vectors instead of going for an array
When should we use vectors
Actually I am trying to become handy with vectors because i learnt it yesterday, so I just want to know when should I use vectors in a particular question and how would I know whether using vectors will make the question simple or not
@sktg99 Sanchit vector is resizable some time It has more advantages than the array it doubles it size when it fills but array can’t do but generally, array makes easier in writing the code. you can try experimenting your assignments with vectors
I tried to access a 2d vector elements with its functions but its giving error for this segment
for (int i = 0;i < v.size();i++) {
for (auto it = v[i].begin();it != v[i].end();–it)
{
cout << *it << ", ";
}
}
what this error could be?
I sent you a part of it
this was the complete code
what is wrong in it now
#include
#include
using namespace std;
int main() {
int n, m;
cin >> n >> m;
vector < vector> v(m);
for (int i = 0;i < n;i++) {
for (int j = 0;j < m;j++) {
int temp;
cin >> temp;
v[i].push_back(temp);
}
}
for (int i = 0;i < v.size();i++) {
for (auto it = v[i].begin();it != v[i].end();–it) {
cout << *it << ", ";
}
}
}
there is some technical problem going on with hackerblocks ide
they said it will be resolved by today
can u pls check that code here for today pls
@sktg99 hey sanchit see this
#include< bits/stdc++.h >
using namespace std;
int main() {
int n, m;
cin >> n >> m;
vector < vector > v(m);
for (int i = 0;i < n;i++) {
for (int j = 0;j < m;j++) {
int temp;
cin >> temp;
v[i].push_back(temp);
}
}
for (int i = 0;i < v.size();i++) {
for (int j=0; j<v[i].size() ;j++) {
cout << v[i][j] <<" ";
}
cout<<endl;
}
}
I know this method but this is similar to an array , i wanted to implement that begin and end function in a 2d vector? how can i do that???