Skip to main content

String class in C++

In C++, another way of representing an array of characters is using Strings class. Using string class gives many advantages over Character array like:
  • The size of the array has to be predefined if it is getting allocated statically(char arr[3]). In this case, there might be unused bytes which results in inefficient memory usage. But, in string class size won't be wasted as memory is allocated on need basis.
  • There is a possibility of array decay in an array of characters(Array decay happens usually when an array of characters is passed as by the value of reference and the type or dimension is changed). Whereas in string class such problems don't arise as it uses objects.
  • Characters array doesn't provide many inbuilt functions to handle operations on strings. Whereas in the string there are many inbuilt functions to do operations.
Some of the built-in APIs provided by string class are:
  1. getline(cin, str): It is used to take input from the string. cin is also to take input of a set of characters but, it will stop accepting characters after space. getline() captures input until enter is recognized.
  2. str.push_back('z'): It concatenates the 'z' character at the end of the string str.
  3. str.pop_back(): It removes the last character from the string.
  4. capacity(): It returns the size of the memory allocated. It can be greater than the size of the characters so that if new characters are being inserted, it will be efficient.
  5. resize(): It changes the size of a string. With this, the size of the string can be increased or decreased based on the size given.
  6. length(): It returns the size of the string(doesn't include null character).
  7. shrink_to_fit(): As already discussed, the capacity of the string can be more than what is the actual size of the string. In order to shrink it so that the extra space is removed shrink_to_fit() is used.
  8. begin(): It returns the beginning pointer of the string
  9. end(): It returns the end pointer of the string.
  10. rbegin(): It returns the reverse iterator pointing at the end of the string.
  11. rend(): It returns the reverse iterator pointing at the start of the string.
  12. copy("character array", length, position): It copies the string into character array of size as length and starting position as position.
  13.  swap(): It swap the two string.

Comments

Popular posts from this blog

Leet Code: Problem #710 Random Pick with Blacklist

Given a blacklist  B containing unique integers from [0, N) , write a function to return a uniform random integer from [0, N) which is NOT  in B . Optimize it such that it minimizes the call to system’s Math.random() . Note: 1 <= N <= 1000000000 0 <= B.length < min(100000, N) [0, N)  does NOT include N. See interval notation . Example 1: Input: ["Solution","pick","pick","pick"] [[1,[]],[],[],[]] Output: [null,0,0,0] Example 2: Input: ["Solution","pick","pick","pick"] [[2,[]],[],[],[]] Output: [null,1,1,1] Example 3: Input: ["Solution","pick","pick","pick"] [[3,[1]],[],[],[]] Output: [null,0,0,2] Example 4: Input: ["Solution","pick","pick","pick"] [[4,[2]],[],[],[]] Output: [null,1,3,1] Explanation of Input Syntax: The input is two lists: the subroutines called and their argume...

Creating Self Signed SSL Certificates for HTTPS Communication

Self Signed CA: Create Private Key for Self Signed CA openssl ecparam -genkey -name secp256r1 | openssl ec -out ca.key     Create CA Certificate for Self Signed CA openssl req -new -x509 -days 36500 -key ca.key -out ca.pem -subj "/C=IN/ST=Karnataka/L=Bengaluru/O=company name/OU=Prod Operations Department/CN=prodops .domain.com   Verify the content of CA certificate openssl x509 -in ca.pem -noout -text Client CERTIFICATE: CLIENT_ID="<Client-Product>" e.g. CLIENT_ID="ClientID" CLIENT_SERIAL="<Client-Release-Number>" e.g. CLIENT_SERIAL="6889" Create Private Key for Client openssl ecparam -genkey -name secp256r1 | openssl ec -out  ${CLIENT_ID}_${CLIENT_SERIAL}.key                   Generate the Certificate Signing Request CSR openssl req -new -key ${CLIENT_ID}_${CLIENT_SERIAL}.key -out ${CLIENT_ID}_${CLIENT_SERIAL}.csr -subj "/C=IN/ST=Karnataka/L=Bengalur...

Tree Data Structure related must solve programming questions: Part - 1

LeetCode Problem #687 Given a binary tree find the longest possible path with same node values. The length of the path is determined the number of edges between the node. Example 1: Input: 5 / \ 4 5 / \ \ 1 1 5 Output:  2   Example 2: Input: 1 / \ 4 5 / \ \ 4 4 5 Output:  2 Solution in C++: /**  * Definition for a binary tree node.  * struct TreeNode {  *     int val;  *     TreeNode *left;  *     TreeNode *right;  *     TreeNode() : val(0), left(nullptr), right(nullptr) {}  *     TreeNode(int x) : val(x), left(nullptr), right(nullptr) {}  *     TreeNode(int x, TreeNode *left, TreeNode *right) : val(x), left(left), right(right) {}  * };  */ class Solution ...