13.18 <cstring>The <cstring> header is for the C++ version of the C standard <string.h> header, which declares string-handling functions. The functions in this section fall into two categories, identified by the first three letters of the function name:
See also <cwchar> for wide character string functions.
The memchr function searches the memory that mem points to, of size n bytes, for the byte whose value is c (converted to unsigned char). The return value is a pointer into the mem array to the first occurrence of c, or a null pointer if c is not present in the first n bytes of mem. See Alsostrchr function, find in <algorithm>, wmemchr in <cwchar>
The memcmp function compares the first n bytes of s1 and s2 as arrays of unsigned char. If all n bytes are equal, the return value is 0. Otherwise, the return value is positive if s1 is greater than s2 or negative if s1 is less than s2. See Alsostrcmp function, strncmp function, equal in <algorithm>, lexicographical_compare in <algorithm>, mismatch in <algorithm>, wmemcmp in <cwchar>
The memcpy function copies n bytes from src to dst. If src and dst overlap, the results are undefined. The return value is dst. If you copy the memory that contains any non-POD objects, the results are undefined. See Chapter 6 for more information about POD objects. See Alsomemmove function, strcpy function, strncpy function, copy in <algorithm>, wmemcpy in <cwchar>, Chapter 6
The memmove function copies n bytes from src to dst. The memory regions can overlap. The return value is dst. If you copy the memory that contains any non-POD objects, the results are undefined. See Chapter 6 for more information about POD objects. See Alsomemcpy function, strcpy function, strncpy function, copy in <algorithm>, copy_backward in <algorithm>, wmemmove in <cwchar>, Chapter 6
The memset function fills the array s with n copies of c (converted to unsigned char). The return value is s. See Alsomemcpy function, fill_n in <algorithm>, wmemset in <cwchar>
The NULL macro expands to a null pointer constant. See <cstddef> for more information. See AlsoNULL in <cstddef>
The size_t type is the type of the result of the sizeof operator. It is an unsigned integral type. The exact type is implementation-defined. See Alsosize_t in <cstddef>
The strcat function concatenates src onto the end of dst, overwriting the null byte that ends dst. The src and dst arrays cannot overlap. The caller must ensure that dst points to a region of memory that is large enough to hold the concatenated result, including its null terminator. See Alsostrcpy function, strncat function, wcscat in <cwchar>
The strchr function returns a pointer to the first occurrence of c (converted to unsigned char) in the null-terminated string s. If c does not appear in s, a null pointer is returned. See Alsomemchr function, strcspn function, strpbrk function, strrchr function, strspn function, wcschr in <cwchar>
The strcmp function compares two null-terminated strings as arrays of unsigned char. If the strings are equal, the return value is 0. Otherwise, the return value is positive if s1 is greater than s2 or negative if s1 is less than s2. If one string is a prefix of the other, the longer string is greater than the shorter string. See Alsomemcmp function, strncmp function, wcscmp in <cwchar>
The strcoll function compares two null-terminated strings, interpreting the strings according to the LC_COLLATE (defined in <clocale>) category of the current C locale. The return value is the same as that of strcmp. See Alsostrcmp function, wcscoll in <cwchar>, collate in <locale>, <clocale>
The strcpy function copies the null-terminated string src to dst. The caller must ensure that dst points to a region of memory that is large enough to hold the entire src string plus its null terminator. The return value is dst. See Alsomemcpy function, strncpy function, wcscpy in <cwchar>
The strcspn function returns the number of characters at the start of str that are not in the string spanset. Thus, the c in its name means complement, that is, strcspn counts characters that are in the complement of the span set. See Alsostrchr function, strpbrk function, strspn function, strstr function, wcscspn in <cwchar>
The strerror function returns a pointer to an error message string that corresponds to the error number errnum. The message is the same as that printed by the perror function. A program must not modify the array returned by strerror, and subsequent calls to strerror can overwrite the array. See Alsoperror in <cstdio>, <cerrno>
The strlen function returns the length of the null-terminated string s, that is, the number of bytes that come before the null byte at the end of the string. See Alsowcslen in <cwchar>
The strncat function concatenates src onto the end of dst, overwriting the null byte at the end of dst. At most, n characters are copied from src. A terminating null character is always appended to the end of dst. The caller must ensure that dst points to a region of memory that is large enough to hold the concatenated result, including the null terminator. The return value is dst. See Alsostrcat function, wcsncat in <cwchar>
The strncmp function compares at most n characters of two null-terminated strings as arrays of unsigned char. If the strings are equal, the return value is 0. Otherwise, the return value is positive if s1 is greater than s2 or negative if s1 is less than s2. If one string is a prefix of the other, the longer string is greater than the shorter string. See Alsomemcmp function, strcmp function, wcsncmp in <cwchar>
The strncpy function copies at most n characters from the null-terminated string src to dst. If src is shorter than dst, null characters are appended to the end so that exactly n characters are always written to dst. The return value is dst. See Alsomemcpy function, strcpy function, wcsncpy in <cwchar>
The strpbrk function searches str for any of the characters in spanset and returns a pointer to the first occurrence of such a character. If none of the characters in spanset appear in str, strpbrk returns a null pointer. See Alsostrchr function, strcspn function, strspn function, wcspbrk in <cwchar>
The strrchr function returns a pointer to the last (rightmost) occurrence of c (converted to unsigned char) in the null-terminated string s. If c does not appear in s, NULL is returned. See Alsomemchr function, strchr function, wcsrchr in <cwchar>
The strspn function returns the number of characters at the start of str that are in the string spanset. See Alsostrchr function, strcspn function, strpbrk function, wcsspn in <cwchar>
The strstr function returns the address in str of the first occurrence of substr or a null pointer if substr does not appear in str. See Alsostrchr function, wcsstr in <cwchar>
The strtok function splits str into separate tokens, separated by one or more characters from delimset. The contents of str are modified when each token is found. To parse a string str, you must call strtok multiple times. The first time, pass str as the first parameter to strtok; for the second and subsequent calls, pass a null pointer. Because strtok saves str, only one series of strtok calls can be active at a time. Each call to strtok can use a different delimset. The strtok function skips over initial delimiters, searching str for the first character that is not in delimset. If it reaches the end of the string without finding any token characters, it returns a null pointer. Otherwise, it saves a pointer to the first non-delimiter character as the start of the token. It then searches for the next delimiter character, which ends the token. It changes the delimiter character to a null character and returns a pointer to the start of the token. When strtok is called with a null pointer as the first parameter, it starts searching for the next token at the point where the previous search ended. See Alsostrcspn function, strpbrk function, strspn function, wcstok in <cwchar>
The strxfrm function transforms the src string by converting each character to its collation order equivalent. The equivalent is copied into dst. Thus, after transforming two different strings with strxfrm, the transformed strings can be compared by calling strcmp to obtain the same result as calling strcoll on the original strings. No more than n bytes are stored in dst, including the trailing null character. If n is 0, dst can be null. The return value is the number of transformed characters written to dst. See Alsostrcmp function, strcoll function, wcsxfrm in <cwchar>, collate in <locale>, <clocale> |