C++ Programming Code Examples
C++ > Strings Code Examples
Important functions supported by String Class
/* Important functions supported by String Class */
append(): This function appends a part of a string to another string
assign():This function assigns a partial string
at(): This function obtains the character stored at a specified location
begin(): This function returns a reference to the start of the string
capacity(): This function gives the total element that can be stored
compare(): This function compares a string against the invoking string
empty(): This function returns true if the string is empty
end(): This function returns a reference to the end of the string
erase(): This function removes character as specified
find(): This function searches for the occurrence of a specified substring
length(): It gives the size of a string or the number of elements of a string
swap(): This function swaps the given string with the invoking one
Strings are objects that represent sequences of characters. The standard string class provides support for such objects with an interface similar to that of a standard container of bytes, but adding features specifically designed to operate with strings of single-byte characters. The string class is an instantiation of the basic_string class template that uses char (i.e., bytes) as its character type, with its default char_traits and allocator types. Note that this class handles bytes independently of the encoding used: If used to handle sequences of multi-byte or variable-length characters (such as UTF-8), all members of this class (such as length or size), as well as its iterators, will still operate in terms of bytes (not actual encoded characters).
Return length of string. Returns the length of the string, in terms of bytes. This function is used to find the length of the string in terms of bytes. This is the actual number of bytes that conform the contents of the string , which is not necessarily equal to the storage capacity. This is the number of actual bytes that conform the contents of the string, which is not necessarily equal to its storage capacity. Note that string objects handle bytes without knowledge of the encoding that may eventually be used to encode the characters it contains. Therefore, the value returned may not correspond to the actual number of encoded characters in sequences of multi-byte or variable-length characters (such as UTF-8).
Swap string values. Exchanges the content of the container by the content of str, which is another string object. Lengths may differ. After the call to this member function, the value of this object is the value str had before the call, and the value of str is the value this object had before the call. Notice that a non-member function exists with the same name, swap, overloading that algorithm with an optimization that behaves like this member function. Function does not return any value.
Append to string. This function is used to extend the string by appending at the end of the current value. Extends the string by appending additional characters at the end of its current value. Append is a special function in the string library of C++ which is used to append string of characters to another string and returns * this operator. This is similar to push_back or += operator, but it allows multiple characters to append at the same time. This means a character array can also be appended but it doesn't allow appending a single character. It also allows to append a specific part of the second string to the first string or defining the number of time a string must be appended. An iterator range is also provided to iterate over the character of strings.
Return size of allocated storage. Returns the size of the storage space currently allocated for the string, expressed in terms of bytes. This function gives the current size of the allocated space for the string. This capacity is not necessarily equal to the string length. It can be equal or greater, with the extra space allowing the object to optimize its operations when new characters are added to the string. Notice that this capacity does not suppose a limit on the length of the string. When this capacity is exhausted and more is needed, it is automatically expanded by the object (reallocating it storage space). The theoretical limit on the length of a string is given by member max_size.
The function in C++ language is also known as procedure or subroutine in other programming languages. To perform any task, we can create function. A function can be called many times. It provides modularity and code reusability. Functions are used to provide modularity to a program. Creating an application using function makes it easier to understand, edit, check... Function declaration, is done to tell the compiler about the existence of the function. Function's return type, its name & parameter list is mentioned. Function body is written in its definition. Functions are called by their names. If the function is without argument, it can be called directly using its name. But for functions with arguments, we have two ways to call them:
Compare strings. Compares the value of the string object (or a substring) to the sequence of characters specified by its arguments. The compared string is the value of the string object or -if the signature used has a pos and a len parameters- the substring that begins at its character in position pos and spans len characters. This string is compared to a comparing string, which is determined by the other arguments passed to the function.
Assign content to string. Assigns a new value to the string, replacing its current contents. This functions assigns a new value to the string, replacing all its current contents. assign() is a library function of "string" class and it is used to assign, replace the string. This function is overloaded function, we can use it for many purposes i.e. to assign the string, replace a part of the string, any constant value etc.
Test if string is empty. The C++ string::empty function is used to check whether the string is empty or not. It returns true if the length of the string is zero, else returns false. Returns whether the string is empty (i.e. whether its length is 0). This function does not modify the value of the string in any way. To clear the content of a string, see string::clear. This function does not accept any parameter. empty() function returns true if the string length is 0, false otherwise.
Erase characters from string. Erases part of the string, reducing its length. erase() string function removes the characters as specified, reducing its length by one. • sequence (1) Erases the portion of the string value that begins at the character position pos and spans len characters (or until the end of the string, if either the content is too short or if len is string::npos. Notice that the default argument erases all characters in the string (like member function clear). • character (2) Erases the character pointed by p. • range (3) Erases the sequence of characters in the range [first,last).
Return iterator to beginning. Returns an iterator pointing to the first character of the string. This function gives a reference to the first element. The C++ string::begin function returns the iterator pointing to the first character of the string. Note that, Unlike the string::front function, which returns a direct reference to the first character, it returns the iterator pointing to the same character of the string. This function does not return any value.
C++ Program demonstrates implementation of "Multiset in STL". "Insert element" into the multiset. "Delete element" from the multiset. "Find element" in a multiset. Count elements