| | |
How do I read a text file into a 2D vector?
Please support our C++ advertiser: Intel Parallel Studio Home
![]() |
•
•
Join Date: Sep 2007
Posts: 15
Reputation:
Solved Threads: 0
Guys,
Your help will be appreicated in this matter.
i have to read a text file which has rows and columns into a vector. The text file could be of any size.
000000000
010010001
010010011
001001001
Now, how do I upload this text file on to a 2D vector?
so far, I have come up with this code:
I don't know if this is correct at all. I am sorry guys, bear with me, I am new to the whole STL thing.
Any help would be appreciated.
Your help will be appreicated in this matter.
i have to read a text file which has rows and columns into a vector. The text file could be of any size.
000000000
010010001
010010011
001001001
Now, how do I upload this text file on to a 2D vector?
so far, I have come up with this code:
C++ Syntax (Toggle Plain Text)
ifstream in ("block.in"); ofstream out("block.out"); vector<vector<int>> block; for (int i = 0; i < ???; ++i) block.push_back(*istream_iterator<int>(in));
I don't know if this is correct at all. I am sorry guys, bear with me, I am new to the whole STL thing.
Any help would be appreciated.
The rows would appear to be newline delimited; columns appear per character. Push back each column in a particular row, then when you are done with the row push it back.
"One of the methods used by statists to destroy capitalism consists in establishing controls that tie a given industry hand and foot, making it unable to solve its problems, then declaring that freedom has failed and stronger controls are necessary." --Ayn Rand
•
•
Join Date: Sep 2007
Posts: 15
Reputation:
Solved Threads: 0
Thanks Dave, it gave me a breakthrough.
ok, so now I used the getline function to get the lines and insert into the vector, however, now that the row has been filled, how do i increment the columns?
This is the code I have so far, please bear with me.
I know that I have to use a 'for' loop before I implement the push back statement. However, I do not know where to restrict or end my for loop. Got any ideas
ok, so now I used the getline function to get the lines and insert into the vector, however, now that the row has been filled, how do i increment the columns?
This is the code I have so far, please bear with me.
C++ Syntax (Toggle Plain Text)
#include <fstream> #include <vector> // for vector #include <iomanip> // for setprecision #include <numeric> #include <string> #include <sstream> using namespace std; void main() { string s; ifstream in ("block.in"); ofstream out("block.out"); vector<vector<int>> block; while(getline(in,s)) block.push_back(*istream_iterator<vector<int>>(in)) }
I know that I have to use a 'for' loop before I implement the push back statement. However, I do not know where to restrict or end my for loop. Got any ideas
For input I'm kinda working with something like this.
C++ Syntax (Toggle Plain Text)
std::vector<std::vector<int> > array; // // Input // std::string line; while ( std::getline(file, line) ) { std::vector<int> row; char ch; std::istringstream ss(line); while ( ss >> ch ) { row.push_back(ch - '0'); } array.push_back(row); }
"One of the methods used by statists to destroy capitalism consists in establishing controls that tie a given industry hand and foot, making it unable to solve its problems, then declaring that freedom has failed and stronger controls are necessary." --Ayn Rand
•
•
Join Date: Sep 2007
Posts: 15
Reputation:
Solved Threads: 0
Thanks for that as well, Dave. You are slowly becoming my favorite person ever!!
However, I wanted to ask you, what specific function does the char ch provide in this code?
and also would my code go tits up if I have this:
?
However, I wanted to ask you, what specific function does the char ch provide in this code?
and also would my code go tits up if I have this:
C++ Syntax (Toggle Plain Text)
vector<int> row; char letter; istringstream ss (row); while(letter >> ss){ row.push_back(*istream_iterator<int>(in)); } block.push_back(row);
?
•
•
•
•
However, I wanted to ask you, what specific function does the char ch provide in this code?
A string is read into line. To push back each represented int, first each character is extracted from the string (using the stringstream) and placed in ch. Then an int is created and pushed back.
"One of the methods used by statists to destroy capitalism consists in establishing controls that tie a given industry hand and foot, making it unable to solve its problems, then declaring that freedom has failed and stronger controls are necessary." --Ayn Rand
•
•
Join Date: Sep 2007
Posts: 15
Reputation:
Solved Threads: 0
I now have a problem with the compiler on that code:
Noe, the first line gives me a C2664 error while the second line gives me tons of C2784 errors. Somehow I think we are implicitly trying to insert one variable type into another. How do you suggest I get rid of the compile errors?
C++ Syntax (Toggle Plain Text)
istringstream ss (row); while(ch >> ss){
Noe, the first line gives me a C2664 error while the second line gives me tons of C2784 errors. Somehow I think we are implicitly trying to insert one variable type into another. How do you suggest I get rid of the compile errors?
I was trying to come up with some fancy output using std::copy, a la this, but the syntax eludes me.
Relevant portions:
I'm gonna hafta break down and reconfigure STLfilt to try to discern my error messages.
Relevant portions:
c++ Syntax (Toggle Plain Text)
std::ostream& operator<< ( std::ostream &os, const std::vector<int> &row ) { std::copy(row.begin(), row.end(), std::ostream_iterator<int>(std::cout, " ")); return os; }
c++ Syntax (Toggle Plain Text)
std::copy(array.begin(), array.end(), std::ostream_iterator<std::vector<int> >(std::cout, "\n"));
"One of the methods used by statists to destroy capitalism consists in establishing controls that tie a given industry hand and foot, making it unable to solve its problems, then declaring that freedom has failed and stronger controls are necessary." --Ayn Rand
![]() |
Similar Threads
- Read text file to a certain point (C#)
- read text file (C)
- read text file (C#)
- using a "for" loop to read a text file (VB.NET)
- how do i read the last line of a text file? (Python)
- Text File Input and manipulation (C++)
Other Threads in the C++ Forum
- Previous Thread: matrix division
- Next Thread: Reading a Registry Key path from a file
| Thread Tools | Search this Thread |
api array arrays beginner binary bitmap c++ c/c++ calculator char char* class classes coding compile compiler console conversion convert count data database delete desktop developer directshow dll dynamic email encryption error file forms fstream function functions game generator getline google graph homeworkhelper iamthwee ifstream input int integer java lib linkedlist linker linux loop looping loops map math matrix memory multiple news node number numbertoword output parameter pointer problem program programming project proxy python random read recursion recursive reference return rpg sorting string strings struct template templates test text tree unix url vector video visualstudio win32 windows winsock word wordfrequency wxwidgets






