This question has already been answered:

There is an array masiv_C0 = {{A, B}, {A, C}, {B, C}}

There is an array masiv_X0 = {{A, B}}

It is necessary to create the third array of the first two:

masiv_Y1 = masiv_C0 - masiv_X0 = {{A, C}, {B, C}}

Update: Here is the right decision

Reported as a duplicate by Kromster , aleksandr barakin , αλεχολυτ , Kirill Stoianov , Bald on Oct 10 '16 at 7:09 .

A similar question was asked earlier and an answer has already been received. If the answers provided are not exhaustive, please ask a new question .

  • problem not solved - Valera Kvip
  • Could you clearly state your task? How should X0 be created, which means C0 without X0 elements - remove the corresponding substring? whole item? And yet - you need to do it all through the dynamic memory? - Harry
  • 1. X0 are elements of the array C0 which are contained in a subset of the other elements of C0; Example: C0 = {(a, bc); (fg, jkl); (agh, bcd)} => X0 = {(a, bc)} - Merk30
  • 2. Task, remove elements of X0 from C0 - Merk30
  • 3. that is, Y0 = {(a, bc); (fg, jkl); (agh, bcd)} - {(a, bc)} = {(fg, jkl); (agh, bcd)} - Merk30

1 answer 1

Since the values ​​in the arrays are not repeated (see comments), I recommend using the appropriate data structure (unsorted set) to solve the problem, which makes the solution trivial:

 unordered_set<string> C0{"AB", "C", "X", "Z"}; unordered_set<string> X0{"GG", "C"}; unordered_set<string> Y0{C0}; // инициализируем Y0 С0 for(string s : X0){ Y0.erase(s); } //удаляем из Y0 элементы Х0 

Update: it turned out from the comments below that a string** needed at the input and output, so I propose to make functions that convert string** to multiple lines and back. Below is the complete code:

 #include <iostream> #include <unordered_set> #include <string> #include <memory> using namespace std; string** set_to_2string_ptr(unordered_set<string> data, int x, int y){ string** result = new string*[x]; for(int i(0); i < x; ++i){ result[i] = new string[y]; } auto ite = data.begin(); for(int i(0); i < x; ++i){ for(int j(0); j < y; ++j){ result[i][j] = (ite != data.end()) ? *ite++ : " "; } } return result; } unordered_set<string> str_2ptr_to_set(string** str, int x, int y){ unordered_set<string> result; for(int i(0); i < x; ++i){ for(int j(0); j < y; ++j){ result.insert(str[i][j]); } } return result; } int main(){ string** masiv_C0 = new string*[3]; for (int i = 0; i < 3; i++) masiv_C0[i] = new string [2]; masiv_C0[0][0] = "AB"; masiv_C0[0][1] = "C"; masiv_C0[1][0] = "X"; masiv_C0[1][1] = "Z"; masiv_C0[2][0] = "XY"; masiv_C0[2][1] = "ZQ"; unordered_set<string> C0 = str_2ptr_to_set(masiv_C0, 3, 2); unordered_set<string> X0{"GG", "C"}; unordered_set<string> Y0{C0}; for(string s : X0){ Y0.erase(s); } string** masiv_Y1 = set_to_2string_ptr(Y0, 3, 2); return 0; } 

P.S. Do not forget to monitor the allocated memory and release it. P.P.S. Then you can reduce this code by combining converting functions within one, which, in fact, creates a difference of sets.

  • Yes, I like your version, but everything is somewhat more complicated for me, my array is the top of the graph, and my entire program is built to work with a two-dimensional array of type string. That is, removing X0 elements from Y0 is only one of the operations that I have to do in the processing of graph vertices .... You can certainly try to work with structures, but it will take more time since I have no idea of ​​implementing them in my program. .. - Merk30
  • for example, in the course of computing, we get the following array: C0 = {(a, adf), (b, bcf), (c, bc), (d, adf), (e, e), (f, abdf), (ab , f), (ad, adf), (af, adf), (bc, bc), (bd, f), (bf, bf), (cf, b), (df, adf)}. - Merk30
  • X0 are the elements of the array C0 which are contained in the subset of the other elements C0: X0 = {(a, adf), (c, bc), (d, adf)} - Merk30
  • I need to calculate Y1 = C0 - X0 = {(b, bcf), (e, e), (f, abdf), (ab, f), (ad, adf), (af, adf), (bc, bc ), (bd, f), (bf, bf), (cf, b), (df, adf)} - Merk30