为什么当代码尝试在 STL 映射中查找键时出现分段错误



给定一个整数数组 A[]。任务是显示有序对正整数(X, Y)使得 X 在 A[] 中至少出现 Y 次和 Y 在 A 中至少出现 X 次

例如,输入A[] = { 1, 1, 2, 2, 3 } - 程序使用此输入运行良好。有序对为 -> { [1, 1], [1, 2], [2, 1], [2, 2] }

例如输入 : A = { 3, 3, 2, 2, 2 } -- 程序崩溃并出现分段错误有序对是 -> { [3, 2], [2, 2], [2, 3] }

在函数 getDesiredSet() 中,在以下行中,查找失败并出现分段错误。

it1 = freqMap.find(itr->first); it2 = freqMap.find(itr->second);

# include "iostream"
# include "map"
# include "iterator"
# include "set"
using namespace std;
typedef pair<int, int> pairs;
map<int,int> getFreqMap(int arr[], int n){
map<int,int> freqMap;
map<int, int>::iterator it;
for (int i=0; i<n; i++){
    //if( freqMap.find(arr[i]) != freqMap.end()){
    if (freqMap.count(arr[i])){
        it = freqMap.find(arr[i]);
        it->second = freqMap.count(arr[i]) + 1;
        freqMap[arr[i]] = freqMap.count(arr[i]) +1;
    }
    else{
        freqMap.insert(pair<int,int> (arr[i], 1) );
    }
}
cout << "nFrequency Map - Element : Freqn";
for (it = freqMap.begin(); it!= freqMap.end(); it++){
    cout << it->first << " : " << it->second << endl;
}
return freqMap;
}
/* getPairSet finds out all the possible ordered pairs with the input numbers*/
/* and stores them in a set(so that there are no repeatation) */
set<pair<int,int> > getPairSet(int arr[], int n){
    set <pairs> s;//setOfPairs ;
    for (int i=0; i<n; i++){
        for (int j=0; j<n-1; j++){
            pairs p = make_pair(arr[i],arr[j]);
            s.insert(p);
        }
    }
    set< pairs >:: iterator itr;
    cout << "nPossible Pairs out of the given numbers in the array:n";
    for (itr = s.begin(); itr != s.end(); itr++){
        cout <<"(" <<(*itr).first << "," << (*itr).second <<") " ;
    }
    return s;
}
/* getDesiredSet removes those order pairs which do match the criteria*/
/* in other words, it removes those order pairs in which */
/* either the frequency of 1st item is less than 2nd item */
/* or frequency of 2nd item is less than 1st item */
set<pairs > getDesiredSet(set<pairs > pairSet, map<int, int> freqMap){
    set<pairs > ::iterator itr, itrEnd;
    map<int, int>:: iterator it1, it2;
    for (itr = pairSet.begin(); itr != pairSet.end(); itr++){
        it1 = freqMap.find(itr->first);
        it2 = freqMap.find(itr->second);
        if ((it1->second < itr->second) || (it2->second < itr->first)){
            itrEnd = itr;
            std::advance(itrEnd , 1);
            pairSet.erase(itr, itrEnd);
        }
    }
    return pairSet;
}
int main(){
map<int, int> freqMap;
int arr[] = { 3, 3, 2, 2, 2 };
cout << "nInput Array Elements:n";
int n = sizeof(arr)/sizeof(arr[0]);
for (int z=0; z< n; z++)
    cout << arr[z] << " ";
freqMap = getFreqMap(arr, n);
set<pair<int,int> > pairSet = getPairSet(arr,n);
set<pair<int,int> > desiredSet = getDesiredSet(pairSet, freqMap);
set<pair<int,int> > :: iterator itr;
cout << "nnDesired order pairsn";
for (itr = desiredSet.begin(); itr != desiredSet.end(); itr++){
    cout <<"(" <<(*itr).first << "," << (*itr).second <<") " ;
}
return 0;
}
擦除

后迭代器无效,但您在下一个循环回合使用它,在这里:

for (itr = pairSet.begin(); itr != pairSet.end(); itr++){
    it1 = freqMap.find(itr->first);
    it2 = freqMap.find(itr->second);
    if ((it1->second < itr->second) || (it2->second < itr->first)){
        itrEnd = itr;
        std::advance(itrEnd , 1);
        pairSet.erase(itr, itrEnd); << itr is invalid now
    }
}

要检测无效的内存访问或内存跳跃,valgrind是一个非常实用的免费工具,如果你可以使用它的话。

相关内容

  • 没有找到相关文章