输出与C++
我在代码中找不到任何错误的错误。语句
'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 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
让我们假设您是对的,并且该函数不执行,让我们找出为什么编译器可能以不需要函数调用的方式对其进行优化:
int* ispalindrome(string&amp; s,int I,int j)
返回一个指向全局数组arr
或null以及其他内容的指针,除了写入ARR之外,它没有副作用。然后,让我们分析主要函数:
您删除了
p
sop
不得为null,编译器可以随时取用其他分支。同样,您取消了
q
和r
,因此它们不能为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_t
。std ::可选&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 arrayarr
or NULL and other than writing to arr it has no side effect.Lets analyze the main function then:
You dereferrence
p
sop
must not be NULL, the compiler can always take the else branch.Again you dereferrence
q
andr
so they can not be NULL. So isPalindrom must have returned the address ofarr
in both cases and must have writtenarr[0]
andarr[1]
, which isp
,q
andr
. The secondisPalindrom
call changesp[0]
andp[1]
and that changes what the thridisPalindrom
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 globalarr
.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 bestd::size_t
. Sostd::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>