views:

55

answers:

1

The original code i wrote uses these arguments:

int m = size of sorted list 1

int n = size of sorted list 2

int A[] = sorted list 1

int B[] = sorted list 2

int C[] = merged list of 1 and 2

I was asked to add this code to an existing file that uses these different arguments:

IntVectorIt start1

IntVectorIt end1

IntVectorIt start2

IntVectorIt end2

IntVectorIt start3

These variables are defined here (n is size of first sorted array, m is size of second sorted array):

typedef vector<int> IntVector;

typedef IntVector::iterator IntVectorIt;

IntVector Vector1(n);
IntVectorIt start1,end1,it1;
start1 = Vector1.begin();
end1 = Vector1.end();

IntVector Vector2(m);
IntVectorIt start2,end2,it2;
start2 = Vector2.begin();
end2 = Vector2.end();

IntVector Vector3(n+m);
IntVectorIt start3,end3,it3;
start3 = Vector3.begin();
end3 = Vector3.end();

//--The variables for my version of merge
IntVector MyVector(n+m);
IntVectorIt mystart,myend,myit;
mystart = MyVector.begin();
myend = MyVector.end();

My merge code:

void mymerge(int m, int n, int A[], int B[], int C[])
{
  int i, j, k = 0;

  while (i < m && j < n)
  {
        if (A[i] <= B[j])
        {
              C[k] = A[i];
              i++;
        }

        else
        {
              C[k] = B[j];
              j++;
        }

        k++;
  }

  if (i < m)
  {
        for (int p = i; p < m; p++)
        {
              C[k] = A[p];
              k++;
        }
  } 

  else
  {
        for (int p = j; p < n; p++)
        {
              C[k] = B[p];
              k++;
        }

  }

}

If anyone could help me figure out how i could take those iterators as arguments, it would help me out a ton. Thank you in advance.

+2  A: 

Since it sounds like homework, I won't write the whole solution. However, here are some suggestions on migrating mymerge:

Change the signature to

void mymerge(
    IntVectorIt aStart,
    IntVectorIt aEnd,
    IntVectorIt bStart, 
    IntVectorIt bEnd,
    IntVectorIt cStart, 
    IntVectorIt cEnd
    );

Change the running indices to iterators, e.g.

IntVectorIt i = aStart;

Change the loop stopping condition to use iterators, e.g.

i != aEnd
ArunSaha
Steve Jessop
thank you to both of you... i just finished with this help :D