1

I would like to fill and then return a vector of vector of sets. In other words a matrix that has a set of numbers into itself. I declared:

#include<iostream>
#include<set>
#include<vector>

vector<int> row = vector<int>(4);
vector<vector<int>> matrix = vector<vector< int>>(4, row)

If it would be a vector of vector of vector I would write
vector<vector<vector<int>>> v = vector<vector<vector<int>>>(4, matrix). Maybe is ok to write vector<vector<set<int>>> v; for a vec of vec of sets. But I would put that set<int> into matrix. It's a 4x4 matrix, with 4 possible numbers into it. I'm trying to populate v with numbers from 1 to 4 using v[i][j].insert(f) iterating f from 0 to 4.

How can I return that matrix printing all numbers too? I would expect something like this:

1234123412341234
1234123412341234
1234123412341234
1234123412341234

Extra question

How can I access and delete numbers into an element of the matrix? For example I want to access into the second row and third column. I have 1234 and I want to erase 3 of those 4 numbers. Let's say I want to write only 1 in matrix[2][3]. I would think it's something like v[2][3].find(1) but I'm not pretty sure.

Please, don't suggest me to use other stl containers. The only other stl I can use are vectors, set or map and unordered_set or map.

IlJoker11
  • 11
  • 6
  • [This](https://stackoverflow.com/a/618547/11261546) might be useful. Try first declaring a proper Matrix (with sets inside if you need) and then putting them together I think that would be more clearand you'd write more readable code – Ivan Oct 08 '20 at 09:58

1 Answers1

0

The code can be this:

#include<iostream>
#include<set>
#include<vector>

using namespace std;

int main(){

vector<set<int>> row = vector< set<int> >(4);
vector<vector<set<int>>> matrix = vector<vector< set<int>>>(4, row);

vector< vector< set<int> > >::iterator itCol;

for (itCol = matrix.begin(); itCol != matrix.end(); ++itCol){
    vector<set<int>>::iterator itRow;
    for(itRow = itCol->begin(); itRow != itCol->end(); ++itRow){
        for(int i = 1 ; i <=4; ++i){
                itRow->insert(i);
        }
    }
} 

for (itCol = matrix.begin(); itCol != matrix.end(); ++itCol){
    vector<set<int>>::iterator itRow;
    for(itRow = itCol->begin(); itRow != itCol->end(); ++itRow){
        set<int>::iterator itSet;
        for(itSet = itRow->begin(); itSet != itRow->end(); ++itSet){
                cout << *itSet;
        }
    }
    cout << endl;
} 

};

If you want to delete some element in a set you can use from the set you want:

matrix[row][col].erase(matrix[i][j].find(number_you_want));
Zig Razor
  • 3,381
  • 2
  • 15
  • 35
  • Not sure I understood second cycle of for iterations. What is used for? And what is the difference between `matrix[row][col]` and `matrix[i][j]`? – IlJoker11 Oct 08 '20 at 11:19
  • The second cycle is for print the matrix as you ask – Zig Razor Oct 08 '20 at 12:22
  • Row and col is the index of the matrix – Zig Razor Oct 08 '20 at 12:22
  • What if I would like to read that matrix from a file, having in this file a matrix with some numbers already written (for example in the file in position [1][1] there is already 4 I have to write only number 4 in matrix[i][j]? Like: 1234123412341234 1234**4**12341234 1234123412341234 1234123412341234 – IlJoker11 Oct 08 '20 at 14:52
  • 1
    This is another too long answer, open another question – Zig Razor Oct 08 '20 at 15:54
  • Can I edit this post? Because I can't write another post for 5 days. – IlJoker11 Oct 08 '20 at 21:19
  • @IlJoker11 No, please don't edit your question, an edit like that will be reverted. Posting restrictions exist for a reason, please don't try to circumvent them, that would be against our rules. – Modus Tollens Oct 08 '20 at 21:21
  • 1
    Ok, no problem. – IlJoker11 Oct 08 '20 at 21:43
  • @ZigRazor Can you explain me what does itCol->begin() means? Why is that a "->" instead of a "."? And why "itSet " has a pointer? – IlJoker11 Oct 09 '20 at 08:57
  • i leave you this useful link https://www.geeksforgeeks.org/iterators-c-stl/ – Zig Razor Oct 09 '20 at 09:00
  • @ZigRazor Writing `return itSet`(without pointer): `[Error] could not convert 'itSet' from 'std::_Rb_tree_const_iterator' to 'std::vector > >'`. So it doesn't return a vector>>. I need it (as I said in title) because I have to make an accessor with it. – IlJoker11 Oct 10 '20 at 16:58
  • Please insert an example... in anycase you can't return a iterator... – Zig Razor Oct 11 '20 at 08:51
  • I would like to return all elements in my vector>>. Can't I do it? I was just printing it to see if it was correct. @ZigRazor – IlJoker11 Oct 11 '20 at 09:40
  • To return all element just return the `matrix`. then if you want access them you can use direct access or iterator, the iterator is the best choice in the mjor of the case. More information on [How to use Iterator](https://www.geeksforgeeks.org/iterators-c-stl/) . – Zig Razor Oct 11 '20 at 11:02
  • What do you mean with direct access? Because returning `matrix` as you said you return the whole matrix. But what if I want to return all matrix knowing indices? A method like `vector > > GetMatrix(int i, int j) {...}`? What should I write into curly brackets? However I understood how to use`.begin()` and `.end()` @ZigRazor – IlJoker11 Oct 11 '20 at 15:04
  • Your mistake is the signature if you want to return element (i,j) you can declare the function return as set& and you can return *yourIterator – Zig Razor Oct 11 '20 at 16:14
  • Or simply the direct access to the matrix. Example matrix[i][j] – Zig Razor Oct 11 '20 at 16:16
  • @ZigRazor Ok, but if I'm iterating on a `set` I can't no more iterate on a `matrix[i][j]`. Am I wrong? Do you mean declaring something like this? `set& GetMatrix() { set::iterator it; for(it = matrix.begin(); it != matrix.end(); ++it) { return *it;} }` I got an error because as you said I can't return an iterator – IlJoker11 Oct 11 '20 at 17:13
  • In this case the error is that you are iterating on the first vector, so on the type vector< set >. To iterate over set you need another nested loop as in my answer – Zig Razor Oct 12 '20 at 04:26