How to pass vector into 2d array?
I have a text file with of content like this:
[1,1]-3-Big_City
[1,2]-3-Big_City
....etc.
I have read the file and tokenize string into vector<string>. The output look like this:
[1,1]
3
Big_City
[1,2]
3
Big_City
...
How to pass [1,1] into 2D array?
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39
|
vector<string> tokenizeString (string input, string delimiter)
{
size_t pos = 0;
string token;
vector<string> result;
while ((pos = input.find(delimiter)) != string::npos)
{
token = input.substr(0, pos);
result.push_back (token);
input.erase(0, pos + delimiter.length());
}
result.push_back (input);
return (result);
}
void displayCityMap()
{
string line;
ifstream myfile ("citylocation.txt");
if (myfile)
{
while (getline (myfile, line))
{
vector<string> tokenStringVector = tokenizeString (line, "-");
tokenStringVector[] = tokenizeString(tokenStringVector, ",");
cout << endl;
for (int i=0; i<tokenStringVector.size(); i++)
cout << tokenStringVector [i] << endl;
cout << endl;
}
myfile.close();
}
}
|
Last edited on
You can simply use push_back as you did for the string:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25
|
void displayCityMap()
{
vector<vector<string> > 2d_vector;
string line;
ifstream myfile ("citylocation.txt");
if (myfile)
{
while (getline (myfile, line))
{
vector<string> c = tokenizeString (line, "-");
2d_vector.push_back(tokenStringVector);
tokenStringVector[] = tokenizeString(tokenStringVector, ",");
cout << endl;
for (int i=0; i<tokenStringVector.size(); i++)
cout << tokenStringVector [i] << endl;
cout << endl;
}
myfile.close();
}
}
|
Topic archived. No new replies allowed.