簡體   English   中英

為什么在 stl 中使用 bitset 時出現 SIGABRT 錯誤?

[英]Why do I get a SIGABRT error while using bitset in stl?

以下代碼在我的 g++ 編譯器上運行良好。 但是在在線評委上提交時,會出現 SIGABRT 錯誤。 我讀到如果 stl 元素嘗試訪問過多的 memory 會產生此錯誤。 我看不到 memory 的任何此類用途。 是計數嗎? 我已經實現了自己的計數,但它仍然給出了同樣的錯誤。

#pragma GCC optimize ("-O2")
#include <bits/stdc++.h>
using namespace std;
#define fastio ios_base::sync_with_stdio(0);cin.tie(0);cout.tie(0)

#define M 100
void range_change(bitset<M>& x, int lower, int upper, bool change){
    if (change){
     for (unsigned i = lower; i <= upper; ++i)
              x.set(i);
     }
    else{
    for (unsigned i = lower; i <= upper; ++i)
              x.reset(i);
     }
    }
int count_Set(bitset<M> & x){
    int count = 0;
    for(int i = 0; i< 100; i++){
        if(1 & x[i]) count++;
    }
    return count;
}

bitset<100> houses;

int main(){
    fastio;
    int t;
    cin>> t;
    int m, x, y;
    int reach{0};
    int lower{0}, upper{0};
    // int t = 1;
    while(t--){

        houses.set();
        // cout<< houses;
        cin >> m >> x >> y;
        reach = x*y;
        vector<int> cat(m, 0);
        for(auto& u: cat){
            cin>> u;

            lower = u - reach -1;
            if(lower < 0) lower = 0;
            upper = u + reach-1;
            if(upper > 100) upper = 99;
            range_change(houses, lower, upper, false);
        }
        // cout<< houses << "\n";
         cout<< houses.count() << "\n";
       // cout<< count_Set(houses) << "\n";

 }
    return 0;
}

只是瀏覽源代碼,這就是我所看到的:

#define M 100
...
bitset<100> houses;

為了安全起見,您似乎想使用M而不是100

那么這里:

if(upper > 100) upper = 99;

這將在upper = 100處中斷 建議:

if(upper >= M) upper = M - 1;

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM