Its time complexity is O(N + M) where N is the size of the first string and M is the size of the second string.Ĭopy(): Copies the substring of the string in the string passed as parameter and returns the number of characters copied. Its time complexity is O(1).Ĭompare(): Compares the value of the string with the string passed in the parameter and returns an integer accordingly. Its time complexity is O(1).Ĭlear(): Erases all the contents of the string and assign an empty string (“”) of length zero. Its time complexity is O(1).īegin(): Returns an iterator pointing to the first character. Its time complexity is O(N) where N is the size of the new string.Īssign(): Assigns new string by replacing the previous value (can also be done using ‘ =’ operator).Īt(): Returns the character at a particular position (can also be done using ‘ ’ operator). String s6 (s1.begin(), s1.begin()+3) // s6 = “Hel”Īppend(): Inserts additional characters at the end of the string (can also be done using ‘ +’ or ‘ +=’ operator). String s4 ("Hello World", 5) // s4 = “Hello” Let us see how to construct a string type. String class provides different string manipulation functions like concatenation, find, replace etc. It is not a built-in data type, but is a container class in the Standard Template Library. You will see the implementations using iterators in the topics explained below.Ĭ++ provides a powerful alternative for the char*. creating a generic function ‘swap (parameter-list)’ using template : But here if we use a generic function, then we can simply make one function and can swap both type of variables by passing their different type in the arguments. Then, we will have to make two functions where one can swap int type variables and the other one can swap float type variables. Let’s try to understand it with an example:Īssume we have to swap two variables of int type and two of float type. Here, ‘type’ is just a placeholder used to store the data type when this function is used you can use any other name instead class is used to specify the generic type of template, alternatively typename can be used instead of it. The syntax for creating a generic function: template return-type function-name (parameter-list) Basically, it tells what type of data is being passed to the function. The way we use normal parameters to pass as a value to function, in the same manner template parameters can be used to pass type as argument to function. So C++ provides a feature to create a single generic function instead of many functions which can work with different data type by using the template parameter. Many times while programming, there is a need for creating functions which perform the same operations but work with different data types. This allows a function or class to work on many different data types without being rewritten for each one. Templates are a feature of the C++ programming language that allows functions and classes to operate with generic types.
0 Comments
Leave a Reply. |