回答两个编译器之间的差异
Answer difference between two compiler
我编译了以下代码,但我遇到了一个严重的问题。当我在Visual Studio 2015中编译此代码时,它运行良好。但是,当我在 Dev C++ 中编译此代码时,我认为它不会打印"是"作为答案。
例如,当我键入诸如
- 哈哈
- 我看到的是汽车还是猫?
- abcdefghiihgfedcba
这些输入必须返回 yes,但在 dev c++ 中返回 no。
为什么会出现此问题?
#include <iostream>
#include <string>
using namespace std;
bool is_palindrome(char input[], int numOfSlots);
int main(void) {
char text[256], fixed[256];
cin.getline(text, sizeof(text), 'n');
for (int i = 0; i < sizeof(text); i++) {
text[i] = toupper(text[i]);
}
int j = 0;
for (int i = 0; i < sizeof(text); i++) {
if ((text[i] >= '0' && text[i] <= '9') || (text[i] >= 'A' && text[i] <= 'Z')) {
fixed[j] = text[i];
j++;
}
}
fixed[j] = ' ';
string s_fixed = fixed;
if (is_palindrome(fixed, s_fixed.length()) == true) {
cout << "Yes";
}
else {
cout << "No";
}
return 0;
}
bool is_palindrome(char input[], int numOfSlots) {
int i = 0;
while (i < numOfSlots / 2)
{
if (input[i] != input[(numOfSlots - 1) - i])
return false;
i++;
}
return true;
}
程序表现出未定义的行为,因为您使用的是未初始化的数据。
你有:
char text[256], fixed[256];
哪些是未初始化的数组。然后你去访问它们:
for (int i = 0; i < sizeof(text); i++) {
text[i] = toupper(text[i]); // Accessing uninitialized array
}
您可以使用几种方法修复它:
初始化数组。
char text[256] = {0}, fixed[256] = {0};
仅访问在调用中填充的元素
getline
。size_t size = strlen(text); for (int i = 0; i < size; i++) {
但是,更好的解决方法是始终使用第二种方法。这样,您就不会处理不必要的数据。
使用 std::string
代替strlen()
是相当奇怪的,当你可以更好地使用它时:
bool is_palindrome( const std::string &input );
int main(void) {
std::string text;
getline(cin,text);
for (size_t i = 0; i < text.length(); i++) {
text[i] = toupper(text[i]);
}
std::string fixed;
for (size_t i = 0; i < text.length(); i++) {
if ((text[i] >= '0' && text[i] <= '9') || (text[i] >= 'A' && text[i] <= 'Z')) {
fixed += text[i];
}
}
if (is_palindrome(fixed)) {
cout << "Yes";
}
else {
cout << "No";
}
return 0;
}
bool is_palindrome(const std::string &input) {
size_t numOfSlots = input.length();
int i = 0;
while (i < numOfSlots / 2)
{
if (input[i] != input[(numOfSlots - 1) - i])
return false;
i++;
}
return true;
}
当然,您的程序可以简化,但我试图使其接近原始程序,以说明为什么最好使用std::string
而不是旧式char[]
C++
这里使用标准库中的std::string
和其他算法的简化版本:
#include <iostream>
#include <string>
#include <algorithm>
bool is_palindrome( std::string str )
{
if( str.empty() ) return false;
std::transform( str.begin(), str.end(), str.begin(), []( char c ) { return std::toupper( c ); } );
str.erase( std::remove_if( str.begin(), str.end(), []( char c ) { return !std::isalnum( c ); } ), str.end() );
auto len = str.length() / 2 + 1;
return std::string( str.begin(), std::next( str.begin(), len ) ) ==
std::string( str.rbegin(), std::next( str.rbegin(), len ) );
}
int main()
{
std::string text;
std::getline( std::cin, text );
std::cout << ( is_palindrome( text ) ? "yes" : "no" ) << std::endl;
return 0;
}
相关文章:
- 编译器如何在使用SFINAE的函数和标准函数之间确定两者是否可行
- 编译器如何在前缀和 postix 运算符之间进行区分?
- 填充上编译器生成的复制构造函数之间的不一致
- 为什么库API+编译器ABI足以确保具有不同版本gcc的对象之间的兼容性
- 检测到 Conan 配置文件设置与 CMake 之间的编译器版本不匹配
- 不同c++编译器之间的自动类型推导不匹配
- 如何让CMake在多个编译器之间进行选择?
- 如果检查和内联条件之间是否存在编译器差异
- 这些编译器之间是否有任何区别
- Android NDK 中的预构建工具链和自定义工具链编译器之间的差异
- 编译器如何在向量vs Initializer_list之间做出决定
- C - 编译器如何以参数为参数的过载函数在超载函数之间决定
- 具有C 接口的C 动态库如何在不同的编译器版本之间不会破坏ABI
- 名称名称空间和类模板之间的冲突:不同的编译器行为
- CL和G 编译器之间的差异
- 当编译器看到 std::vector<Typo> 和 std::vector<struct 拼写错误时发出的诊断之间的差异背后的理由是什么>
- 编译器之间在丢弃的 if constexpr(false) 语句中实例化模板的行为不一致
- C 编译器如何在延期和异步执行std :: async之间进行选择
- 可能的编译器错误:在两台机器之间使用英特尔编译器的boost bessel函数会产生奇怪的结果
- C 编译器可以在用户定义和编译器生成的复制构建器之间进行自由选择