Technically, there is no string data type in the C++ programming language. However, the concept of a string data type makes it easy to handle strings of character data. A single character has some limitations. Many data items are not integers or floating-point values. The message Hi Mom! is a good example of a string. Thus, the need to handle a series of characters as a single piece of data (in English correctly called a datum).
In the "C" programming language all string were handled as an array of characters that end in an ASCII null character (the value 0 or the first character in the ASCII character code set). Associated with object oriented programming the string class has been added to C++ as a standard part of the programming language. This changed with the implementation with strings being stored as a length controlled item with a maximum length of 255 characters. Included in the C++ string class is the reserved word of string as if it were a data type. Some basics about strings include:
|C++ Reserved Word||String|
|Represent||Series of Characters (technically an array)|
|Size||Varies in length|
|Domain (Values Allowed)||Extended ASCII Character Code Set|
|C++ syntax rule||Double quote marks for constants|
For now, we will address only the use of strings as constants. There are numerous methods to process strings with, and we will work with some of those as we prgress through the semester.
C++ has in its definition a way to represent sequence of characters as an object of class. This class is called std:: string. String class stores the characters as a sequence of bytes with a functionality of allowing access to single byte character.
std:: string vs Character Array
- A character array is simply an array of characters can terminated by a null character. A string is a class which defines objects that be represented as stream of characters.
- Size of the character array has to allocated statically, more memory cannot be allocated at run time if required. Unused allocated memory is wasted in case of character array. In case of strings, memory is allocated dynamically. More memory can be allocated at run time on demand. As no memory is preallocated, no memory is wasted.
- There is a threat of array decay in case of character array. As strings are represented as objects, no array decay occurs.
- Implementation of character array is faster than std:: string. Strings are slower when compared to implementation than character array.
- Character array do not offer much inbuilt functions to manipulate strings. String class defines a number of functionalities which allow manifold operations on strings.
Operations on strings
- getline() :- This function is used to store a stream of characters as entered by the user in the object memory.
- push_back() :- This function is used to input a character at the end of the string.
- pop_back() :- Introduced from C++11(for strings), this function is used to delete the last character from the string.
- capacity() :- This function returns the capacity allocated to the string, which can be equal to or more than the size of the string. Additional space is allocated so that when the new characters are added to the string, the operations can be done efficiently.
- resize() :- This function changes the size of string, the size can be increased or decreased.
- length():-This function finds the length of the string
- shrink_to_fit() :- This function decreases the capacity of the string and makes it equal to its size. This operation is useful to save additional memory if we are sure that no further addition of characters have to be made.
- begin() :- This function returns an iterator to beginning of the string.
- end() :- This function returns an iterator to end of the string.
- rbegin() :- This function returns a reverse iterator pointing at the end of string.
- rend() :- This function returns a reverse iterator pointing at beginning of string.
- A series or array of characters as a single piece of data.
"String Data Type" by Kenneth Leroy Busbee, (Download for free at http://cnx.org/contents/303800f3-07f...email@example.com) is licensed under CC BY 4.0
"std::string class in C++" by Manjeet Singh, Geeks for Geeks is licensed under CC BY-SA 4.0