小编Jam*_*mes的帖子

错误:使用自定义比较功能排序时"无效比较器"

我试图对一些整数进行排序,并使奇数整数后跟偶数.我正在使用Visual Studio 2015.

这是我的代码:

int w[]={1,2,3,4,5,6};
sort(w,w+6,[](const int&i,const int&j)->bool {
return (i&1)==(j&1)//When both are odd or even, the order is OK
||i&1;//if one is odd and one is even,check if the first one is odd
});
Run Code Online (Sandbox Code Playgroud)

执行时,会遇到错误"表达式:无效的比较器".我不知道为什么会导致这个错误.如何修改?

c++ c++11

14
推荐指数
1
解决办法
1万
查看次数

插入到std :: unordered_map在MSVC++的STL中调​​用哈希函数两次,设计不好还是特殊原因?

对于此代码:

#include<unordered_map>
#include<iostream>
using namespace std;
struct myhash {
    unsigned operator()(const unsigned&v)const {
        cout<<"Hash function is called:"<<v<<endl;
        return v;
    }
};
unordered_map<unsigned,unsigned,myhash>mp;
int main() {
    for (unsigned i=0;i<3;++i) {
        cout<<"Visiting hash table:"<<i<<endl;
        ++mp[i];
    }
}
Run Code Online (Sandbox Code Playgroud)

使用g ++时,输出并不奇怪:

Visiting hash table:0
Hash function is called:0
Visiting hash table:1
Hash function is called:1
Visiting hash table:2
Hash function is called:2
Run Code Online (Sandbox Code Playgroud)

但MSVC++(2015)的输出震惊了我:

Visiting hash table:0
Hash function is called:0
Hash function is called:0
Visiting hash table:1
Hash function is called:1
Hash function is …
Run Code Online (Sandbox Code Playgroud)

c++ stl visual-c++ c++11

9
推荐指数
1
解决办法
435
查看次数

我可以通过清除其底层容器来清除priority_queue吗?

以下代码继承std :: priority_queue并提供clear()调用内部std::vector的代码clear()

#include<iostream>
#include<queue>
using namespace std;
template<class type>
struct mypq :public priority_queue<type> {
    void clear(){
        this->c.clear();
    }
};
mypq<int>pq;
int main() {
    for(int i=0;i<10;++i)
        pq.push(i);
    pq.clear();
    for(int j=-5;j<0;++j)
        pq.push(j);
    while (!pq.empty()){
        cerr<<pq.top()<<endl;
        pq.pop();
    }
}
Run Code Online (Sandbox Code Playgroud)

当我使用g ++,MSVC++和clang测试它时,它会产生预期的输出:

-1
-2
-3
-4
-5
Run Code Online (Sandbox Code Playgroud)

但我没有看到任何保证,即清除内部向量将pop()与priority_queue不为空时调用相同.虽然我知道其他方法来清除它,比如交换或使用空的priority_queue分配它,但我认为如果这个代码能够很好地工作,那么它会更有效,因为向量中分配的内存是可重用的.所以我想知道这段代码是可移植的还是不会一直有效?

c++ c++11

8
推荐指数
1
解决办法
569
查看次数

是否可以使用命名空间重用变量名称?

例如,最初我有一个示例程序:

#include<iostream>
#include<algorithm>
using namespace std;
int main() {
    int a[3];
    sort(begin(a),end(a));
    cin;
}
Run Code Online (Sandbox Code Playgroud)

现在我想修改std::cin(提供更多函数,如输入失败时调用函数).所以我介绍了一个标题mystd.h:

#include<iostream>
#include<algorithm>
//begin of mystd.h
namespace mystd {
    struct cin_wrapper {

    }cin;
}
//end of mystd.h
using namespace std;
int main() {
    int a[3];
    sort(begin(a),end(a));
    mystd::cin;
}
Run Code Online (Sandbox Code Playgroud)

但变化似乎不太方便.(用户必须提及的所有组件using std::sort;using mystd::cin;或更换所有cinmystd::cin.using namespace std;using mystd::cin;引起cin歧义)

事实上,我将编写一个经过修改的标准库,并使其像原始库一样方便.我希望用户可以编写的理想代码是:

(PS:这意味着mystd可以只用作std,而不是表示我想鼓励用户using namespace到处使用)

#include<iostream>
#include<algorithm>
#include "mystd.h"
using namespace mystd;
int main() …
Run Code Online (Sandbox Code Playgroud)

c++ c++11

7
推荐指数
1
解决办法
316
查看次数

是否有一种可控制的方式来减慢我在线评委的C++程序?

我正在寻找一种可控制的方式(很容易设置延迟时间)来减缓我在线评委的C++解决方案.(主要用于UVa g++ 4.8.2 -lm -lcrypt -O2 -std=c++11 -pipe)

我试过以下代码:

{auto start=std::chrono::high_resolution_clock::now();
while (std::chrono::duration<double,std::milli>
    (std::chrono::high_resolution_clock::now()-start).count()<2000);
}
Run Code Online (Sandbox Code Playgroud)

但解决方案减慢了大约1.6秒,而不是预期的2秒,我不知道为什么.

我也试过std::this_thread::sleep_forusleep()FOM <unistd.h>,但这些几乎没有在线的法官影响运行.

因为std::this_thread::sleep_for,我试过:

std::this_thread::sleep_for(std::chrono::milliseconds(2600));
Run Code Online (Sandbox Code Playgroud)

我想这样做的原因是我的老师经常在这些在线评委上分配问题,我们的家庭作业评分员会将我们的解决方案提交给在线评委,以检查他们是否可以获得AC(已接受).因此,我的解决方案将在排名系统中计算两次,我认为这对后来的用户来说是不公平的,特别是当我的解决方案排在排名榜首时.因此,我倾向于放慢我的解决方案,以减少对其他用户的影响,然后再将其提交到作业评分系统.

c++ c++11

2
推荐指数
1
解决办法
884
查看次数

标签 统计

c++ ×5

c++11 ×5

stl ×1

visual-c++ ×1