gpt4 book ai didi

c++ - 我可以知道为什么这段代码没有给出任何输出吗?

转载 作者:行者123 更新时间:2023-12-02 09:53:18 24 4
gpt4 key购买 nike

请帮助我解决该代码在特定行上无限运行的查询。
它没有给出任何输出,因为在代码末尾,我编写了代码以打印 vector 。即使在我为 vector “result”手动分配了任何值之后,它也没有给出任何输出。为什么会这样呢?

#include<bits/stdc++.h>
using namespace std;

bool authorize(int strValue, int value, int M)
{
long int newValue = (strValue - (value * 131) % M);
if (newValue >= 48 && newValue <= 57)
return true;
if (newValue > 65 && newValue <= 90)
return true;
if (newValue >= 97 && newValue <= 122)
return true;
return false;
}

int hashingfunct(string str, int M)
{
long int P, F, sum = 0;
int len = str.length();
for (int i = 0; i < len; i++)
{
P = pow(131, len - i - 1);
F = (int)str[i];
sum += (F * P) % M;
}
sum = sum % M;
return sum;
}

int main()
{
int n = 5;
string str1, str2;
vector<vector<string> > events;
for (int i = 0; i < n; i++) {
cin >> str1 >> str2;
vector<string > temp;
temp.push_back(str1);
temp.push_back(str2);
events.push_back(temp);
}
for (int i = 0; i < n; i++) {
cout << events[i][0] << events[i][1];
}
/*
INPUT FORMAT:
setpassword 1
setpassword 2
setpassword 3
authorize 49
authorize 50
*/
vector<int> result;
int j = 0;
long int m = pow(10, 9);
long int M = m + 7;
long int value, strValue;
for (int i = 0; i < events.size(); i++)
{
strValue = stoi(events[i][1]);
if (events[i][0] == "setPassword") {
value = hashingfunct(events[i][1], M);
}
else if (strValue == value)
result[j++] = 1;
else if (authorize(strValue, value, M))
result[j++] = 1;
else
result[j++] = 0;
}

for (int i = 0; i < result.size(); i++) {
cout << result[i];
}
}

最佳答案

您的程序具有完整的未定义行为。
让我们开始第一个问题。在下面的检查代码中

long int value, strValue;  // not initialised
for (int i = 0; i < events.size(); i++)
{
// ...
// here it should have been "setpassword" (i.e. all are small letters)
if (events[i][0] == "setPassword")
{
// if the check fails the `value` never get initialised!
value = hashingfunct(events[i][1], M);
}
// If the `value` not been initialised, check happens with any garbage value here!
else if (strValue == value)

// ...other code
}
您正在检查字符串是否是 "setPassword"而不是 "setpassword"(即在 events vector 中看到,所有字符串都是小写字母)。
如果这是错误的,则 value将永远不会初始化,这意味着将其初始化为 holds any garbage value,因此执行此检查 else if (strValue == value)可以 cause any behaviour to your program (aka Undefined Behaviour)
其次, vector<int> result;在开始时为空。因此,稍后通过 std::vector::operator[]访问元素
result[j++] = 1;
// ...
result[j++] = 1;
// ...
result[j++] = 0;
触发 access out of bounds (UB) 。在那里,您只需要 result.emplace_back(/*value*/);result.push_back(/*value*/);,而不需要冗余变量 j
简而言之,您需要
#include <iostream>
#include <vector>
#include <string>

// ..other functions
int main()
{
std::vector<std::vector<std::string> > events {
{"setpassword", "1"}, // can be also user input, like in your example
{"setpassword", "2"},
{"setpassword", "3"},
{"authorize", "49" },
{"authorize", "50" }
};

std::vector<int> result;
const long int M = pow(10, 9) + 7;
long int value{ 0 }, strValue{ 0 }; // default initialization
for (const std::vector<std::string> row: events) // better use range-based loop
{
strValue = std::stoi(row[1]);
if (row[0] == "setpassword") {
value = hashingfunct(row[1], M);

if (strValue == value)
result.emplace_back(1);
else if (authorize(strValue, value, M))
result.emplace_back(1);
}
else
result.emplace_back(0);
}
}

作为旁注,
  • do not use using namespacestd;
  • Why should I not #include <bits/stdc++.h>?
  • 关于c++ - 我可以知道为什么这段代码没有给出任何输出吗?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/62495428/

    24 4 0
    Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
    广告合作:1813099741@qq.com 6ren.com