[英]Checking reversed text on C++ (doesn't work with a -1 index?)
我正在制作一個簡單的回文檢查器,無法理解為什么迭代器使用索引 0,但不能使用索引 -1,您可以看到兩個代碼打印相同的文本,但不是相同的 boolean。
誰能解釋一下這背后的邏輯是什么?
兩個代碼唯一不同的部分是:
for(int i=text.size();i>= -1 ;i--)
for(int i=text.size() -1 ;i>= 0 ;i--)
在職的:
#include <iostream>
// Define is_palindrome() here:
std::string is_palindrome(std::string text){
std::string reverse= "";
for(int i=text.size()-1;i>=0;i--){
reverse += text[i];
}
if(reverse == text){
return text + " IS palindrome (reverse text: " + reverse +")";
} else {
return text + " NOT palindrome (reverse text: " + reverse + ")";
}
}
int main() {
std::cout << is_palindrome("madam") << "\n";
std::cout << is_palindrome("ada") << "\n";
std::cout << is_palindrome("lovelace") << "\n";
}
output
madam IS palindrome (reverse text: madam)
ada IS palindrome (reverse text: ada)
lovelace NOT palindrome (reverse text: ecalevol)
不工作
#include <iostream>
// Define is_palindrome() here:
std::string is_palindrome(std::string text){
std::string reverse= "";
for(int i=text.size();i>=-1;i--){
reverse += text[i];
}
if(reverse == text){
return text + " IS palindrome (reverse text: " + reverse +")";
} else {
return text + " NOT palindrome (reverse text: " + reverse + ")";
}
}
int main() {
std::cout << is_palindrome("madam") << "\n";
std::cout << is_palindrome("ada") << "\n";
std::cout << is_palindrome("lovelace") << "\n";
}
output
madam NOT palindrome (reverse text: madam)
ada NOT palindrome (reverse text: ada)
lovelace NOT palindrome (reverse text: ecalevol)
C++ 使用基於 0 的索引,即N
大小容器的有效索引是0,1,2...N-1
。 如果您嘗試使用超出范圍的索引,您的代碼會調用未定義的行為。
看這段代碼的output:
#include <iostream>
#include <string>
void foo(const std::string& text){
for(int i=text.size();i>=-1;i--){
std::cout << i << "\n";
}
}
int main() {
foo("madam");
}
這是
5
4
3
2
1
0
-1
-1
和5
都不是字符串的元素,因為它只有5
字符。 (對於5
,我允許自己稍微簡化一下問題,因為字符串在訪問最后一個字符 null 終止符時有一些奇怪的地方。因為-1
肯定是越界並且使用std::string
你通常不需要處理null 終止符明確,這對於這個答案是可以的)。
現在看這段代碼的output:
#include <iostream>
#include <string>
void foo(const std::string& text){
for(int i=text.size()-1;i>=0;i--){
std::cout << i << "\n";
}
}
int main() {
foo("madam");
}
這是
4
3
2
1
0
它迭代輸入字符串的所有字符。 這就是為什么這是正確的,而另一個是錯誤的。
當您的代碼具有未定義的行為時,任何事情都可能發生。 結果可能看起來正確或部分正確。 錯誤的代碼不一定會產生明顯錯誤的 output。
請注意,當字符串的大小為0
時, for(int i=text.size()-1;i>=0;i--)
會出現問題,因為size()
返回一個 unsigned ,它會環繞導致 a大正數。 您可以通過使用迭代器來避免這種情況。 有一些反向迭代器使反向循環看起來與正向循環相同:
#include <iostream>
#include <string>
int main() {
std::string text("123");
for (auto i = text.rbegin(); i != text.rend(); ++i) std::cout << *i;
}
Output
321
除了 463035818_is_not_a_number 的答案之外,您還可以使用迭代器從另一個字符串構造一個字符串:
#include <iostream>
#include <string>
int main() {
std::string text("123");
std::string rev(crbegin(text), crend(text));
std::cout << rev << '\n';
}
#include <iostream>
#include <string>
#include <algorithm>
int main() {
std::string text("123");
reverse(begin(text), end(text));
std::cout << text << '\n';
}
但是對於回文,您不必比較兩個完整的字符串。 你只需要看到字符串的前半部分等於字符串后半部分的反轉。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.