有人能告诉我为什么它在 Visual Studio 中编译和运行良好,但在编译 GNU C++ 编译器时因分段错误而失败
不能为我的生活弄清楚这一点。我检查了前缀/后缀运算符,并确保将迭代器正确放置在条件语句中......我错过了什么?通常我在处理分配/解除分配动态数组时遇到这个问题,我在这里没有这样做。
#include <cmath>
#include <cstdio>
#include <vector>
#include <iostream>
#include <algorithm>
#include <random>
#include <unordered_set>
namespace std{
template <>
struct hash<vector<int>> : public unary_function<vector<int>, size_t>
{
size_t operator()(const vector<int>& v) const
{
return v.size();
}
};
}
using namespace std;
struct testCase{
int n;
int m;
vector <int> wordVector;
};
bool lexOrderConfirm(std::vector<int>::const_iterator, std::vector<int>::const_iterator, std::vector<int>::const_iterator, int );
int main() {
int t;
cin>>t;
vector <testCase*> testCaseVector(t);
for (int i = 0; i < t; ++i){
testCaseVector[i] = new testCase;
cin>>testCaseVector[i]->n;
cin>>testCaseVector[i]->m;
}
for (int i = 0; i < t; ++i){
std::unordered_set<std::vector<int>, std::hash<std::vector<int> > >permutations;
int permu = pow((double)testCaseVector[i]->n, testCaseVector[i]->m);
int count = 0;
for (int z= 0; z < (permu*10); ++z){
std::random_device rd;
std::default_random_engine randomeng( rd() );
int v;
for (int b = 0; b < testCaseVector[i]->m; ++b){
v= randomeng() % testCaseVector[i]->n + 1; /*random number between 1 and n*/
testCaseVector[i]->wordVector.push_back(v);
}
std::sort(testCaseVector[i]->wordVector.begin(), testCaseVector[i]->wordVector.end());
permutations.insert(testCaseVector[i]->wordVector);
testCaseVector[i]->wordVector.clear();
}
do {
vector <int> test = *permutations.begin();
do {
std::vector<int>::const_iterator start = test.begin();
std::vector<int>::const_iterator end;
end = test.end();
std::vector<int>::const_iterator lastElem = start+(testCaseVector[i]->m-1);
if (lexOrderConfirm(start, end, lastElem, testCaseVector[i]->n)){
++count;
}
}while(std::next_permutation(test.begin(),test.end()));
permutations.erase(permutations.begin());
test.clear();
}while(!permutations.empty());
count = count%100000007;
cout<<count<<endl;
}
return 0;
}
bool lexOrderConfirm(std::vector<int>::const_iterator start, std::vector<int>::const_iterator end, std::vector<int>::const_iterator lastElem, int N){
bool confirmed = true;
for (std::vector<int>::const_iterator t = start; t != end; ++t){
if ((2* (*t) > N)){
} else if ((2*(*t) <= N) && (*(t+1) >= 2 * (*t)) && (t != (lastElem))){
}else{
confirmed = false;
break;
}
}
return confirmed;
}