delete from 2d vector and reset position

I have a csv file containing data:

add, 100, 101, 102, 103, 104, 105, 106
add, 200, 201, 202, 203
read, 106
delete, 200
add, 600, 601, 602, 603



In my code, i take the second column to end of each row from the csv file and place in into a int vector. From the data vector, if first column of each row is equals to add, it will add the datas to an empty 2d vector,
For e.g., if my 2D vector is 16x8 colummns, I will have my output as the below after adding, with the <delete, 200> coming after the read line, it will delete all numbers from range 200 to 299 found in the vector and reset to 0. However, in the next line to add <601,602,603> it is supposed to add to the first 0 it detected and it is not working as it should be

my current output:

101 102 103 104 105 106 0 0
0 601 602 603 0 0 0 0
0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0


my expected output:

101 102 103 104 105 106 601 602
603 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0


This is my code
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
		// ------------------------------
		for (int i = 0; i < list.size(); i++) {
			if (list[i][0] == "add") {
				for (j = 2; j < list[i].size(); j++) {
					//For adding of files
					dataLen = list[i].size() - 2; //size of data
					//cout << dataLen << endl;
					//cout << list[i][j] << endl;
					int toInteger = stoi(list[i][j]);
					myvecs[y][x] = toInteger;
					cout << "Adding file" << list[i][1] << " and found free B" << y + 1 << endl;
					cout << "Added file" << list[i][1] << " at B" << y + 1 << "(" << toInteger << ")" << endl;
					++x;
					if (x == myvecs[y].size()) {
						++y;
						x = 0;
						
					}
				}
			}
			//For deleting of files
			else if (list[i][0] == "delete") {
				deleteIdentifier = stoi(list[i][1]);
				minData = deleteIdentifier + 1;
				maxData = deleteIdentifier + 99;
				// Check if nextIter is true, if it is then force row to increment, which means
				// moving it to second row because first row is fully occupied
				if (nextIter) {
					row++;
				}
				// -----------------
				// Change y to row
				for (row; row < diskBlocks; row++) {
					for (int x = 0; x < myvecs[i].size(); x++) {
						if (myvecs[row][x] >= minData && myvecs[row][x] << maxData) {
							myvecs[row][x] = 0;
							count++;
							deleteCounter++; // <--- Increment count
						}
						else {
							continue;
						}

					}
					// Check count equals to vector row size
					// If true, then set nextIter to true, which will force the loop to point to second row
					if (count == myvecs[i].size()) {
						nextIter = true;
						count = 0;
						
					}
					else { // Otherwise set it back to false
						nextIter = false;
						
					}
				}
				
			}
			else {
				//readData
			}
		}


I have tried many different ways and i also have a deleteCounter that counts everytime an elemetn is deleted. How can i achieve this?

Any help is appreciated, thank you.


Last edited on
you should have explained the other operations before, I didn't know that the insertion cell could move left-up.

if you don't have time constraints, for the "add" part create a function find_zero() that'll return you the <x,y> position of the first 0 in your matrix
1
2
3
4
for (int K=0; K<m.size(); ++K)
   for (int L=0; L<m[K].size(); ++L)
      if (m[K][L] == 0)
         return make_pair(K, L);



¿could you explain the problem you are trying to solve? perhaps we can come up with a better approach
The problem i'm trying to solve is after the delete is detected, for e.g delete 200, it would delete 201 to 299. And any add detected after the delete will add to the first detected 0, as shown in the expected output.

may i know what's make_pair?
Last edited on
http://www.cplusplus.com/reference/utility/make_pair/
that way you'll return a structure that holds two values.

nolyc wrote:
You're trying to do X, and you thought of solution Y. So you're asking about solution Y, without even mentioning X. The problem is, there might be a better solution, but we can't know that unless you describe what X is.
I want to know why you are doing this and what kind of solution do you need
i'm doing this for an assignment. I need a add function and a delete function where it adds data read from the csv file into my 2d vectors, before inserting to the 2d vectors, it needs to check if value in 2d vector is 0 before inserting, if not it will go to the next element in the row and so on. as for delete, it willl get the delete block range and delete those that are in the range from the 2d vector, anything that needs to be added after that has to be in sequence, meaning adding to the first 0 it detects.

so if my initial data inserted data was
10 11 12
21 22 23

if delete 20 is read from csv,
it will delete 21-23 and reset the value to 0
10 11 12
0 0 0
if add 31,32,33 is read on the row after the delete 20,
it will add on to the space that was provided from deleting the 21-23
which will look like
10 11 12
31 32 33
Topic archived. No new replies allowed.