C++ String front()30 Aug 2024 | 1 min read This function is used to reference a first character of the string. SyntaxConsider a string str. Syntax would be: ParameterThis function does not contain any parameter. Return valueIt is used to return a reference of the first character. Example 1Let's see the simple example. Output: 1 Example 2Let's see an another simple example. Output: j Example 3Let's see a simple example to modify first character using front() function. Output: Hello World Next TopicC++ Strings |
C++ String size() This function is used to return the length of the string in terms of bytes. It defines the actual number of bytes that conform to the contents of the string object which is not necessarily equal to the capacity. Syntax Consider a string object named...
1 min read
C++ String erase() This function removes the characters as specified, reducing its length by one. Syntax Consider a string str. Syntax would be: str.erase(pos,len); str.erase(itr); str.erase(first,last); Parameter pos :It defines the position of the character which is to be removed. len :It defines the number of characters to be erased. Itr : It is an iterator...
1 min read
C++ String crbegin() The crbegin() function stands for 'reverse beginning'. This function references to the last character of the string. Syntax Consider a string str. Syntax would be: str.crbegin(); Parameter This function does not contain any parameter. Return value This function returns the constant iterator pointing to the last character of the string. Example 1 Let's...
1 min read
C++ String insert() This function is used to insert a new character, before the character indicated by the position pos. Syntax Consider two strings str1 and str2, pos is the positon. Syntax would be : str1.insert(pos,str2); Parameters str : String object to be inserted in another string object. pos : It defines the...
2 min read
C++ String capacity() This function gives the current size of the allocated space for the string. Capacity of the string is not necessarily equal to the size or length of the string. If it is greater, it means extra space is allocated for other string operations. Syntax Consider a string...
1 min read
C++ String end() This function is used to return an iterator pointing to the last character of the string. Syntax Consider a string 'str' and an iterator 'it'. Syntax would be: iterator it = str.end(); Parameter This function does not contain any parameter. Return value It returns an iterator pointing to the end of...
1 min read
C++ String replace() This function replaces the portion of string that begins at character position pos and spans len characters. Syntax Consider two strings str1 and str2. Syntax would be: str1.replace(pos,len,str2); Parameters str : str is a string object, whose value to be copied in another string object. pos : pos defines the...
2 min read
C++ String swap() This function is used to exchange the values of two string objects. Syntax Consider two strings s1 and s2 , we want to exchange the values of these two string objects. Its syntax would be : s1.swap(s2) Parameters It contains single parameter, whose value is to be exchanged with...
1 min read
C++ String shrink_to_fit() This function reduces the capacity of the string and makes it equal to its size. Syntax Consider a string str. Synatx would be: str.shrink_to_fit(); Parameter It does not contain any parameter. Return value It does not return any value. Example 1 Let's see the simple example. #include<iostream> using namespace std; int main() { string str="C++ Programming"; cout<<str.capacity()<<'\n'; str.shrink_to_fit(); cout<<str.capacity(); return 0; } Output: 15 15 In this...
1 min read
C++ String cbegin() It gives a reference to the first element of the string. Difference b/w cbegin() and begin() The only difference between cbegin() and begin() is cbegin() returns the constant iterator pointing to the constant value. Syntax Consider a string str. Its syntax would be: str.cbegin(); Parameter This function does not contain...
1 min read
We request you to subscribe our newsletter for upcoming updates.
We provides tutorials and interview questions of all technology like java tutorial, android, java frameworks
G-13, 2nd Floor, Sec-3, Noida, UP, 201301, India