0

I don't have much experience with vectors and for this problem I need to be able to read in a txt file that contains a grid that looks like this:

enter image description here

I know that I want to put it into a vector of vectors so I have written this so far

vector<vector<char> > map;

I am thinking that I need to read in the txt file by using some loops and the get line function but am unsure how to continue.

user4581301
  • 33,082
  • 7
  • 33
  • 54
Justin Li
  • 9
  • 1
  • what's the problem? just loop your vectors like for (int i = 0; i < vector1.size(); i++) { (int j = 0; j < vector1[i].size(); j++) and inside do whatever map[i][j] needs to do. – Asphodel Jan 20 '22 at 20:51
  • Warning: It looks like you're planning on using `using namespace std;` ([Why you shouldn't](https://stackoverflow.com/questions/1452721/why-is-using-namespace-std-considered-bad-practice)) and coupled with putting the name `map` into play could lead to a collision with the pre-existing `std::map`. Weird, nigh-inscrutable smurf can result. – user4581301 Jan 20 '22 at 20:53
  • @Asphodel consider that the asker may not know the size of the `vector`s ahead of time. Recommending using `[]` could be dangerous. `push_back` is probably a better place to start. – user4581301 Jan 20 '22 at 20:55
  • 2
    Speaking of starting, forget about the `vector` for now. Start by writing a simple program that merely reads the file character by character and writes it to the console. Once you can reproduce the file exactly as it appears in the file on the screen you're ready for the next step of putting it into your `vector` of `vector`s. – user4581301 Jan 20 '22 at 20:56
  • 1
    My browser's not functioning at the moment. Please search the internet for "C++ read 2d (array or matrix)". Always search before posting. – Thomas Matthews Jan 20 '22 at 21:17
  • 1
    @ThomasMatthews Got the old Stack Overflow App working again, did you? – user4581301 Jan 20 '22 at 22:20

1 Answers1

0

Let's consider you have a file called "Pet Sematary.txt" and you have the following quote inside

The soil of a man's heart is stonier, Louis.
A man grows what he can, and he tends it.
'Cause what you buy, is what you own.
And what you own... always comes home to you.

which you want to read line by line and character by character and save to a vector of vectors of char.

#include <fstream>
#include <iostream>
#include <vector>
#include <iterator>
#include <iomanip>

int main() {
    std::ifstream is("Pet Sematary.txt");
    if (!is) {
        std::cout << "Unable to open file!\n";
    }
    std::vector<std::vector<char>> grid;
    for (std::istreambuf_iterator<char> beg(is), end; beg != end; ++beg) {
        std::vector<char> tmp;
        while (beg != end && *beg != '\n') {
            tmp.emplace_back(*beg++);
        }
        grid.emplace_back(tmp);
    }
    for (auto const& line : grid) {
        for (auto const ch : line) {
            std::cout << std::setw(2) << ch;
        }
        std::cout << '\n';
    }
    return 0;
}

Define the ifstream object is and open the given file. Check whether the file exists and If it doesn't print an appropriate message. Define your grid. Loop through the file with istreambuf_iterators. Define the vector tmp which will represent the line you are processing. While the character you are reading isn't end of line grow tmp. When the character you read was end of line '\n' increment beg via for's ++beg to prepare beg for the next line. Grow grid this way until you read end-of-file. When you print grid you will get this:

 T h e   s o i l   o f   a   m a n ' s   h e a r t   i s   s t o n i e r ,   L o u i s .
 A   m a n   g r o w s   w h a t   h e   c a n ,   a n d   h e   t e n d s   i t .
 ' C a u s e   w h a t   y o u   b u y ,   i s   w h a t   y o u   o w n .
 A n d   w h a t   y o u   o w n . . .   a l w a y s   c o m e s   h o m e   t o   y o u .
Dharman
  • 30,962
  • 25
  • 85
  • 135