C++ 避免C+中的小页面错误+;使用g++;

C++ 避免C+中的小页面错误+;使用g++;,c++,g++,page-fault,gild,C++,G++,Page Fault,Gild,我正在努力解决这个难题:。这是我到目前为止提出的代码: #include <fcntl.h> #include <sys/mman.h> #include <stdlib.h> #include <stdio.h> #include <math.h> #include <sstream> #include <set> using namespace std; struct Container { Cont

我正在努力解决这个难题:。这是我到目前为止提出的代码:

#include <fcntl.h>
#include <sys/mman.h>
#include <stdlib.h>
#include <stdio.h>
#include <math.h>
#include <sstream>
#include <set>
using namespace std;
struct Container
{
    Container(int c, int w) : cost(c), weight(w){}
    int cost;
    int weight;
    bool operator<(const Container& c) const
    {
        return double(cost)/weight < double(c.cost)/c.weight;
    }
};
int main(int argc, char** argv)
{
    int fd = open(argv[1], O_RDONLY);
    char* pContent = (char*)mmap(NULL, 128 * sizeof(int), PROT_READ, MAP_PRIVATE, fd, 0);
    pContent += 8;
    stringstream ss(pContent);
    int unload = 0;
    ss >> unload;
    set<Container> containers;
    int cost = 0, weight = 0;
    while(ss >> cost)
    {
        ss >> weight;
        containers.insert(Container(cost, weight));
    }

    const Container& best = *containers.begin();
    cost = best.cost * ceil(double(unload)/best.weight);
    printf("%d\n", cost);
    return 0;
}
#包括
#包括
#包括
#包括
#包括
#包括
#包括
使用名称空间std;
结构容器
{
集装箱(整箱c,整箱w):成本(c),重量(w){}
国际成本;
整数权重;
bool操作符>卸载;
设置容器;
整数成本=0,权重=0;
而(ss>>成本)
{
ss>>重量;
容器。插入(容器(成本、重量));
}
const Container&best=*containers.begin();
成本=最佳成本*ceil(双倍(卸载)/最佳重量);
printf(“%d\n”,成本);
返回0;
}

我知道这个逻辑可能有一些错误。但我的问题与逻辑无关。当我提交此代码时,它会成功运行,但我得到的分数较低,因为它表示次要页面错误的数量为
409
。当我看到领导板时,有人提交了一个带有小页面错误的C++代码<代码> 69 < /C>。有什么方法可以控制这些小的页面错误吗?可能正在使用一些g++标志?现在,我的make文件非常简单:
g++-o MyExe MyExe.cc

你将受制于Gild用来判断这些的任何算法和运行时环境。我认为编译器标志可能是一种转移注意力的手段;我怀疑在“发布模式”中提交会为您打开-O2或-O3


我怀疑这是一个优化内存使用的问题,可能是内存局部性的问题。例如,stringstream和set可能会非常昂贵;站在你的立场上,我会开始考虑是否还有别的办法,可能需要一个更为定制的算法。

你能不能具体说明一下在这种情况下什么是
小页面错误
?我认为应该是
要求分页
,那么可能是其他人使用了一种完全不同的方法来实现相同的结果。@Als我很确定他指的是软页面错误(未标记内存时)