top button
Flag Notify
    Connect to us
      Site Registration

Site Registration

How to swap two arrays of unequal length using C/C++?

+2 votes
592 views
How to swap two arrays of unequal length using C/C++?
posted Nov 4, 2014 by anonymous

Share this question
Facebook Share Button Twitter Share Button LinkedIn Share Button
How can you swap two arrays which has different size wont it result in segmentation fault.

1 Answer

0 votes
---- main.cpp ----
#include <iostream>
#include <map>

int main()
{
using namespace std;
int a[] = { 1, 3, 5, 600, 600, 123, 145 };
int b[] = { 3, 3, 1, 600, 5000, 145 };

map<int, pair<bool, bool> > m;

for (int i = 0; i < sizeof(a) / sizeof(a[0]); ++i)
{
pair<bool, bool>& p = m[a[i]];
p.first = true;
}

for (int i = 0; i < sizeof(b) / sizeof(b[0]); ++i)
{
pair<bool, bool>& p = m[b[i]];
p.second = true;
}

for (map<int, pair<bool, bool> >::const_iterator cit = m.begin(); cit !=
m.end(); ++cit)
{
int key = cit->first;
pair<bool, bool> const& p = cit->second;
cout << key << " " << p.first << " " << p.second << endl;
}

return 0;
}

/* Output:

1 1 1
3 1 1
5 1 0
123 1 0
145 1 1
600 1 1
5000 0 1

thus:
1 is in a and b
3 is in a and b
5 is in a not b
....
5000 is not in a but in b
etc etc
*/
--------------------
answer Dec 1, 2014 by Shivaranjini
This may not be answering the original query
...