输出与C++

发布于 2025-02-09 10:41:46 字数 1445 浏览 1 评论 0原文

我在代码中找不到任何错误的错误。语句

'r = ispalindrome(str,p [0],p [1] -1);'

当p& q的函数调用时,没有被执行。执行正常。它仅打印仅P&amp的值问。有人可以解释程序的流程有什么问题吗?

#include <iostream>
#include<vector>
using namespace std;

int arr[2];


int* isPalindrome(string &s, int i, int j){

    int sz = s.length();

    if(i==j) return NULL;

    while(i<j){

        if(s[i] == s[j]){
            i++;
            j--;
        }

        else{
            arr[0] = i;
            arr[1] = j;
            return arr;
        }
    }

    return NULL;
}


int main()
{

   string s = "abcdefdba", str;
   int *p, *q, *r;

   // removes any additional character or spaces and make lower-case
   for(int i=0; i<s.length();i++){
    if(s[i] >= 65 && s[i]<=90) str.push_back(s[i]+32);
    if((s[i] >=97 && s[i]<=122) || (s[i]>=48 && s[i]<=57)) str.push_back(s[i]);
   }

    p = isPalindrome(str, 0, str.length()-1);

    cout<<"pointer p: "<<p[0]<<p[1]<<endl;

    if(p==NULL) cout<<"true";

    else{
         q = isPalindrome(str, p[0]+1, p[1]);

         r = isPalindrome(str, p[0], p[1]-1); // not getting executed
        }


    cout<<"pointer q: "<<q[0]<<q[1]<<endl;
    cout<<"pointer r: "<<r[0]<<r[1]<<endl;

    if(q==NULL || r==NULL) cout<<"true";
    else cout<<"false";


    return 0;

}

I can't find any error in my code for an almost palindrome. The statement

'r = isPalindrome(str, p[0], p[1]-1);'

is not getting executed while the function calls for p&q are getting executed fine. It prints values of only p & q. Can someone please explain what is wrong with the flow of the program?

#include <iostream>
#include<vector>
using namespace std;

int arr[2];


int* isPalindrome(string &s, int i, int j){

    int sz = s.length();

    if(i==j) return NULL;

    while(i<j){

        if(s[i] == s[j]){
            i++;
            j--;
        }

        else{
            arr[0] = i;
            arr[1] = j;
            return arr;
        }
    }

    return NULL;
}


int main()
{

   string s = "abcdefdba", str;
   int *p, *q, *r;

   // removes any additional character or spaces and make lower-case
   for(int i=0; i<s.length();i++){
    if(s[i] >= 65 && s[i]<=90) str.push_back(s[i]+32);
    if((s[i] >=97 && s[i]<=122) || (s[i]>=48 && s[i]<=57)) str.push_back(s[i]);
   }

    p = isPalindrome(str, 0, str.length()-1);

    cout<<"pointer p: "<<p[0]<<p[1]<<endl;

    if(p==NULL) cout<<"true";

    else{
         q = isPalindrome(str, p[0]+1, p[1]);

         r = isPalindrome(str, p[0], p[1]-1); // not getting executed
        }


    cout<<"pointer q: "<<q[0]<<q[1]<<endl;
    cout<<"pointer r: "<<r[0]<<r[1]<<endl;

    if(q==NULL || r==NULL) cout<<"true";
    else cout<<"false";


    return 0;

}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

探春 2025-02-16 10:41:47

让我们假设您是对的,并且该函数不执行,让我们找出为什么编译器可能以不需要函数调用的方式对其进行优化:

int* ispalindrome(string&amp; s,int I,int j)返回一个指向全局数组arr或null以及其他内容的指针,除了写入ARR之外,它没有副作用。

然后,让我们分析主要函数:

p = isPalindrome(str, 0, str.length()-1);

cout<<"pointer p: "<<p[0]<<p[1]<<endl;

if(p==NULL) cout<<"true";
else {

您删除了p so p不得为null,编译器可以随时取用其他分支。

     q = isPalindrome(str, p[0]+1, p[1]);

     r = isPalindrome(str, p[0], p[1]-1); // not getting executed
    }


cout<<"pointer q: "<<q[0]<<q[1]<<endl;
cout<<"pointer r: "<<r[0]<<r[1]<<endl;

if(q==NULL || r==NULL) cout<<"true";
else cout<<"false";

同样,您取消了qr,因此它们不能为null。因此,Ispalindrom在两种情况下都必须返回arr的地址,并且必须已书面arr [0]arr [1],它是<代码> p ,<代码> q 和r。第二个ISPALINDROM调用更改p [0]p [1],然后更改ispalindrom呼叫做。我认为他们中的任何一个都不会被跳过。

但是,编译器可以在编译时计算所有内容并消除所有内容。

在输出中,您应该看到最后两个cout调用打印同一件事。也许这会让您认为跳过了其中一个功能呼叫。但是您看到的是,它们都只是打印相同的全局arr


您应该做的是返回std ::可选&lt; std :: pair&lt&lt; int,int&gt;&gt;而不是全局数组。

更好:int不是索引的正确类型,应该是std :: size_tstd ::可选&lt; std :: pair&lt; std :: size_t,std :: size_t&gt;&gt;

为什么不返回子弦的迭代器呢?并使其const,该参数应为const std :: string&amp;std :: optional&lt; std :: pair&lt&std :: string :: string :: const_iterator,std :: string,string string :: const_iterator&gt;&gt;

或让我们真正获得现代并返回字符串视图:std ::可选&lt; std :: string_view&gt;

Lets assume you are right and the function isn't execute and lets find out why the compiler might optimize it in such a way that the function call is not needed:

int* isPalindrome(string &s, int i, int j) returns a pointer to the global array arr or NULL and other than writing to arr it has no side effect.

Lets analyze the main function then:

p = isPalindrome(str, 0, str.length()-1);

cout<<"pointer p: "<<p[0]<<p[1]<<endl;

if(p==NULL) cout<<"true";
else {

You dereferrence p so p must not be NULL, the compiler can always take the else branch.

     q = isPalindrome(str, p[0]+1, p[1]);

     r = isPalindrome(str, p[0], p[1]-1); // not getting executed
    }


cout<<"pointer q: "<<q[0]<<q[1]<<endl;
cout<<"pointer r: "<<r[0]<<r[1]<<endl;

if(q==NULL || r==NULL) cout<<"true";
else cout<<"false";

Again you dereferrence q and r so they can not be NULL. So isPalindrom must have returned the address of arr in both cases and must have written arr[0] and arr[1], which is p, q and r. The second isPalindrom call changes p[0] and p[1] and that changes what the thrid isPalindrom call does. I don't think any of them can be skipped.

But the compiler can potentially compute it all at compile time and eliminate it all.

In the output you should see that the last two cout calls print the same thing. Maybe that makes you think one of the function calls is skipped. But what you see is that they both just print the same global arr.


What you should do is return std::optional<std::pair<int, int>> instead of the global array.

Better: int is not the right type of an index, that should be std::size_t. So std::optional<std::pair<std::size_t, std::size_t>>

Why not return iterators of the substring instead? And make it const, the argument should be a const std::string &: std::optional<std::pair<std::string::const_iterator, std::string::const_iterator>>

Or lets get really modern and return a string view: std::optional<std::string_view>

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文