【问题标题】:MayI know why this code is not giving any output?我可以知道为什么这段代码没有给出任何输出吗?
【发布时间】:2025-12-19 17:55:07
【问题描述】:

请帮我解决这段代码在特定行无限运行的查询。

它没有给出任何输出,因为在代码的末尾我编写了代码来打印向量。即使我手动为向量“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];
   }
}

【问题讨论】:

  • 你能比“在特定行”更具体吗?你能在这里和那里添加一些日志,看看它卡在哪里

标签: c++ algorithm c++14 stdvector stdstring


【解决方案1】:

您的程序具有完整的未定义行为。

让我们从第一个问题开始。在下面的校验码中

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 向量中看到,所有字符串都是小写字母)。

如果出现问题,value 将永远不会被初始化,这意味着它holds any garbage value 并且因此执行此检查else if (strValue == value) 可以cause any behaviour to your program (aka Undefined Behaviour)

其次,vector&lt;int&gt; 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);
   }
}

顺便说一句,

【讨论】:

    【解决方案2】:

    Corrected code

    #include<bits/stdc++.h>
    using namespace std;
    bool authorize(long int strValue,long int value,int M){
        long int value1=value*131;
        long int newValue=(strValue-(value1%M))%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);
        }
    
    /*
    setPassword cAr1
    authorize 223691457
    authorize 303580761
    setPassword d
    authorize 100
    */
        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++){
            
            if(events[i][0]=="setPassword"){
                value=hashingfunct(events[i][1],M);
                continue;
            }
            strValue=stoi(events[i][1]);
            if(strValue==value)
            result.push_back(1);
            else if(authorize(strValue,value,M))
            result.push_back(1);
            else
            result.push_back(0);
            
        }
        
    
        for(int i=0;i<result.size();i++){
            cout<<result[i];
        }
    
    
    }
    

    【讨论】:

      最近更新 更多