
Introduction to STL in C++ Programming
"Explore the key components of the Standard Template Library (STL) in C++, including containers, algorithms, and derived containers. Learn how to efficiently manipulate data and save time in program development with STL."
Download Presentation

Please find below an Image/Link to download the presentation.
The content on the website is provided AS IS for your information and personal use only. It may not be sold, licensed, or shared on other websites without obtaining consent from the author. If you encounter any issues during the download, it is possible that the publisher has removed the file from their server.
You are allowed to download the files provided on this website for personal or commercial use, subject to the condition that they are used lawfully. All files are the property of their respective owners.
The content on the website is provided AS IS for your information and personal use only. It may not be sold, licensed, or shared on other websites without obtaining consent from the author.
E N D
Presentation Transcript
GANESAR COLLEGE OF ARTS AND SCIENCE MELAISIVAPURI DEPARTMENT OF COMPUTER SCIENCE
PROGRAMMING IN C++ UNIT - V
Introduction STL stands for standard template library. Collection of these generic classes and functions is called STL. Large and complex Using STL can save considerable time and effort and lead to high quality programs namespace directive ex :- using namespace std;
Components of STL The STL has three components, Containers -hold data Algorithms -manipulating data Iterators - access data
CONTAINERS A Container is an object that actually stores data It is a way data is organized in memory Hold the difference types of data Container has grouped into three categories: Sequence containers store elements in a linear sequence three types of sequence containers, - vector - list - Dequeue
Associative containers It was designed to support direct access to elements using keys. Not sequential Four types of Associative containers -set -multiset -map -multimap
Derived containers STL has provide three types of derived containers -stack -queue -priority _queue Two member fuctions are used pop() and push() for implementing deleting inserting operations.
Algorithms It is a procedure Used to process the data contained in the containers. Such as initializing, searching, copying, sorting, and merging. Using algorithms, programmers can save a lot of time and effort
Categorized the algorithms Retrieve or nonmutating algorithms Mutating algorithms Set algorithms Relational algorithms Sorting algorithms
Iterators Access the container elements Traverse from one element to another A process known as iterating through the container Iterators function: Input iterators -simplest and is used mostly in single-pass algorithms.
output iterators -same as input iterators but not used for traversing. Forward iterators -can be used only in forward direction, one step at a time. Bidirectional iterators -these iterators can move in both directions. Random access itertors -same as pointers . Can be used to any element randomly.
Application of container classes Three types of containers are supported in the STL, -Vectors change its size dynamically supports random access Number of constructors for creaating Vector oobjects, vector<int> v1; //zero-length int vector vector<double> v2; //10-element double vector
-lists It is a another container is popularly used List can be accessed sequentially only Bidirectional iterators are used for accessing list elements Any algorithm that requires input, output, forward, or bidirectional iterators can operate on a list The objects of list can be initialized with other list objects, list A = list1; list B =list2; The statement, list1.merge(list2);
-Maps A map is a sequence of (key , value) pairs Where a single value is associated with each unique key Retrieval of values is based on the key and is very fast Key 1 Value 1 : : Value 2 Key 2 Key N Value N
Introduction A string is a sequence of character C++ does not support a built in string type The strings are called C-strings or C-style strings. ANSI standard c++ now provides a new class called string . The string class is very large and includes many constructors, member functions, and operators. Follow,
Creating string objects Reading string object from keyboard Displaying string objects to the screen Finding a substring from a string Modifying string objects Comparing string objects Adding string objects Accessing characters in a string Obtaining the size of strings Many other operations
Stringconstructor String(); -for creating an empty string String(const chat*str); -for creating a string object from a null-terminated string String(const string & str); -for creating a string object from sother string object Important function of string class assign() assigns a partial string Begin() returns a reference to the start of a string End() returns a reference to the end of a string Erase() removes characters as specified Insert() inserts characters at as specified location
Operators for string objects Operator meaning = assignment + concatenation += concatenation assignment == equality != inequality < less than <= less than or equal << output >> input
Creating (string) objects We can creating string objects in a number of ways as illustrated below : String s1; // using constructor with no argument String s2 ( xyz ) ; // using one argument constructor S1 =s2 ; // assigning string objects G3 = abc + s2 // concatenating strings Cin >> g1 ; // reading through keyboard (one word) Getline (cin, s1); // reading through keyboard a line of text Overloaded +operator concatenates two string objects . Examples : s3 += s1; //s3 = s3 + s1 S3 += abc ; // s3 =s3 + abc
MANIPULATING STRING OBJECTS We can modify contents of string objects in several ways, using the member functionssuch as insert(),replace(),erase(), and append(). Example program : #include <iostream.h> #include <string> using namespace std; Void main() { String s1( 1234 ); String s2( abcd );
cout << original strings are: \n cout << S1: << s1 << \n ; cout << S2: << s2 << \n\n ; // inserting a string in to another cout << place S2 inside s1 \n ; s1.insert(3,s2); Cout << modified S1: << s1 << \n\n ; // removing characters in a string Cout << remove 4 characters from S2 \n ; s1.erase(3,4); cout << Now S1 : << \n\n ;
getch(); } Output: Original strings S1: 1234 S2: abcd Place S2 inside S1 Modified S1: 123abcd4 Remove 4 characters from S1 Now s1 : 1234
Relational operations A number of operators that can be used on strings are defined for string objects Examples the operator =and + for creating objects Compare() function can also be used for this strings String characteristics Characteristics of strings such as size , length , capacity , etc. The size or length denotes the number of elements currently stored in a given strings. Another characteristic is the maximum size which is the largest possible size of a string object.
comparing and swapping The string supports two functions Comparing - used for to compare either two strings or portions of two strings Swapping - used for swapping the contents of two string objects.
OBJECT ORIENTED SYSTEM DEVELOPMENT
Introduction Software engineers have been trying various tools, methods, and procedures to control the process of software development in order to build high quality software with improved productivity. The methods provide how to s for building the software while the tools provide automated or semi-automated support for the methods. They are used in all the stages of software development process, namely, planning, analysis, design, development and maintenance.
Software development components Software development Procedures Methods Tools
A successful system must: 1. satisfy the user requirements, 2. be easy to understand by the users and operators, 3. be easy to operate, 4. have a good user interface, 5. be easy to modify, 6. be expandable, 7. have adequate security controls against misuse of data, 8. handle the errors and exceptions satisfactorily, and
Procedure-Oriented Paradigms Software development is usually characterized by a series of stages depicting the various asks involved in the development process. Problem Definition: -This activity requires a precise definition of the problem in user terms. -A clear statement of the problem is crucial to the success of the software. -It helps not only the development but also the user to understand the problem better.
Analysis: -This covers a detailed study of the requirements of both the user and the software. Design: -The design phase deals with various concepts of system design such as data structure, software architecture, and algorithms. -This phase translates the requirements into a representation of the software. Coding: -coding refers to the translation of the design into machine-readable form. -its reliability.
Testing: -once the code is written, it should be tested rigorously for correctness of the code and results.. -It requires a detailed plan as to what, when and how to test. Maintenance: -This may occur due to a change in the user s requirement, a change in the operating environment, or an error in the software that has been fixed during the testing.
Procedure-Oriented Development Tools System flowcharts: A graphical representation of the important inputs, outputs, and data flow. Program flowcharts: A graphical representation of program logic. Play script: A narrative description of executing Layout forms: A format designed for putting the input data or displaying results.
Data flow diagrams: The flow of data between various components of a system. Decision table: A table of configurations for defining a problem and the actions to be taken. Grid charts: A chart showing the relationship between different modules of a system.
Object-Oriented Paradigm A system can be viewed as a collection of entities that interact together to accomplish certain objectives. Entities may represent physical objects such as equipment and people, and abstract concepts such as data files and functions. In object oriented analysis, the entities are called objects.
Object oriented software development functions Object-oriented analysis (OOA) refers to the methods of specifying requirements of the software in the terms of real-world objects, their behavior, and their interactions. Object-oriented design (OOD), on the other hand, turns the software requirements into specifications for objects and derives class hierarchies from which the objects can be created. object-oriented programming (OOP) refers to the implementation of the
Object-Oriented Notation and Graphs Graphical notations are an essential part of any design and development process, and object- oriented design is no exception. We need notations to represent classes, objects, subclasses, and their inter-relationships
some of the commonly used notations to represent the following: 1. Classes and objects. 2. Instances of objects. 3. Message communication between and objects. 4. Inheritance relationship. 5. Classification relationship. 6. Composition relationship. 7. Hierarchical chart. 8. Client-Server relationship. 9. Process layering.
Inheritance relationship Base class A Derived class B
Client server relationship X Y client Server
ObjectOriented Analysis Object-oriented analysis provides us with simple, yet powerful, mechanism for identifying objects. The object-oriented analysis (OOA) approach consists of the following steps: 1. Understanding the problem. 2. Drawing the specification of requirement of the user and the software. 3. Identifying the objects and their attributes. 4. Identifying the services that each object is expected to provide (interface). 5. Establishing inter-connections (collaborations) between the objects.
Object-Oriented Design Design is concerned with a mapping of objects in the problem space into objects in the solution space, and creating an overall structure and computational models of the system. The object oriented design (OOD) approach may involve the following steps:
1.Review of objects created in the analysis phase. 2. Specification of class dependencies. 3. Organization of class hierarchies. 4. Design of classes. 5. Design of member functions. 6. Design of driver program.