Introduction to Programming - Lecture 11 - Strings PDF

Summary

These lecture notes cover the topic of strings in C++. It explores various string operations, such as copying, searching, insertion, and deletion. The comparison between C-style strings and C++ strings is outlined. The notes include examples to illustrate the concepts discussed.

Full Transcript

Introduction to Programming Lecture 11 : Strings Askar Khaimuldin Senior-lecturer [email protected] Basics of std::string The class template basic_string provides typical string-manipulation operations:...

Introduction to Programming Lecture 11 : Strings Askar Khaimuldin Senior-lecturer [email protected] Basics of std::string The class template basic_string provides typical string-manipulation operations: copying, searching, insertion/deletion, etc. The definition of that template is provided in namespace std; It also creates the alias type “string” for basic_string typedef basic_string string; Basics of std::string #include String objects have several build-in member functions (methods) It can increase its length dynamically Moreover, it is possible to use a string object as an array of chars Nevertheless, it is prohibited to use cells of memory which are not meant to be used It is better to use at(i) function It will not cause an error, only exception String vs C-style string A C-style string is simply an array of characters which is terminated by a null character. A string is a class which defines objects that can be represented as a set of characters. It is safer to use string rather than C-style string Fundamentally, you can consider std::string as a container for handling char arrays It has a self-increasing buffer behind where all elements can be stored That buffer is just a dynamic character array which is allocated and deallocated automatically Example The library getline() : This function is used to store a stream of characters as entered by the user in the object memory push_back() : This member function is used to input a character at the end of the string pop_back() : Introduced from C++11 (for strings), this member function is used to delete the last character from the string The library find() : Searches the string for the first occurrence of the sequence specified by its arguments. 1 5 12 26 Returns the position index. (See also rfind()) find_first_of() : Searches the string for the first character that matches ANY of the characters specified in its arguments. Returns the position index. (See also find_last_of(), find_first_not_of(), etc) All these return string::npos (-1) if no matches are found The library insert() : Inserts additional characters into the string right before the character indicated by pos (or p) This member function has more than 6 overloads depending on C++ version erase() : Erases part of the string, reducing its length This member function has 3 overloads substr() : Returns a newly constructed string object with its value initialized to a copy of a substring of this object Iterators Iterators are used to point at the memory addresses of STL containers They are primarily used in sequence of numbers, characters, etc They reduce the complexity and execution time of program begin() : This function is used to return the beginning position of the container end() : This function is used to return the end position of the container advance() : This function is used to increment the iterator position till the specified number mentioned in its arguments Example Example 2 The library Whenever a string is assigned to another string, a copy is created It is better to pass a string by reference during a function call String library provides all necessary overloaded functions for operators Example: >, >=,

Use Quizgecko on...
Browser
Browser