Transfering data from a text file into a 2D vector array in c++

In summary, the conversation includes a discussion about transferring data from a file into a 2D vector array. The dimensions of the array must be detected dynamically and there are two files involved in the code. There is an attempt to read the files and push the data into the array, but the program does not compile due to a type mismatch error.
  • #1
Absolutism
28
0

Homework Statement



I am attempting to transfer the data from a file into a 2D vector array. The data look like this:
1 2 3
4 5 6
7 8 9

However, the dimensions must be detected dynamically.

Homework Equations


The Attempt at a Solution


There are two files in this code, but I attempted to transfer only one of them. I tried to read line by line and then take each word and push it into the array of vectors. The program does not compile though.

#include <iostream>
#include <fstream>
#include <string>
#include <sstream>
#include <vector>

using namespace std;

int main ( ) {
ifstream infile1;
ifstream infile2;
infile1.open ("Array.txt");
infile2.open ("Pattern.txt");
if (!infile1||!infile2) {cout<<"error"; return -1;}

string line; string word;
vector<vector<string>>a();while (!infile1.eof())
{
getline (infile1, line);
istringstream stringline (line);
while ( stringline >> word ) {
istringstream strw (word);
a.push_back(strw); //the error is here. The "a." specifically.
}
return 0;
}
 
Physics news on Phys.org
  • #2
Hi Absolutism! :smile:

You're trying to push_back() an istringstream onto a vector of vector<string>.
That's not possible.
The type does not match...

Btw, don't you get a compilation error on the following line?
Code:
vector<vector<string>>a();
 
Last edited:
  • #3
Oh yes, I got it. Thank you!
 

Related to Transfering data from a text file into a 2D vector array in c++

1. How do I read data from a text file into a 2D vector array in C++?

To read data from a text file into a 2D vector array in C++, you can use the ifstream class and its getline() function to read each line of the text file. Then, you can use the stringstream class to split the line into individual values and insert them into the vector array.

2. What is a 2D vector array in C++?

A 2D vector array in C++ is a data structure that stores elements in a row-column format. It is essentially a vector of vectors, where each inner vector represents a row of elements. This allows for easy manipulation and access of data in a 2D grid-like structure.

3. How do I declare and initialize a 2D vector array in C++?

To declare and initialize a 2D vector array in C++, you can use the vector class and its push_back() function to add rows of data. You can also use the resize() function to specify the number of rows and columns in the vector array.

4. What are the advantages of using a 2D vector array in C++?

One advantage of using a 2D vector array in C++ is its flexibility in storing and accessing data in a grid-like structure. It also allows for dynamic resizing, meaning the size of the vector array can be changed during runtime. Additionally, vector arrays have built-in functions for sorting, searching, and other data manipulation operations.

5. How do I output data from a 2D vector array to a text file in C++?

To output data from a 2D vector array to a text file in C++, you can use the ofstream class and its open() and close() functions to create and close the output file. Then, you can use a loop to iterate through the vector array and use the file << data syntax to write the data to the file.

Similar threads

Replies
16
Views
2K
Replies
15
Views
2K
Replies
2
Views
4K
Replies
3
Views
2K
Replies
1
Views
1K
Replies
4
Views
2K
Replies
1
Views
2K
Replies
6
Views
3K
Back
Top