Monday, November 13, 2023

basic_string


Overview
strings are essential part of any programming language. In C/C++ strings are represented as set of characters, terminated by a null character. 
A string can be created as a const char* or as char[]. However for backward compatibility, it is referred as const char *, when passed by value as shown below. Note that the compiler adds null character implicitly at the end. These strings are also known as cstring.
template <typename T>
void printtype(T) 
{
    cout << typeid(T).name() << endl;
}

//Example
const char *str = "hello,";
const char str2[9] = "world!";
printtype(str);   //prints char const * 
printtype( str2); //prints char const * 

The example 6  depicts the usage.

Details
CRT provides plethora of functions to handle strings. There are different functions for getting length, append, copy etc. 
std::basic_string class attempts to objectify strings so that it's easier to use. string is  a template based class as defined below. 
syntax
//charT - one of the character types char_t, wchar_t, char16_t or char32_t
//traits - defines null character, comparison and other properties
template <typename charT,
typename traits = char_traits<charT>,
typename Allocator = allocator<charT> >
class basic_string;

//Predefined string classes 
typedef basic_string<char_t> string;
typedef basic_string<wchar_t> wstring;
typedef basic_string<char16_t> u16string;
typedef basic_string<char32_t> u32string;

char_traits classes define common behavior such as comparison, assignment, copy etc. and also other aspects such as eof  type, offset type position type etc.

Buffering
Internally raw strings are stored in heap as shown below. It's possible to change storage. This example 15 allocates 100 mb string  using a shared memory based custom allocator class.
Constants
npos is used in constructor, string modification functions. Based on the context, it means end of string.

Constructors
The following constructors are available. Note [khrisha rao]  denotes a string object. "khrisha rao"  denotes a string literal.
NameDescription
string() default constructor
string(const string&)copy constructor
string(string&&)move constructor
string(initializer_list<char>)initializer list constructor.
string(const string& s, size_t p, size_t n=npos)from s, copy n chars, starting from p
string(const char* s)copy all chars from c string s
string(const char* s, size_t n)copy n chars from c string s
string(size_t n, char c)fill n chars with char c
string(InputIterator b, InputIterator e)copy chars from iterator b to iterator e
The example 7 depicts the usage.

Access using Iterators
Following functions return one of the iterators - random access iterators, const access iterators, reverse iterators and  const access reverse iterators.
NameDescription
iterator begin()Return iterator to beginning
iterator end()Return iterator to end
reverse_iterator rbegin()Return reverse iterator to reverse beginning
reverse_iterator rend()Return reverse iterator to reverse end
const_iterator cbegin()Return const_iterator to beginning
const_iterator cend()Return const_iterator to end
const_reverse_iterator  crbegin()Return const_reverse_iterator to reverse beginning
const_reverse_iterator  crend()Return const_reverse_iterator to reverse end
The example 8 depicts the usage.

Storage and length
The following functions returns length, capacity of the underlying string buffer.
NameDescription
const char* c_str()Get C string equivalent
const char* data()Same as c_str()  except null character in the end.
size_t size()Return length of string
size_t length()Return length of string
size_t max_size()Return maximum size of string
  1. void resize(size_t n)
  2. void resize(size_t n, char  c)  
  1. Resize string to size n and copy contents. The string should be initialized.
  2. Resize string to size n and fill with char c. The string should be empty.
size_t  capacity()Return size of allocated storage
void reserve(size_t n=0)Change  capacity to size n  or minimum
void clear()Clear string
bool empty()Test if string is empty
void shrink_to_fit()Shrink to fit
The example 9 depicts the usage.

Access using Index
Access individual elements in the string
NameDescription
  1. char& at(size_t p)
  2. const char& at(size_t p) const 
Get character in string at position p
  1. char& back()
  2. const char& back() const 
Access last character
  1. char& front()
  2. const char& front() const 
Access first character
  1. char& operator[] (size_t p)
  2. const char& operator[] (size_t p) const 
indexed access to characters of the string

String Modifications
Perform operations such as insert, append, replace, erase, assign and swap
NameDescription
  1. string& append(const string& s)
  2. string& append(const string& s, size_t p, size_t n)
  3. string& append(const char* s)
  4. string& append(const char* s, size_t n)
  5. string& append(size_t n, char c)
  6. string& append(InputIterator b, InputIterator e)
  7. string& append(initializer_list<char> il)
  1. Append string s
  2. Append substring from s
  3. Append c string s
  4. Append n chars from c string s
  5. Append  char c, n times
  6. Append  chars from iterators
  7. Append  chars from initializer list
  1. string& assign(const  string& s)
  2. string& assign(const string& s, size_t p, size_t n)
  3. string& assign(const char* s)
  4. string& assign(const char* s, size_t n)
  5. string& assign(size_t n, char c)
  6. string& assign(InputIterator b, InputIterator e)
  7. string& assign(initializer_list<char> il)
  8. string&  assign (string&& s)
  1. Assign string s
  2. Assign substring from s
  3. Assign c string s
  4. Assign n chars from c string s
  5. Assign char c, n times
  6. Assign chars from iterators
  7. Assign chars from initializer list
  8. Assign string s
  1. string&  insert (size_t p, const string& s)
  2. string&  insert (size_t p, const string& s, size_t sp, size_t n)
  3. string&  insert (size_t p, const char* s)
  4. string&  insert (size_t p, const char* s, size_t n)
  5. string&  insert (size_t p,   size_t n, char c)
  6. iterator insert (const_iterator p, size_t n, char c)
  7. iterator insert (const_iterator p, char c)
  8. iterator insert (iterator p, InputIterator b, InputIterator e)
  9. iterator  insert (const_iterator p, initializer_list<char> )
  1. insert string s at position p
  2. insert substring from s at position p
  3. insert c string s at position p
  4. insert n chars c string s at position p
  5. insert char c, n times at position p
  6. insert char c, n times at iterator p
  7. insert char c, at iterator p
  8. insert chars from b to e, at iterator p
  9. insert chars from initializer list at iterator p
  1. string&  erase (size_t p = 0, size_t n = npos)
  2. iterator erase (const_iterator p)
  3. iterator erase (const_iterator b, const_iterator e)
  1. Erase n chars at p 
  2. Erase char at p 
  3. Erase chars from b to e 
  1. string&  replace (size_t p, size_t n, const string& s)
  2. string&  replace (const_iterator i1, const_iterator i2, const string& s) 
  3. string& replace (size_t p, size_t n, const string& s, size_t sp, size_t sn)
  4. string&  replace (size_t p, size_t n, const char* s)
  5. string&  replace (const_iterator i1, const_iterator i2, const char* s)
  6. string&  replace (size_t p, size_t n, const char* s, size_t sn)
  7. string&  replace (const_iterator i1, const_iterator i2, 
    const 
    char
    * s, size_t n)
  8. string&  replace (size_t  pos, size_t len, size_t n, char c)
  9. string&  replace (const_iterator i1, const_iterator i2, size_t n, char c)
  10. string&  replace (const_iterator i1,
     const_iterator i2, InputIterator  b, InputIterator e)
  11. string&  replace (const_iterator i1, const_iterator i2, initializer_list<char>)
  1. replace n chars from string s at position p
  2. replace chars from i1 to i2 from string s
  3. replace n chars at position p from substring of string s 
  4. replace n chars at position p from cstring s
  5. replace chars from i1 to i2 from cstring s
  6. replace n chars or less at position p from cstring s 
  7. replace n chars from i1 to i2 from cstring s
  8. replace n chars with char c at position p
  9. replace n chars from i1 to i2 with char c
  10. replace chars from i1 to i2 with chars from iterators b to e
  11. replace chars from i1 to i2 with chars from initializer list
void push_back(char c)Append character to string
void pop_back()Delete last character
void swap (string& x, string& y) Exchanges the values of two strings
void swap(string& s)Swap string values with s
The example 10 depicts the usage.

String Operations
Perform operations such as extract string buffer, copy, find, compare and substring. The find functions return the position of the match, otherwise, npos.

compare functions, compare full or substring with a string  or its substring or a cstring or its substring. 
The return values are
Result  Description
0compare equal.
< 0either the value of the first character that does not match is lower in the compared string,
or all compared characters match but the compared string is shorter.
> 0either the value of the first character that does not match is greater in the compared string,
or all compared characters match but the compared string is longer.

NameDescription
size_t copy (char* s, size_t n, size_t p = 0)Copy n chars from position p to buffer s
  1. size_t find (const string& s, size_t p = 0) 
  2. size_t find (const char* s, size_t p = 0)
  3. size_t find (const char* s, size_t p, size_t n) 
  4. size_t find (char c, size_t p = 0) 
  1. Find string s from position p
  2. Find cstring s from position p
  3. Find n chars in cstring s from position p
  4. Find char c from position p
  1. size_t rfind (const string& s, size_t p = npos) 
  2. size_t rfind (const char* s, size_t p =npos)
  3. size_t rfind (const char* s, size_t p, size_t n) 
  4. size_t rfind (char c, size_t p=npos) 
  1. Reverse find string s from position p
  2. Reverse find cstring s from position p
  3. Reverse find n chars in cstring s from position p
  4. Reverse find char c from position p
  1. size_t find_first_of(const string& s, size_t p = 0) 
  2. size_t find_first_of(const char* s, size_t p = 0)
  3. size_t find_first_of(const char* s, size_t p, size_t n) 
  4. size_t find_first_of(char c, size_t p = 0)
  1. Find any char in string s from position p
  2. Find any char in cstring s from position p
  3. Find any of n chars in cstring s from position p
  4. Find char c from position p
  1. size_t find_last_of(const string& s, size_t p = npos) 
  2. size_t find_last_of(const char* s, size_t p = npos)
  3. size_t find_last_of(const char* s, size_t p, size_t n) 
  4. size_t find_last_of(char c, size_t p = npos)
  1. Reverse Find any char in string s from position p
  2. Reverse Find any char in cstring s from position p
  3. Reverse Find any of n chars in cstring s from position p
  4. Reverse Find char c from position p
  1. size_t find_first_not_of(const string& s, size_t p = 0) 
  2. size_t find_first_not_of(const char* s, size_t p = 0)
  3. size_t find_first_not_of(const char* s, size_t p, size_t n) 
  4. size_t find_first_not_of(char c, size_t p = 0)
  1. Find any char not in string s from position p
  2. Find any char not in cstring s from position p
  3. Find any of n chars not in cstring s from position p
  4. Find any char but not char c from position p
  1. size_t find_last_not_of(const string& s, size_t p = npos) 
  2. size_t find_last_not_of(const char* s, size_t p = npos)
  3. size_t find_last_not_of(const char* s, size_t p, size_t n) 
  4. size_t find_last_not_of(char c, size_t p = npos)
  1. Reverse Find any char not in string s from position p
  2. Reverse Find any char not in cstring s from position p
  3. Reverse Find any of n chars not in cstring s from position p
  4. Reverse Find any char but not char c from position p
  1. int compare (const string& s) 
  2. int compare (size_t p, size_t n, const string& s)
  3. int compare (size_t p, size_t n, const  string& s, size_t sp, size_t  sn)
  4. int compare (const char* s)
  5. int compare (size_t p, size_t n, const char* s)
  6. int compare (size_t p, size_t n, const char* s, size_t n)
  1. Compare with string s
  2. Compare substring with string s
  3. Compare substring with substring of string s
  4. Compare with cstring s
  5. Compare substring with cstring s
  6. Compare substring with substring of cstring s
string substr (size_t p = 0, size_t n = npos)Generate substring
The example 11 depicts the usage.

Overloaded Operators
Following overloaded operators are implemented externally and internally.
NameDescription
  1. string operator+ (const string& lhs, const string& rhs)
  2. string operator+ (const string& lhs, const char*   rhs)
  3. string operator+ (const char * lhs, const string& rhs)
  4. string operator+ (const string& lhs, char rhs)
  5. string operator+ (char lhs, const string& rhs)
  1. add two strings. return the result.
  2. add string and cstring. return the result.
  3. add cstring and string. return the result.
  4. add string and char. return the result.
  5. add char and string. return the result.
  1. string& operator=(const string& str)
  2. string& operator=(const char* s)
  3. string& operator=(char c)
  4. string& operator=(initializer_list<char>)
  5. string& operator=(string&& s)
  1. assign string s
  2. assign cstring s
  3. assign char c
  4. assign from initializer list
  5. assign and move string s
  1. string& operator+= (const string& str)
  2. string& operator+= (const char * s)
  3. string& operator+= (char c)
  1. Append a string
  2. Append a cstring
  3. Append a char

Relational Operators
These operators are externally implemented for string comparison.
NameDescription
  1. bool operator== (const string& lhs, const string& rhs)
  2. bool operator== (const char *   lhs, const string& rhs)
  3. bool operator== (const string& lhs, const  char *   rhs)
Equality operator for string
  1. bool operator!= (const string& lhs, const string& rhs)
  2. bool operator!= (const char *   lhs, const string& rhs)
  3. bool operator!= (const string& lhs, const char *   rhs)
Non Equality operator for string
  1. bool operator< (const  string& lhs, const string& rhs)
  2. bool operator< (const char *   lhs, const string& rhs)
  3. bool operator< (const string& lhs, const char *   rhs)
Less than operator for string
  1. bool operator<= (const string& lhs, const string& rhs)
  2. bool operator<= (const char *   lhs, const string& rhs)
  3. bool operator<= (const string& lhs, const  char *   rhs)
 Less than equal operator for string
  1. bool operator> (const string& lhs, const string& rhs)
  2. bool operator> (const char *   lhs, const string& rhs),
  3. bool operator> (const string& lhs, const char*   rhs)
 Greater than operator for string
  1. bool operator>= (const string& lhs, const string& rhs)
  2. bool operator>=(const char *   lhs, const string& rhs),
  3. bool operator>= (const string& lhs, const char *   rhs)
 Greater than equal operator for string

Stream operators and functions
The following overloaded function can be used with stream read and write operations.
NameDescription
istream& operator>> (istream& is, string& str)Extract string from stream
ostream& operator<< (ostream& os, const string& str)Insert string into stream
  1. istream& getline (istream& is, string& str, char delim)
  2. istream& getline (istream& is, string& str)
Externally overloaded function, reads a line from stream into string
The example 12 depicts the usage.

Convert strings to arithmetic types
The following functions convert strings to arithmetic type. The supported char types are char and wchar.
The value of the parameter  const string&  str can be preceded with or whitespace but followed by a numerical value. which can be trailed by any character. Examples:"99", "  99namaskara" etc
The value of the parameter size_t* idx , is set by the function to the position of the next character in str after the numerical value. This parameter can also be a null pointer, in which case it is not used. 
The value of the parameter int base represents numerical base (radix) that determines the valid characters and their interpretation.
If this is 0, the base used is determined by the format in the sequence. Notice that by default this argument is 10, not 0.
NameDescription
int stoi (const string&  str, size_t* idx = 0, int base = 10)
Convert string to integer
long stol (const  string&  str, size_t* idx = 0, int base = 10)
Convert string to long int
unsigned long stoul (const string&  str, size_t* idx = 0, int base = 10)
Convert string to unsigned integer
long long stoll (const string&  str, size_t* idx = 0, int base = 10)
Convert string to long long
unsigned long long stoull (const str, string&  str, size_t* idx = 0, int base = 10)
Convert string to unsigned long long
float stof (const string&  str, size_t* idx = 0)
Convert string to float
double stod (const  string&  str, size_t* idx = 0)
Convert string to double
long double stold (const string&  str, size_t* idx = 0)
Convert string to long double
The example 13 depicts the usage.

Convert arithmetic types to string
The following functions convert strings to arithmetic type. The supported char types are char and wchar.
NameDescription
  1. string to_string (int val)
  2. wstring to_wstring (int val)
Convert integer to string / wstring
  1. string to_string (long val)
  2. wstring to_wstring (long val)
Convert long int to string / wstring
  1. string to_string (long long val)
  2. wstring to_wstring (long long val)
Convert long long to string / wstring
  1. string to_string (unsigned val)
  2. wstring to_wstring (unsigned val)
Convert unsigned int to string / wstring
  1. string to_string (unsigned long val)
  2. wstring to_wstring (unsigned long val)
Convert unsigned long to string / wstring
  1. string to_string (unsigned long long val)
  2. wstring to_wstring (unsigned long long val)
Convert unsigned long long to string / wstring
  1. string to_string (float val)
  2. wstring to_wstring (float val)
Convert float to string / wstring
  1. string to_string (double val)
  2. wstring to_wstring (double val)
Convert double to string / wstring
  1. string to_string (long double val)
  2. wstring to_wstring (long double val)
Convert long double to string / wstring
The example 14 depicts the usage.




No comments:

Post a Comment