Movatterモバイル変換


[0]ホーム

URL:


cppreference.com
Namespaces
Variants
    Actions

      std::basic_string

      From cppreference.com
      <cpp‎ |string
       
       
       
      std::basic_string
       
      Defined in header<string>
      template<

         class CharT,
         class Traits=std::char_traits<CharT>,
         class Allocator=std::allocator<CharT>

      >class basic_string;
      (1)
      namespace pmr{

      template<
         class CharT,
         class Traits=std::char_traits<CharT>
      >using basic_string=
          std::basic_string<CharT, Traits,std::pmr::polymorphic_allocator<CharT>>;

      }
      (2)(since C++17)

      The class templatebasic_string stores and manipulates sequences ofcharacter-like objects, which are non-array objects ofTrivialType andStandardLayoutType. The class is dependent neither on the character type nor on the nature of operations on that type. The definitions of the operations are supplied via theTraits template parameter - a specialization ofstd::char_traits or a compatible traits class.

      The elements of abasic_string are stored contiguously, that is, for abasic_strings,&*(s.begin()+ n)==&*s.begin()+ n for anyn in[0s.size()), and*(s.begin()+ s.size()) has valueCharT() (a null terminator)(since C++11); or, equivalently, a pointer tos[0] can be passed to functions that expect a pointer to the first element ofan array(until C++11)a null-terminated array(since C++11) ofCharT.

      std::basic_string satisfies the requirements ofAllocatorAwareContainer (except that customizedconstruct/destroy are not used for construction/destruction of elements),SequenceContainer andContiguousContainer(since C++17).

      If any ofTraits::char_type andAllocator::value_type is different fromCharT, the program is ill-formed.

      All member functions ofstd::basic_string areconstexpr: it is possible to create and usestd::basic_string objects in the evaluation of a constant expression.

      However,std::basic_string objects generally cannot beconstexpr, because any dynamically allocated storage must be released in the same evaluation of constant expression.

      (since C++20)

      Several typedefs for common character types are provided:

      Defined in header<string>
      Type Definition
      std::stringstd::basic_string<char>
      std::wstringstd::basic_string<wchar_t>
      std::u8string(C++20)std::basic_string<char8_t>
      std::u16string(C++11)std::basic_string<char16_t>
      std::u32string(C++11)std::basic_string<char32_t>
      std::pmr::string(C++17)std::pmr::basic_string<char>
      std::pmr::wstring(C++17)std::pmr::basic_string<wchar_t>
      std::pmr::u8string(C++20)std::pmr::basic_string<char8_t>
      std::pmr::u16string(C++17)std::pmr::basic_string<char16_t>
      std::pmr::u32string(C++17)std::pmr::basic_string<char32_t>

      Contents

      [edit]Template parameters

      CharT - character type
      Traits - traits class specifying the operations on the character type
      Allocator -Allocator type used to allocate internal storage

      [edit]Nested types

      Type Definition
      traits_typeTraits
      value_typeCharT
      allocator_typeAllocator[edit]
      size_type
      Allocator::size_type(until C++11)
      std::allocator_traits<Allocator>::size_type(since C++11)
      [edit]
      difference_type
      Allocator::difference_type(until C++11)
      std::allocator_traits<Allocator>::difference_type(since C++11)
      [edit]
      referencevalue_type&[edit]
      const_referenceconst value_type&[edit]
      pointer

      Allocator::pointer

      (until C++11)

      std::allocator_traits<Allocator>::pointer

      (since C++11)
      [edit]
      const_pointer

      Allocator::const_pointer

      (until C++11)

      std::allocator_traits<Allocator>::const_pointer

      (since C++11)
      [edit]
      iterator

      LegacyRandomAccessIterator andLegacyContiguousIterator tovalue_type

      (until C++20)

      LegacyRandomAccessIterator,contiguous_iterator, andConstexprIterator tovalue_type

      (since C++20)
      [edit]
      const_iterator

      LegacyRandomAccessIterator andLegacyContiguousIterator toconst value_type

      (until C++20)

      LegacyRandomAccessIterator,contiguous_iterator, andConstexprIterator toconst value_type

      (since C++20)
      [edit]
      reverse_iteratorstd::reverse_iterator<iterator>[edit]
      const_reverse_iteratorstd::reverse_iterator<const_iterator>[edit]

      [edit]Data members

      constexpr size_typenpos[static] the special valuesize_type(-1), its exact meaning depends on the context

      [edit]Member functions

      constructs abasic_string
      (public member function)[edit]
      destroys the string, deallocating internal storage if used
      (public member function)[edit]
      assigns values to the string
      (public member function)[edit]
      assign characters to a string
      (public member function)[edit]
      assign a range of characters to a string
      (public member function)[edit]
      returns the associated allocator
      (public member function)[edit]
      Element access
      accesses the specified character with bounds checking
      (public member function)[edit]
      accesses the specified character
      (public member function)[edit]
      (DR*)
      accesses the first character
      (public member function)[edit]
      (DR*)
      accesses the last character
      (public member function)[edit]
      returns a pointer to the first character of a string
      (public member function)[edit]
      returns a non-modifiable standard C character array version of the string
      (public member function)[edit]
      returns a non-modifiablebasic_string_view into the entire string
      (public member function)[edit]
      Iterators
      returns an iterator to the beginning
      (public member function)[edit]
      (C++11)
      returns an iterator to the end
      (public member function)[edit]
      returns a reverse iterator to the beginning
      (public member function)[edit]
      (C++11)
      returns a reverse iterator to the end
      (public member function)[edit]
      Capacity
      checks whether the string is empty
      (public member function)[edit]
      returns the number of characters
      (public member function)[edit]
      returns the maximum number of characters
      (public member function)[edit]
      reserves storage
      (public member function)[edit]
      returns the number of characters that can be held in currently allocated storage
      (public member function)[edit]
      reduces memory usage by freeing unused memory
      (public member function)[edit]
      Modifiers
      clears the contents
      (public member function)[edit]
      inserts characters
      (public member function)[edit]
      inserts a range of characters
      (public member function)[edit]
      removes characters
      (public member function)[edit]
      appends a character to the end
      (public member function)[edit]
      removes the last character
      (public member function)[edit]
      appends characters to the end
      (public member function)[edit]
      appends a range of characters to the end
      (public member function)[edit]
      appends characters to the end
      (public member function)[edit]
      replaces specified portion of a string
      (public member function)[edit]
      replaces specified portion of a string with a range of characters
      (public member function)[edit]
      copies characters
      (public member function)[edit]
      changes the number of characters stored
      (public member function)[edit]
      changes the number of characters stored and possibly overwrites indeterminate contents via user-provided operation
      (public member function)[edit]
      swaps the contents
      (public member function)[edit]
      Search
      finds the first occurrence of the given substring
      (public member function)[edit]
      find the last occurrence of a substring
      (public member function)[edit]
      find first occurrence of characters
      (public member function)[edit]
      find first absence of characters
      (public member function)[edit]
      find last occurrence of characters
      (public member function)[edit]
      find last absence of characters
      (public member function)[edit]
      Operations
      compares two strings
      (public member function)[edit]
      checks if the string starts with the given prefix
      (public member function)[edit]
      (C++20)
      checks if the string ends with the given suffix
      (public member function)[edit]
      (C++23)
      checks if the string contains the given substring or character
      (public member function)[edit]
      returns a substring
      (public member function)[edit]

      [edit]Non-member functions

      concatenates two strings, a string and achar, or a string andstring_view
      (function template)[edit]
      (removed in C++20)(removed in C++20)(removed in C++20)(removed in C++20)(removed in C++20)(C++20)
      lexicographically compares two strings
      (function template)[edit]
      specializes thestd::swap algorithm
      (function template)[edit]
      erases all elements satisfying specific criteria
      (function template)[edit]
      Input/output
      performs stream input and output on strings
      (function template)[edit]
      read data from an I/O stream into a string
      (function template)[edit]
      Numeric conversions
      (C++11)(C++11)(C++11)
      converts a string to a signed integer
      (function)[edit]
      (C++11)(C++11)
      converts a string to an unsigned integer
      (function)[edit]
      (C++11)(C++11)(C++11)
      converts a string to a floating point value
      (function)[edit]
      (C++11)
      converts an integral or floating-point value tostring
      (function)[edit]
      (C++11)
      converts an integral or floating-point value towstring
      (function)[edit]

      [edit]Literals

      Defined in inline namespacestd::literals::string_literals
      converts a character array literal tobasic_string
      (function)[edit]

      [edit]Helper classes

      hash support for strings
      (class template specialization)[edit]

      [edit]Deduction guides(since C++17)

      [edit]Iterator invalidation

      References, pointers, and iterators referring to the elements of abasic_string may be invalidated by any standard library function taking a reference to non-constbasic_string as an argument, such asstd::getline,std::swap, oroperator>>, and by calling non-const member functions, exceptoperator[],at,data,front,back,begin,rbegin,end, andrend.

      [edit]Notes

      Although it is required that customizedconstruct ordestroy is used when constructing or destroying elements ofstd::basic_string until C++23, all implementations only used the default mechanism. The requirement is corrected byP1072R10 to match existing practice.

      Feature-test macroValueStdFeature
      __cpp_lib_string_udls201304L(C++14)User-defined literals for string types
      __cpp_lib_starts_ends_with201711L(C++20)starts_with,ends_with
      __cpp_lib_constexpr_string201907L(C++20)Constexpr forstd::basic_string
      __cpp_lib_char8_t201907L(C++20)std::u8string
      __cpp_lib_erase_if202002L(C++20)erase,erase_if
      __cpp_lib_string_contains202011L(C++23)contains
      __cpp_lib_string_resize_and_overwrite202110L(C++23)resize_and_overwrite
      __cpp_lib_containers_ranges202202L(C++23)Member functions for construction, insertion, and replacement that acceptcontainer compatible range

      [edit]Example

      Run this code
      #include <iostream>#include <string> int main(){usingnamespace std::literals; // Creating a string from const char*    std::string str1="hello"; // Creating a string using string literalauto str2="world"s; // Concatenating strings    std::string str3= str1+" "+ str2; // Print out the resultstd::cout<< str3<<'\n';     std::string::size_type pos= str3.find(" ");    str1= str3.substr(pos+1);// the part after the space    str2= str3.substr(0, pos);// the part till the space std::cout<< str1<<' '<< str2<<'\n'; // Accessing an element using subscript operator[]std::cout<< str1[0]<<'\n';    str1[0]='W';std::cout<< str1<<'\n';}

      Output:

      hello worldworld hellowWorld

      [edit]Defect reports

      The following behavior-changing defect reports were applied retroactively to previously published C++ standards.

      DRApplied toBehavior as publishedCorrect behavior
      LWG 530C++98contiguity of the storage for elements ofbasic_string
      was accidently made not required byLWG259
      required again
      LWG 2861C++98value_type wasTraits::char_typechanged toCharT
      LWG 2994
      (P1148R0)
      C++98the behavior is undefined if any ofTraits::char_type[1]
      andAllocator::char_type is different fromCharT
      the program is
      ill-formed in this case
      1. TheTraits::char_type case is fixed inP1148R0.

      [edit]See also

      read-only string view
      (class template)[edit]

      [edit]External links

      C++ string handling
      Retrieved from "https://en.cppreference.com/mwiki/index.php?title=cpp/string/basic_string&oldid=182889"

      [8]ページ先頭

      ©2009-2025 Movatter.jp