A RetroSearch Logo

Home - News ( United States | United Kingdom | Italy | Germany ) - Football scores

Search Query:

Showing content from https://en.cppreference.com/w/cpp/language/../experimental/../string/basic_string.html below:

std::basic_string - cppreference.com

The class template basic_string stores and manipulates sequences of character-like objects, which are non-array objects of TrivialType and StandardLayoutType. 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 the Traits template parameter - a specialization of std::char_traits or a compatible traits class.

The elements of a basic_string are stored contiguously, that is, for a basic_string s, &*(s.begin() + n) == &*s.begin() + n for any n in [​0​s.size()), and *(s.begin() + s.size()) has value CharT() (a null terminator)(since C++11); or, equivalently, a pointer to s[0] can be passed to functions that expect a pointer to the first element of an array(until C++11)a null-terminated array(since C++11) of CharT.

std::basic_string satisfies the requirements of AllocatorAwareContainer (except that customized construct/destroy are not used for construction/destruction of elements), SequenceContainer and ContiguousContainer(since C++17).

If any of Traits::char_type and Allocator::value_type is different from CharT, the program is ill-formed.

All member functions of std::basic_string are constexpr: it is possible to create and use std::basic_string objects in the evaluation of a constant expression.

However, std::basic_string objects generally cannot be constexpr, 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:

Type Definition std::string std::basic_string<char> std::wstring std::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> [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 [edit] Data members constexpr size_type npos [static] the special value size_type(-1), its exact meaning depends on the context [edit] Member functions constructs a basic_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] accesses the first character
(public member function) [edit] 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-modifiable basic_string_view into the entire string
(public member function) [edit] Iterators returns an iterator to the beginning
(public member function) [edit] returns an iterator to the end
(public member function) [edit] returns a reverse iterator to the beginning
(public member function) [edit] 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] checks if the string ends with the given suffix
(public member function) [edit] 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 [edit] Literals converts a character array literal to basic_string
(function) [edit] [edit] Helper classes [edit] Deduction guides (since C++17) [edit] Iterator invalidation

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

[edit] Notes

Although it is required that customized construct or destroy is used when constructing or destroying elements of std::basic_string until C++23, all implementations only used the default mechanism. The requirement is corrected by P1072R10 to match existing practice.

[edit] Example
#include <iostream>
#include <string>
 
int main()
{
    using namespace std::literals;
 
    // Creating a string from const char*
    std::string str1 = "hello";
 
    // Creating a string using string literal
    auto str2 = "world"s;
 
    // Concatenating strings
    std::string str3 = str1 + " " + str2;
 
    // Print out the result
    std::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 world
world hello
w
World
[edit] Defect reports

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

DR Applied to Behavior as published Correct behavior LWG 530 C++98 contiguity of the storage for elements of basic_string
was accidently made not required by LWG259 required again LWG 2861 C++98 value_type was Traits::char_type changed to CharT LWG 2994
(P1148R0) C++98 the behavior is undefined if any of Traits::char_type[1]
and Allocator::char_type is different from CharT the program is
ill-formed in this case
  1. ↑ The Traits::char_type case is fixed in P1148R0.
[edit] See also [edit] External links

RetroSearch is an open source project built by @garambo | Open a GitHub Issue

Search and Browse the WWW like it's 1997 | Search results from DuckDuckGo

HTML: 3.2 | Encoding: UTF-8 | Version: 0.7.4