0

This is a beta test code of the algorithm Malgrange, I need to create array Y1 from C0... (Y1 = C0 - X0)

#include <iostream>

using namespace std;

int main()
{
// create array C0
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";

//create array X0 
string** masiv_X0=new string*[1];
masiv_X0[i]=new string [2];

masiv_X0[0][0]="X";
masiv_X0[0][1]="Z";

//create array Y1 = C0 - X0 (remove from C0 elements X0)
bool flag;
string** masiv_Y1=new string*[3];

for (int i=0;i<3;i++)
    masiv_Y1[i]=new string [2];

for (int i=0;i<3;i++)
{
    flag=true;
    for (int j=0;j<3;j++)
    {
        if ((masiv_C0[i][0]==masiv_X0[j][0])&&(masiv_C0[i][1]==masiv_X0[j][1]))
        {
            flag=false;
            break;
        }
    }
    if (flag)
    {
        masiv_Y1[i][0]=masiv_C0[i][0];
        masiv_Y1[i][1]=masiv_C0[i][0];
    }
}

for (int i=0;i<3;i++)
{
    for (int j=0;j<2;j++)
    {
        cout<<masiv_Y1[i][j];
    }
    cout<<endl<<endl;
}}

Of course the algorithm is not finalized and most of the code is not here, but the problem is this error, I can not create an array without the elements that are contained in a subset of the other elements

Merk30
  • 5
  • 2
  • 1
    Do yourself a favor and learn how to use the STL container classes instead of raw arrays and pointers. – PaulMcKenzie Oct 04 '16 at 21:13
  • "`std::string** masiv_c0 = new std::string*[3]`" - don't use dynamically allocated C-arrays. If you want to have a matrix of `std::string` instances then use `std::vector>` and save yourself a big headache whilst doing so. – sjrowlinson Oct 04 '16 at 21:13

1 Answers1

0

If you posted what the proper output should be, it would have been helpful.

However, given your description, it would be better served if you used STL container and algorithms to do any sort of removal of elements, since arrays cannot be resized. For example, the std::vector class serves as a dynamic array of elements.

Your code attempts to "resize" the array by writing over previous elements, which is not necessary when using std::vector, since using vector, you will actually be removing elements, not just merely writing over items in the array.

Here is an implementation of your code that uses std::vector, and a few STL algorithms to do the work (again, I am going by your description in your post):

First we use the requisite headers:

#include <vector>
#include <string>
#include <algorithm>

Next, we create some typedefs for convenience.

typedef std::vector<std::string> String1D;  // a 1 dimensional "array" of string
typedef std::vector<String1D> String2D;  // a 2 dimensional "array" of string

Now that we have the above, it is really simple to create the two-dimensional arrays, all without using new[]:

int main()
{
    // create array C0
    String2D masiv_C0(3, String1D(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";

    //create array X0 
    String2D masiv_X0(1, String1D(2));
    masiv_X0[0][0]="X";
    masiv_X0[0][1]="Z";

Next, we simply create a "Y" 2-dimensional array by first starting out with the masiv_C0 array:

String2D masiv_Y1 = masiv_C0;

Once we have that, then we can remove the elements from masiv_Y1 easily using a loop and using erase / remove idiom:

for ( size_t i = 0; i < masiv_X0.size(); ++i)
{
    for ( size_t j = 0; j < masiv_X0[i].size(); ++j)
    {
        auto& str = masiv_X0[i][j];
        for (size_t cur = 0; cur < masiv_Y1.size(); ++cur)
        {
            auto iter = std::remove(masiv_Y1[cur].begin(), masiv_Y1[cur].end(), str);
            masiv_Y1[cur].erase(iter, masiv_Y1[cur].end());
        }
    }
}

So basically, for each string in the masiv_X0 array, we go through each row of the masiv_Y array, searching and removing the masiv_X0 string. This is accomplished by using the std::remove function, and then to rid the array of the elements, the vector::erase function is used.

Here is a live example

Note how we also output the final results in the example. The size() member function is used instead of hard-coding the number of rows and columns.

Community
  • 1
  • 1
PaulMcKenzie
  • 34,698
  • 4
  • 24
  • 45
  • @Merk30 You need to fix your compiler that CodeBlocks is using. CodeBlocks is an IDE, not a compiler. You may be using a very old version of g++. Also, "Linux" is not a C++ compiler, it is an operating system. Basically you need to properly know and identify the tools that you're using, number one being the exact compiler and version you're using to build your code. The code compiles without error using Visual Studio 2015, and various versions of g++ and clang [as seen here](http://rextester.com/VQTWZ20327) – PaulMcKenzie Oct 05 '16 at 03:03
  • It seems you forgot `#include `. Second [here is your code using `std::vector`](http://ideone.com/C2Hed6). – PaulMcKenzie Oct 05 '16 at 03:16