0
有人能告訴我爲什麼這個編譯並運行在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;
}
你**不應該被**注射任何自定義類型或功能爲'std'命名空間,否則,你就是自找麻煩自己。此外,您發佈了大量樣板和格式錯誤的代碼,但未提供調試器的跟蹤信息或至少一條錯誤消息。在這種情況下,沒有人可以幫助你。重新設計你的問題。 –