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.