我是编码的新手,所以自然而然,我经常遇到编码错误。我一直遇到分段错误错误,想知道是否有人可以解释它是什么,下面的代码有什么问题,以及将来如何避免分段错误。预先感谢。
#include <iostream>
#include <algorithm>
#include <vector>
int main() {
std::vector<std::vector<int> > pointers;
std::vector<int> squawkers;
int n, m, s, t, counter = 0;
std::cin >> n >> m >> s >> t;
squawkers[s] = 1;
for (int i = 0; i < m; i++) {
int a, b;
std::cin >> a >> b;
pointers[a].push_back(b);
pointers[b].push_back(a);
}
while (counter < t) {
t++;
std::vector<int> temp (n);
std::fill(temp.begin(), temp.begin()+n-1, 0);
for (int i = 0; i < n; i++) {
if (pointers[i].size() != 0) {
for (int j = 0; j < pointers.size(); j++)
temp[j] += squawkers[i];
}
}
squawkers = temp;
}
int sum;
for (int i = 0; i < squawkers.size(); i++)
sum += squawkers[i];
std::cout << sum << std::endl;
}
答案 0 :(得分:1)
从上面的代码中提取的这段短代码是错误的。
const nonce = this.generateUniqueString();
const state = this.generateUniqueString();
const date = new Date();
const query = `${otherQueryOptions}&state=${state}&nonce=${nonce}`;
const authSessionData = {
authority: auth_server_url,
client_id: client_id,
created: date.getTime(),
id: state,
nonce: nonce,
redirect_uri: `your/return.url`
}
// You must prefix the key with 'oidc'
localStorage.setItem(`oidc.${authSessionData.id}`, JSON.stringify(authSessionData));
const registrationPath = `myServerRegistrationPath?${query}`
navigateToPage(registrationPath);
int main() {
std::vector<int> squawkers;
std::cin >> s;
squawkers[s] = 1;
}
的值无关紧要,s
的大小为零,因此在任何上使用squawkers
都是错误的。
对于初学者来说,其余代码相当先进。因此,这个简单的错误存在对我来说很奇怪。