Reputation: 138
This program launches, but when entering data, it throws an error (In visual studio: Expression: Vector subscript out of range)
I must be doing something wrong, but I'm very new at vector arrays, so I'm trying to learn them ;)
Also, I would like to know, what loop condition to using, when the size of the vector array is unknown (it may vary)
#include <iostream>
#include <conio.h>
#include <vector>
#include <string>
using namespace std;
int main()
{
vector<vector<string>> v; // 2D vector array
string stri; // Input Data
for (auto i = 0;; i++)
{
for (auto j = 0;; j++)
{
cout << "Enter array's " << i << " and " << j << " element:";
cin >> stri;
v[i][j] = stri;
}
}
for (auto i = 0;; i++)
{
for (auto j = 0;; j++)
{
cout << v[i][j] << "\t";
}
cout << endl;
}
_getch();
}
Thanks for help ;)
Upvotes: 0
Views: 154
Reputation: 3274
There are two problems. First, the vectors are not initialized. By default, vectors have 0 elements, so when you try to acces v[i][j]
, it will complain that it is out of range. Second, there is no condition to exit the loops, making the loops infinite. Assuming that you know the size of the arrays in advance, try this:
#include <iostream>
#include <conio.h>
#include <vector>
#include <string>
using namespace std;
int main()
{
size_t n = 4;
size_t m = 3;
vector<vector<string>> v(n, vector<string>(m)); // 2D vector array
string stri; // Input Data
for (auto i = 0; i<v.size(); i++)
{
for (auto j = 0; j<v[i].size(); j++)
{
cout << "Enter array's " << i << " and " << j << " element:";
cin >> stri;
v[i][j] = stri;
}
}
for (auto i = 0; i<v.size(); i++)
{
for (auto j = 0; j<v[i].size(); j++)
{
cout << v[i][j] << "\t";
}
cout << endl;
}
_getch();
}
If you do not know the size in advance and you want the user to decide, try something like this:
int main()
{
vector<vector<string>> v; // 2D vector array
string stri; // Input Data
for (auto i = 0; ; i++)
{
v.emplace_back();
for (auto j = 0; ; j++)
{
cout << "Enter array's " << i << " and " << j << " element (Enter 'End' to end the row): ";
cin >> stri;
if(stri == "End")
break;
v[i].push_back(stri);
}
cout << "Do you want to add another row? [y/n] ";
char c;
cin >> c;
if (c == 'n')
break;
}
for (auto i = 0;i<v.size(); i++)
{
for (auto j = 0;j<v[i].size(); j++)
{
cout << v[i][j] << "\t";
}
cout << endl;
}
getch();
}
Upvotes: 4
Reputation: 923
So v in your code is a vector of vectors. Then this line in your code:
v[i][j] = stri;
is only OK if the following is true:
You can use the resize
function to set the size of a vector. If you modify your code like this, adding two resize
calls, then your code can succeed to use the vector a few times, but only until the size has been reached:
#include <iostream>
#include <vector>
#include <string>
using namespace std;
int main()
{
vector<vector<string>> v; // 2D vector array
v.resize(5);
string stri; // Input Data
for (auto i = 0;; i++)
{
v[i].resize(5);
for (auto j = 0;; j++)
{
cout << "Enter array's " << i << " and " << j << " element:";
cin >> stri;
v[i][j] = stri;
}
}
}
The above program survives until the size 5 is reached, then it anyway does not work anymore. It is anyway a strange program since the loops are infinite; i will remain zero and j will just increase forever (or in practice until the vector size is reached).
Upvotes: 1