【问题标题】:Probably segmentation fault n>150 000 wtih vector可能是分段错误 n>150 000 与向量
【发布时间】:2015-11-14 12:28:15
【问题描述】:
int main()
{
    ios_base::sync_with_stdio(0);

    cin>>n;
    vector<vector<bool> > tab (n+1, vector<bool>(n+1));
    vector<unsigned short int >wifi (n+1);    
    int  z=0;
    x=1;
    do{    
        cin>>a>>b;
        tab[a][b]=1;
        tab[b][a]=1;
        x++;    
    }while(x!=n);
}

您好,我还有一个问题,即使他们将其更改为矢量,该程序已停止运行结果大于 150 000。我简单介绍一下代码。

【问题讨论】:

  • 这不是minimal reproducible example,请编辑您的问题。例如,nab 是什么?我们必须输入哪些输入值才能重现问题?这个程序应该做什么?
  • 我写了这是程序的一部分,输出是n

标签: c++ vector segmentation-fault


【解决方案1】:

我的预感是您的内存不足没有足够检查输入值!

更改[i].at(i) 并确保索引有效:

Live On Coliru

#include <iostream>
#include <vector>

int main() {
    std::ios_base::sync_with_stdio(0);

    size_t n;
    if (std::cin >> n)
    {
        std::cout << "n = " << n << "\n";

        std::vector<std::vector<bool> > tab(n + 1, std::vector<bool>(n + 1));
        std::vector<unsigned short int> wifi(n + 1);

        std::cout << "allocated, reading\n";

        size_t a, b;
        for (size_t m = n; m && std::cin >> a >> b; --m)
        {
            tab.at(a%n).at(b%n) = 1;
            tab.at(b%n).at(a%n) = 1;
            if (m%1000==1) std::cout << "." << std::flush;
        }
    }
}

即使是 250,000 行,我的电脑也没有问题。

现在,考虑改用dynamic_bitsetboost::icl::interval_set。考虑对分布式 AdjacencyMatrix 图使用并行 BGL。

如果矩阵是稀疏的,请考虑 AdjacencyListsEdgeList

【讨论】:

  • 奇怪,因为对我来说是一样的。
  • @xodos 可能导致您的系统内存不足。能分享一下输入文件吗?是在线编程挑战赛吗?
猜你喜欢
  • 2020-04-19
  • 1970-01-01
  • 1970-01-01
  • 2019-11-10
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多