Best data structure store sets of numbers from a text file - c++

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({}).

Related

fstream deleting specific start to end rows

Im a noob programmer currently making a small family database using cpp but i have trouble deleting a family from the the list...
My list looks something like this
start-of-family 1
jim
joe
bob
sam
end-of-family 1
start-of-family 2
rob
max
end-of-family 2
start-of-family 3
sue
tom
kim
end-of-family 3
If i wanted to delete family 1, I would locate start-of-family 1 and end-of-family 1. Then run a loop but how do i locate it if the user only inputs an int to represent a family number. Also how do i make the succeeding family numbers deduct by 1 so that family 2 will be 1 and family 3 will be 2.
thanks a lot
If I were doing this problem I would start by making each family into a vector of names. Then, I would create a vector containing those family vectors.
The result would look something like:
{
<(jim), (joe), (bob), (sam) >
<(rob), (max) >
<(sue), (tom), (kim) >
}
Then, if the user wants to delete one of the families, you can use vector.remove(n) where n is the index of the family to be removed.
This sounds like a school or text book assignment. Have you gotten to vectors yet? Where are the names coming from? Are you hard coding them into the list? Or reading them from a .txt file? What kind of list structure are you storing them in right now?
i realized that clearing the db and updating it with what ive got is way easier than modifying the db and updating my program

Looping a program to cover all possible cases c++

My program reads in 4 terminal arguments:
a text file
"bubble", "quick", or "brick" corresponding to the sort the user wants
a number between 2 and 392 corresponding to the amount of items from the text file to sort through, and
"verbose" or "terse" where verbose prints the vector once it is sorted and where terse prints the amount of swaps used to sort the data.
For example:
./SortAutoData auto-mpg.data.txt bubble 100 terse
would print out the amount of sorts needed to do a bubblesort on the first 100 items from auto-mpg.data.txt.
What I need to do is find the number of swaps for each of the three sorts for each amount of elements 2 through 392. Is there a way to increment the amount of data without completely altering my code?
I tried doing this: for i in {2..392}; do echo ./SortAutoData auto-mpg.data.txt bubble ${i} terse; done but it doesnt print exactly how I would like. Ideally, i would like for it to print:
(sorts for 2 data items)
(sorts for 3 data items)
(sorts for 4 data items)
(sorts for 5 data items)
(sorts for 6 data items)
etc....
Thanks in advance for any help anyone can provide!

power function and arrays in c++

I'm trying to write a function that will take an array or vector and have its values taken to a "power of" and then display it's values. I'm not too familiar with arrays but simply put I'm trying to create something like
n = {2^1, 3^1, 5^1,2^2,3^2,5^2,....}
the "power of" is going to be looped.
I then plan to sort the array, and display 1500th term.
this problem corresponds to prime number sequence only divisible by 2 , 3 & 5;
I'm trying to find a more time efficient way than just if statements and mod operators.
If I remember correctly this is the Ugly Numbers problem I've faced some years ago in the UVa.
The idea to solve this problem is to use a priority queue with the numbers 2, 3 and 5 as initial values. At each step remove the topmost value t and insert the values 2*t, 3*t and 5*t in the priority queue, repeat this steps till the 1500th term is found.
See this forum for more info: http://online-judge.uva.es/board/viewtopic.php?t=93

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.

Inputting and Outputting Multidimensional Vectors w/ Binary

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.