如何删除数组中相同的数字并输出?求解
意思是这样的,我有两个数组int arrayA[] = {3,1,3,4,8,9,9,9};
int arrayB[] = {3,1,1,9,9,5,7,2};
然后删除它们相同的部分,所得结果应该是
int arrayA[] = {4,8};
int arrayB[] = {5,7,2};
但我写的程序输出不对,大神们看看怎么修改才是吧。
程序代码:
#include <iostream> #include <map> using namespace std; int main() { map<int,int> mapList; int arrayA[] = {3,1,3,4,8,9,9,9}; int arrayB[] = {3,1,1,9,9,5,7,2}; int aLen = (int)sizeof(arrayA)/sizeof(int); int bLen = (int)sizeof(arrayB)/sizeof(int); //mark object between arrayA for (int i = 0; i < aLen; i++) { map<int,int>::iterator it = mapList.find(arrayA[i]); if (it == mapList.end()) mapList[arrayA[i]] = 0; else mapList[arrayA[i]] = 1; } //mark object between arrayB for (int i = 0; i < bLen; i++) { map<int,int>::iterator itor = mapList.find(arrayB[i]); if (itor == mapList.end()) mapList[arrayB[i]] = 0; else mapList[arrayB[i]] = 1; } //print mapList for (map<int,int>::iterator it = mapList.begin(); it != mapList.end(); it++) cout<<it->first<<"-----"<<it->second<<endl; //delete mark from arrayA for (int i = 0; i < aLen; i++) { map<int,int>::iterator itor = mapList.find(arrayA[i]); if (1 == itor->second)//delete { for (int j = i; j < aLen - 1; j++) arrayA[j] = arrayA[j+1]; aLen--; } } //delete mark from arrayB for (int i = 0; i < bLen; i++) { map<int,int>::iterator itor = mapList.find(arrayB[i]); if (1 == itor->second)//delete { for (int j = i; j < bLen - 1; j++) arrayB[j] = arrayB[j+1]; bLen--; } } //print arrayA and arrayB cout<<"arrayA:"<<endl; for (int i = 0; i < aLen; i++) cout<<arrayA[i]<<" "; cout<<"\narrayB:"<<endl; for (int i = 0; i < bLen; i++) cout<<arrayB[i]<<" "; return 0; }