GV

GeezerVison

The World Wide Web, restored to 800 pixels of glory.

Home|About|Gaming cabinet|Archives|How it works|Starter sites

On this site

  • cpprefjp - C++日本語リファレンス
  • GitHub Project

This view

  • Original page
  • New address

GeezerVison is showing cpprefjp.github.io — std::basic_string::find_first_not_of - cpprefjp C++日本語リファレンス

  1. リファレンス
  2. string
  3. basic_string
  4. find_first_not_of

最終更新日時: 2026年08月25日 12時11分17秒 (JST)
Akira Takahashi が更新

履歴 編集

function
<string>

std::basic_string::find_first_not_of

size_type
  find_first_not_of(const basic_string& str,
                    size_type pos = 0) const;          // (1) C++98
size_type
  find_first_not_of(const basic_string& str,
                    size_type pos = 0) const noexcept; // (1) C++11
constexpr size_type
  find_first_not_of(const basic_string& str,
                    size_type pos = 0) const noexcept; // (1) C++20

size_type
  find_first_not_of(const charT* s,
                    size_type pos,
                    size_type n) const; // (2) C++98
constexpr size_type
  find_first_not_of(const charT* s,
                    size_type pos,
                    size_type n) const; // (2) C++20

size_type
  find_first_not_of(const charT* s,
                    size_type pos = 0) const; // (3) C++98
constexpr size_type
  find_first_not_of(const charT* s,
                    size_type pos = 0) const; // (3) C++20

size_type
  find_first_not_of(charT c,
                    size_type pos = 0) const; // (4) C++98
constexpr size_type
  find_first_not_of(charT c,
                    size_type pos = 0) const; // (4) C++20

// string_viewを引数に取るオーバーロード
template <class T>
size_type
  find_first_not_of(const T& t,
                    size_type pos = 0) const noexcept(下記参照); // (5) C++17
template <class T>
constexpr size_type
  find_first_not_of(const T& t,
                    size_type pos = 0) const noexcept(下記参照); // (5) C++20

概要

指定された文字列中のいずれの文字にも一致しない最初の場所を検索する。

テンプレートパラメータ制約

  • (5) :
    • is_convertible_v<const T&, basic_string_view<charT, traits>>がtrueであること
    • is_convertible_v<const T&, const charT*>がfalseであること

要件

  • (3) の形式の場合、s は少なくとも traits_type::length(s) + 1 の要素を持つ charT の配列を指していること。

効果

  • (1) : pos 以降で最初に str 内に存在しない文字の位置を返す。
  • (2) : pos 以降で最初に s 内に存在しない文字の位置を返す。s は長さ n の文字列へのポインタである。
  • (3) : (2) と同様だが、こちらは NULL 終端の文字列を扱う。
  • (4) : pos 以降で最初に c と一致しない文字の位置を返す。
  • (5) : basic_string_view<charT, traits> sv = t;として変数svを作成し、pos 以降で最初に sv 内に存在しない文字の位置を返す。

戻り値

見つかればその位置を返し、見つからない場合は basic_string::npos を返す。

例外

  • (1) : 投げない
  • (5) : noexcept内の式は、以下と等価である
    is_nothrow_convertible_v<const T&, basic_string_view<charT, traits>>
    

備考

  • 一致しない文字の検索は、指定された文字列の各文字について traits_type::eq を使用することで行われる。
    例えば、(1) の形式の場合、以下のような条件を満たす最小の xpos を求める。
    • pos <= xpos かつ xpos < size()
    • 0 <= I かつ I < str.size() を満たす全ての I について、!traits_type::eq(at(xpos), str.at(I))
  • (3) の形式の場合、s の文字列長は traits_type::length(s) で求められる。

例

#include <iostream>
#include <string>

int main()
{
  const std::string s("Hello, world. Welcome to C++ world.");
  const std::string str("world");

  // "Hello" の "l" 以降で "world" を指定すると、"," が見つかる
  std::cout << s.find_first_not_of(str, 2) << std::endl;

  // "Hello" の "e" 以降で "worlde,. " を指定すると、"Welcome" の 1 文字目の "W" が見つかる
  std::cout << s.find_first_not_of("worlde,. ", 1) << std::endl;

  // "C++" の 1文字目の "+" 以降で '+' を指定すると、直後の空白が見つかる
  std::cout << s.find_first_not_of('+', 26) << std::endl;
}

出力

5
14
28

実装例

(1) 以外の形式は、(1) の形式を使用して実装することができる。

// (2)
template <class charT, class traits, class Allocator>
size_type basic_string<charT, traits, Allocator>::find_first_not_of(const charT* s, size_type pos, size_type n) const
{
  return find_first_not_of(std::basic_string(s, n), pos);
}

// (3)
template <class charT, class traits, class Allocator>
size_type basic_string<charT, traits, Allocator>::find_first_not_of(const charT* s, size_type pos = 0) const
{
  return find_first_not_of(std::basic_string(s), pos);
}

// (4)
template <class charT, class traits, class Allocator>
size_type basic_string<charT, traits, Allocator>::find_first_not_of(charT c, size_type pos = 0) const
{
  return find_first_not_of(std::basic_string(1, c), pos);
}

参照

  • LWG2064 - More noexcept issues in basic_string
  • P0254R2 Integrating std::string_view and std::string
  • LWG Issue 2946. LWG 2758's resolution missed further corrections
    • 意図しない暗黙変換防止のためにstring_viewを受けるオーバーロード(5)の引数型をconst T&に変更
  • LWG Issue 4259. P1148R0 changed the return values of searching functions of std::basic_string on some platforms
    • 検索に失敗したときにbasic_string自身のnposを返すことが明確化された。P1148R0によってbasic_string_view::nposを変換した値を返すようになっていたため、アロケータのsize_typeがstring_viewと異なる幅を持つ環境で戻り値が変わってしまう問題があった。この変更は欠陥報告 (DR) であり、C++26より前のバージョンでもコンパイラが早期に対応している場合がある
  • P0980R1 Making std::string constexpr
You are visitor 01402503 — GeezerVison does not store the pages you look up.
Best viewed with Netscape Navigator 4.7 or Internet Explorer 6 at 800×600. No cookies required.