Happy Codings - Programming Code Examples
Html Css Web Design Sample Codes CPlusPlus Programming Sample Codes JavaScript Programming Sample Codes C Programming Sample Codes CSharp Programming Sample Codes Java Programming Sample Codes Php Programming Sample Codes Visual Basic Programming Sample Codes


C++ Programming Code Examples

Learn C++ Language

Strings in C++ Programming Language

Strings in C++ Language
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).
Declaration for Strings in C++
char str[4] = "C++ Programming"; char str[] = {'C','+','+','\0'}; char str[4] = {'C','+','+','\0'};
In C programming, the collection of characters is stored in the form of arrays. This is also supported in C++ programming. Hence it's called C-strings. C-strings are arrays of type char terminated with null character, that is, \0 (ASCII value of null character is 0). • A character array is simply an array of characters that can be terminated by a null character. A string is a class that defines objects that be represented as a stream of characters. • The size of the character array has to be allocated statically, more memory cannot be allocated at run time if required. Unused allocated memory is wasted in the case of the character array. In the 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 the case of the 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 arrays do not offer many inbuilt functions to manipulate strings. String class defines a number of functionalities that allow manifold operations on strings.
String Functions in C++
• int compare(const string& str): It is used to compare two string objects. • int length(): It is used to find the length of the string. • void swap(string& str): It is used to swap the values of two string objects. • string substr(int pos,int n): It creates a new string object of n characters. • int size(): It returns the length of the string in terms of bytes. • void resize(int n): It is used to resize the length of the string up to n characters. • string& replace(int pos,int len,string& str): It replaces portion of the string that begins at character position pos and spans len characters. • string& append(const string& str): It adds new characters at the end of another string object. • char& at(int pos): It is used to access an individual character at specified position pos. • int find(string& str,int pos,int n): It is used to find the string specified in the parameter. • int find_first_of(string& str,int pos,int n): It is used to find the first occurrence of the specified sequence. • int find_first_not_of(string& str,int pos,int n ): It is used to search the string for the first character that does not match with any of the characters specified in the string. • int find_last_of(string& str,int pos,int n): It is used to search the string for the last character of specified sequence. • int find_last_not_of(string& str,int pos): It searches for the last character that does not match with the specified sequence. • string& insert(): It inserts a new character before the character indicated by the position pos. • int max_size(): It finds the maximum length of the string. • void push_back(char ch): It adds a new character ch at the end of the string. • void pop_back(): It removes a last character of the string. • string& assign(): It assigns new value to the string. • int copy(string& str): It copies the contents of string into another. • char& back(): It returns the reference of last character. • Iterator begin(): It returns the reference of first character. • int capacity(): It returns the allocated space for the string. • const_iterator cbegin(): It points to the first element of the string. • const_iterator cend(): It points to the last element of the string. • void clear(): It removes all the elements from the string. • const_reverse_iterator crbegin(): It points to the last character of the string. • const_char* data(): It copies the characters of string into an array. • bool empty(): It checks whether the string is empty or not. • string& erase(): It removes the characters as specified. • char& front(): It returns a reference of the first character. • string& operator+=(): It appends a new character at the end of the string. • string& operator=(): It assigns a new value to the string. • char operator[](pos): It retrieves a character at specified position pos. • int rfind(): It searches for the last occurrence of the string. • iterator end(): It references the last character of the string. • reverse_iterator rend(): It points to the first character of the string. • void shrink_to_fit(): It reduces the capacity and makes it equal to the size of the string. • char* c_str(): It returns pointer to an array that contains null terminated sequence of characters. • const_reverse_iterator crend(): It references the first character of the string. • reverse_iterator rbegin(): It reference the last character of the string. • void reserve(inr len): It requests a change in capacity. • allocator_type get_allocator();: It returns the allocated object associated with the string.
Non-member Function Overloads
• operator+ Concatenate strings (function ) • relational operators Relational operators for string (function ) • swap Exchanges the values of two strings (function ) • operator>> Extract string from stream (function ) • operator<< Insert string into stream (function ) • getline Get line from stream into string (function )
Operators used for String Objects
• =: assignment • +: concatenation • ==: Equality • !=: Inequality • <: Less than • <=: Less than or equal • >: Greater than • >=: Greater than or equal • []: Subscription • <<: Output • >>: Input
/* C++ String Library */ /* The C-Style Character String */ // C++ Program to demonstrate the working of getline(), push_back() and pop_back() #include <iostream> #include <string> // for string class using namespace std; // Driver Code int main() { // Declaring string string str; // Taking string input using getline() getline(cin, str); // Displaying string cout << "The initial string is : "; cout << str << endl; // Inserting a character str.push_back('s'); // Displaying string cout << "The string after push_back operation is : "; cout << str << endl; // Deleting a character str.pop_back(); // Displaying string cout << "The string after pop_back operation is : "; cout << str << endl; return 0; }










Takes the input of the number of 'vertexes' & the number of edges in the graph. It takes the input of vertex pairs for the given number of edges. It generates a line graph for the given