std::string.find_first_not_of,意外的返回值

发布于 2024-10-21 18:50:14 字数 221 浏览 3 评论 0原文

#include <stdio.h>
#include <string>

int main(void)
{
    printf("%u\n", std::string("\n").find_first_not_of(" \t\n\v\f\r", 0, 1));
}

下面的程序打印 0,而不是我预期的 std::string::npos 。为什么?

#include <stdio.h>
#include <string>

int main(void)
{
    printf("%u\n", std::string("\n").find_first_not_of(" \t\n\v\f\r", 0, 1));
}

The following program prints 0, not std::string::npos as I expected. Why?

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

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

发布评论

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

评论(6

混吃等死 2024-10-28 18:50:14

您的调用匹配:

size_t find_first_not_of ( const char* s, size_t pos, size_t n ) const;

ns 中的字符数,并且您传递的是 1。因此,您正在搜索第一个不是空格的字符。其余的 " \t\n\v\f\r" 字符串将被忽略。

可能您只是想要:

find_first_not_of(" \t\n\v\f\r")

Your call matches:

size_t find_first_not_of ( const char* s, size_t pos, size_t n ) const;

n is the number of chars in s, and you're passing 1. So, you're searching for the first char that is not space. The rest of your " \t\n\v\f\r" string is ignored.

Likely you simply want:

find_first_not_of(" \t\n\v\f\r")
魔法少女 2024-10-28 18:50:14

第三个参数并不意味着你认为它的作用。

The third parameter doesn't mean what you think it does.

痕至 2024-10-28 18:50:14

根据thisstring::find_first_not_of搜索对象中不属于 str、s 或 c 的第一个字符,并返回其位置。由于“\t”是此类字符,因此返回值为0。

According to this, string::find_first_not_of searches for the first character in the object which is not part of either str, s or c, and returns its position. Since "\t" is such character, return value is 0.

乙白 2024-10-28 18:50:14

根据您想要打印的内容,我可以说第三个参数应该是您传递的字符串的长度。所以这里是更正后的版本:

#include <stdio.h>
#include <string>

int main(void)
{
    std::string s=" \t\n\v\f\r";
    printf("%u\n", std::string("\n").find_first_not_of(s.c_str(), 0, s.length()));

   //since now I'm using std::string, you can simply write:
   printf("%u\n", std::string("\n").find_first_not_of(s));
}

ideone 的演示: http://ideone.com/y5qCX

Based on what you want to print, I can say the third parameter should be the length of string you passed. So here is the corrected version:

#include <stdio.h>
#include <string>

int main(void)
{
    std::string s=" \t\n\v\f\r";
    printf("%u\n", std::string("\n").find_first_not_of(s.c_str(), 0, s.length()));

   //since now I'm using std::string, you can simply write:
   printf("%u\n", std::string("\n").find_first_not_of(s));
}

Demo at ideone : http://ideone.com/y5qCX

难理解 2024-10-28 18:50:14

看看它:

#include <stdio.h>
#include <string>

int main(void)
{
        std::string s("\n");
        if( s.find_first_not_of(" \t\n\v\f\r", 0, 1) != std::string::npos )
                    printf("%u\n", s.find_first_not_of(" \t\n\v\f\r", 0, 1));
        else
                puts("npos");
        return 0;
}

See it:

#include <stdio.h>
#include <string>

int main(void)
{
        std::string s("\n");
        if( s.find_first_not_of(" \t\n\v\f\r", 0, 1) != std::string::npos )
                    printf("%u\n", s.find_first_not_of(" \t\n\v\f\r", 0, 1));
        else
                puts("npos");
        return 0;
}
我还不会笑 2024-10-28 18:50:14

find_first_not_of 方法将最后一个参数解释为第一个参数中要考虑的字符数,而不是字符串中的字符数。

size_type std::string::find_first_not_of(
    const char* str, size_type index, size_type num) const;

参数 num 是在 str 中考虑的数字,而不是在 this 中!因此,在您的情况下,它只考虑 " \t\n\v\f\r" 的第一个字符。您的代码相当于:

#include <cstdio>
#include <string>

int main(void)
{
    printf("%u\n", std::string("\n").find_first_not_of(" ", 0));
}

如果您只想匹配 std::string 的子字符串,我认为您必须在显式子字符串上调用 find_first_not_of ,即:

#include <cstdio>
#include <string>

int main(void)
{
    printf("%u\n", std::string("\n").substr(0, 1).find_first_not_of(" \t\n\v\f\r"));
}

BTW ,这里find_first_not_of 方法:

find_first_not_of() 函数可以:

  • 返回当前字符串中与 str 中任何字符都不匹配的第一个字符的索引,从索引处开始搜索,如果没有找到,则为 string::npos,
  • 从索引处开始搜索当前字符串,查找与 str 中前 num 个字符不匹配的任何字符,返回当前字符串中找到的满足此条件的第一个字符的索引,否则返回 string::npos,
  • 或者返回当前字符串中第一次出现与 ch 不匹配的字符的索引,如果没有找到,则从索引 string::npos 开始搜索。

The method find_first_not_of interpret the last argument as the number of char to consider in its first argument, not in the string.

size_type std::string::find_first_not_of(
    const char* str, size_type index, size_type num) const;

The argument num is the number to consider in str, not in this ! So in your case, it only consider the first caracter of " \t\n\v\f\r". You code is equivalent to:

#include <cstdio>
#include <string>

int main(void)
{
    printf("%u\n", std::string("\n").find_first_not_of(" ", 0));
}

If you want to only match a substring of the std::string, I think you must call find_first_not_of on an explicit substring, that is:

#include <cstdio>
#include <string>

int main(void)
{
    printf("%u\n", std::string("\n").substr(0, 1).find_first_not_of(" \t\n\v\f\r"));
}

BTW, here is the description of the behavior of the find_first_not_of method:

The find_first_not_of() function either:

  • returns the index of the first character within the current string that does not match any character in str, beginning the search at index, string::npos if nothing is found,
  • searches the current string, beginning at index, for any character that does not match the first num characters in str, returning the index in the current string of the first character found that meets this criteria, otherwise returning string::npos,
  • or returns the index of the first occurrence of a character that does not match ch in the current string, starting the search at index, string::npos if nothing is found.
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文