nullptr 没有打破零终止字符串的循环
nullptr not breaking loop over zero terminated string
我尝试使用 C++ 之旅中给出的以下代码示例,它使用 nullptr 来中断零终止字符串的循环。但是,我的示例程序似乎并没有在循环中停止。
摘自本书:
书中代码的第一个版本:
```
int count_x(char∗ p, char x)
// count the number of occurrences of x in p[]
// p is assumed to point to a zero-terminated array of char (or to nothing)
{
if (p==nullptr) return 0;
int count = 0;
for (; p!=nullptr; ++p)
if (∗p==x)
++count;
return count;
}
```
第二个简化版
```int count_x(char* p, char x)
// count the number of occurrences of x in p[]
// p is assumed to point to a zero-terminated array of char (or to
// nothing)
{
int count = 0;
while (p) {
if (*p==x)
++count;
++p;
}
return count;
}```
书中代码如下声明:
while 语句一直执行到它的条件变为假为止。
对指针的测试(例如,while (p))等同于将指针与空指针进行比较(例如,
while (p!=nullptr)).
我的程序使用相同的结构:
char name[] = "ABCD";
char *p = name;
int count = 0;
int loopc = 0;
while (p)
{
loopc++;
if(*p == '[=12=]')
cout << "zero found\n";
else
cout << *p << "\n";
//emergency stop
if (loopc == 12)
break;
p++;
}
预计:
应该在打印名称后停止。
实际的:
A
B
C
D
zero found
zero found
zero found
zero found
zero found
zero found
zero found
zero found
感谢所有有用的评论。
似乎作者在早期版本(第 1 版)中给出了错误的示例,后来在 2018 年发布的第二版中更正了。
新版本的更正版本:
int count_x(char∗ p, char x)
// count the number of occurrences of x in p[]
// p is assumed to point to a zero-terminated array of char (or to nothing)
{
if (p==nullptr) return 0;
int count = 0;
for (; *p!=0; ++p)
if (∗p==x)
++count;
return count;
}
第一个版本应该return0
当你通过它nullptr
。但是,在 for 循环中,您只传递了一次。
无论如何只有一个 char*
(考虑使用 std::string
顺便说一句)...
这是我的快速修复,尝试理解它:
int count_x(char* c_ptr, char c) {
if (c_ptr == nullptr) return 0;
int count = 0;
/* Note that I check if *c_ptr is '[=10=]' that's the **value**
* pointed to by c_ptr
*/
for (; *c_ptr != '[=10=]'; ++c_ptr) // '[=10=]' is a nul character
if (*c_ptr == c) ++count;
return count;
}
int foo(const std::string& word, char letter) noexcept {
int count = 0;
for (const auto& c: word) { // for all const char ref in word
if (c == letter) ++count;
}
return count;
}
int main() {
int a = count_x("acbccc", 'c');
int b = foo("acbccc", 'c');
std::cout << a << '\n' << b;
}
如有任何问题,欢迎随时提问。
干杯。
我尝试使用 C++ 之旅中给出的以下代码示例,它使用 nullptr 来中断零终止字符串的循环。但是,我的示例程序似乎并没有在循环中停止。
摘自本书:
书中代码的第一个版本:
```
int count_x(char∗ p, char x)
// count the number of occurrences of x in p[]
// p is assumed to point to a zero-terminated array of char (or to nothing)
{
if (p==nullptr) return 0;
int count = 0;
for (; p!=nullptr; ++p)
if (∗p==x)
++count;
return count;
}
```
第二个简化版
```int count_x(char* p, char x)
// count the number of occurrences of x in p[]
// p is assumed to point to a zero-terminated array of char (or to
// nothing)
{
int count = 0;
while (p) {
if (*p==x)
++count;
++p;
}
return count;
}```
书中代码如下声明: while 语句一直执行到它的条件变为假为止。 对指针的测试(例如,while (p))等同于将指针与空指针进行比较(例如, while (p!=nullptr)).
我的程序使用相同的结构:
char name[] = "ABCD";
char *p = name;
int count = 0;
int loopc = 0;
while (p)
{
loopc++;
if(*p == '[=12=]')
cout << "zero found\n";
else
cout << *p << "\n";
//emergency stop
if (loopc == 12)
break;
p++;
}
预计:
应该在打印名称后停止。
实际的:
A
B
C
D
zero found
zero found
zero found
zero found
zero found
zero found
zero found
zero found
感谢所有有用的评论。
似乎作者在早期版本(第 1 版)中给出了错误的示例,后来在 2018 年发布的第二版中更正了。
新版本的更正版本:
int count_x(char∗ p, char x)
// count the number of occurrences of x in p[]
// p is assumed to point to a zero-terminated array of char (or to nothing)
{
if (p==nullptr) return 0;
int count = 0;
for (; *p!=0; ++p)
if (∗p==x)
++count;
return count;
}
第一个版本应该return0
当你通过它nullptr
。但是,在 for 循环中,您只传递了一次。
无论如何只有一个 char*
(考虑使用 std::string
顺便说一句)...
这是我的快速修复,尝试理解它:
int count_x(char* c_ptr, char c) {
if (c_ptr == nullptr) return 0;
int count = 0;
/* Note that I check if *c_ptr is '[=10=]' that's the **value**
* pointed to by c_ptr
*/
for (; *c_ptr != '[=10=]'; ++c_ptr) // '[=10=]' is a nul character
if (*c_ptr == c) ++count;
return count;
}
int foo(const std::string& word, char letter) noexcept {
int count = 0;
for (const auto& c: word) { // for all const char ref in word
if (c == letter) ++count;
}
return count;
}
int main() {
int a = count_x("acbccc", 'c');
int b = foo("acbccc", 'c');
std::cout << a << '\n' << b;
}
如有任何问题,欢迎随时提问。 干杯。