Inputting and Outputting Multidimensional Vectors w/ Binary - c++

I'm trying to create a simple class for maps in my game. Maps are 2 dimensional vectors made of "tile" structs. I'm trying to figure out the best way to input/output these tiles in rows.
For example, I used to use a .txt based loading format. The .txt file would look like this:
1 0 2 0 0 0 2 4 5 6 3
2 4 5 0 0 0 2 0 0 3 4
0 3 5 2 5 3 0 5 5 3 4
0 2 0 5 0 6 0 5 7 8 4
I would then go line by line to find the ID integer of each tile. 1 would represent grass, 0 would mean water, and so on. When the parser would reach the end of a line, it would skip down to the next line of ints.
Now I'm trying to do this via fwrite and fread with binary files and structs instead of ints. How would I go about doing this? All I've seen is how to store an array of structs in a binary file, not how to store a multidimensional array of structs. Any ideas?
EDIT: Yes, I could just store the 2D vector in the file, but that wouldn't allow me to do seamless map loading, which I need. I have large map files, so having 100k tiles loaded at once would hog CPU.

If you want your map to be human readable, you need to write a parser.
However, if you don't care, you should do something completely different: Use serialization, e.g. from Boost.
A third alternative for your case, which is quite common for games, is to store the map as an image file. You can then edit the map in your favorite image manipulation program, and it is easily interpreted by your program.
Many libs can read image data for you (and present it to you as a 2-D array), and in a game you maybe have something like that already in place.

Related

Continue writing to a file with max size in C++

I want to write to a text file with limited size (1KB for example)
and when the file reaches the maximum size I want the first half to be removed and continue writing to the file (append to what remained after removing first half).
is this possible in C++?
for example:
file that reached maximum size:
1 2 3 4 5 6
and I want to continue writing [7,9]
the new file will look like:
4 5 6 7 8 9
You could use a dedicated logging library for example spdlog.
https://github.com/gabime/spdlog
It has a ton of features, including rotating logs.
You can define how big a log file should be, and how many logfiles you want. It can automatically discard the older logs.
If you really want to write it yourself, you have to
keep the content you want to keep in memory, e.g. in a ring buffer.
Whenever something is added to that buffer, you have to rewrite the file. And then flush it, otherwise it is lost if the program crashes.
This can have a big performance impact, so hande with care.

Best data structure store sets of numbers from a text file

I have an assignment where I need to find minimum set cover of some points. I want to be able to store each row of numbers in an individual sets but I do not know the best data structure or approach to do this. I have the number of rows there will be. For example, the .txt file will look like this:
1 2 3 4 5 6
5 6 8 9
1 4 7 10
2 5 7 8 11
3 6 9 12
10 11
Is there a way to dynamically create multiple data structures to store each row of numbers? I was thinking something that works like this if it exists:
list<int> myList[6]; // create 6 lists
myList[0].insert(num); // insert numbers into this list
myList[1].insert(num); //insert numbers into the second list
I do not want to individually create lists because in a .txt file, there can be up to 300 sets of numbers.
edit: my main issue is figuring out how to dynamically create some data structure, preferably if it works with std::set_union since it looks useful to my assignment
If you want to control the number of lists programmatically, you can use a std::vector of datasets. So in your case the declaration would be
std::vector<std::list<int>> lists(6);
Adding new, empty lists to the lists set is done by
lists.push_back({}).

c++ vector of vectors size of the next column

I am writing a code to do some template matching using cv::matchTemplate but I have run into some problems with the 2-dimensional vector of vectors (vov) I created which I have called vvABC. At the moment, my vov has 10 elements which can change based on the values I pass while running the code.
My problem is moving from one column in my vov to the next so I can calculate the size. From my understanding of how vov works, if I have my elements stored in my vov as:
C_A C_B
0 0
1 1
2 2
3
4
5
6
To calculate the size of the first column, I should simply do something like:
vvABC[0].size() to get the size of the first column (which would give 3 in this case) and vvABC[1].size() to get the size of the second column (which would give 7). The problem I am now faced with is both of them give '3' in both cases which is obviously wrong.
Can someone please help me out on how I can get the correct size of the next column?
I stored my detections in my vvABC, now I want to match them one at a time.
It seems like you made a mistake here:
for (uint iCaTemplate = iCa + 1; iCaTemplate < vvABC[iCa].size(); ++iCaTemplate) {
iCa is an index on the 'first level' of vector (of size 2 in your example above), i.e. columns, and you use it to go through the elements of the 'second level' of vector, i.e. rows.
Thanks a lot guys, esp. JGab, after several debug outputs, I finally found that my vector of vectors wasn't being filled up the way I thought it was...thanks once more and my apologies for my belated response.

Load textfile in sparse format using Eigen

I am new to Eigen and have limited experience in C++. I have a file which is represented in sparse format (like in LIBSVM) and I want to load the data into a matrix using Eigen. Can someone tell me how to do it ? If you can share your code, it will be really helpful.
So, the file has contents like :
1:13 4:56 9:1
2:45 3:12 5:12 7:2
I want to load this into a matrix using Eigen.
You can use our loadMarket function as an example. The market format looks like this:
%%MatrixMarket matrix coordinate real general
rows cols nnz
1 1 13
1 4 56
1 9 1
2 2 45
2 3 12
2 5 12
2 7 2
with rows, cols, and nnz replaced by the actual number of rows, columns and non-zeros.
Basically, the easiest is to fill a std::vector of triplet (i,j,value), and call SparseMatrix::setFromTriplets to create the sparse matrix.
Another approach would be to write a simple script transforming your format to the market format and call the Eigen's loadMarket function.

Calculating and correcting bit errors using Hidden Markov Model in C++

I am a student and new to C++. It would be great if somebody could help me write a program. Here is what it is suppose to do;
1- generates a Pseudo Random Bit Sequence (PN9 or PN15) 9 or 15 bit length.
2- saves the bit sequence from step 1 into a bit array/buffer and displays the array.
3- calculates the transition probabilities for
1. 0 --> 0
2. 0 --> 1
3. 1 --> 0
4. 1 --> 1
4- asks the user to input a bit sequence
5- introduces some noise.. i.e. flips some of the input bits
6- calculates and corrects the BIT errors based on the transition probabilities calculated in step-3
can any body guide or share his work with me on this?
You'll probably find many of the functions in the GNU Scientific Library (GSL) useful for your work. Apart from that, you'll have to do some work and then ask a specific question to get guidance.