String class and STL. string class has the following constructors: string (const char *s) //initialize a string object with the string pointed by s string(size_type.

Slides:



Advertisements
Similar presentations
Hold data and provide access to it. Random-access containers: -Allow accessing any element by index -arrays, vectors Sequential containers: -Allow accessing.
Advertisements

Data Structures Using C++ 2E
. STL: C++ Standard Library (continued). STL Iterators u Iterators are allow to traverse sequences u Methods  operator*  operator->  operator++, and.
C++ Templates. What is a template? Templates are type-generic versions of functions and/or classes Template functions and template classes can be used.
CSE 332: C++ Algorithms I The C++ Algorithm Libraries A standard collection of generic algorithms –Applicable to various types and containers E.g., sorting.
C++ Sets and Multisets Set containers automatically sort their elements automatically. Multisets allow duplication of elements whereas sets do not. Usually,
Introduction to the STL
STL. What is STL? Standard Templates Library Templates are best used for –Defining containers for storing data –Some kinds of algorithms that work the.
Standard Containers: Vectors
COMP 171 Data Structures and Algorithms Tutorial 1 Template and STL.
Main Index Contents 11 Main Index Contents Container Types Container Types Sequence Containers Sequence Containers Associative Containers Associative Containers.
1 Standard Containers: Lists Gordon College Resource:
Main Index Contents 11 Main Index Contents Container Types Container Types Sequence Containers Sequence Containers Associative Containers Associative Containers.
OOP Etgar 2008 – Recitation 101 Object Oriented Programming Etgar 2008 Recitation 10.
CMSC 202 Lesson 24 Iterators and STL Containers. Warmup Write the class definition for the templated Bag class – A bag has: Random insertion Random removal.
Prof. amr Goneid, AUC1 CSCE 110 PROGRAMMING FUNDAMENTALS WITH C++ Prof. Amr Goneid AUC Part 11a. The Vector Class.
Lecture 11 Standard Template Library Stacks, Queue, and Deque Lists Iterators Sets Maps.
Templates and the STL.
Object Oriented Data Structures
Sorting and Vectors Mechanism for representing lists JPC and JWD © 2002 McGraw-Hill, Inc. Modified by S. Sudarshan.
Data Structures Using C++ 2E
1 Chapter 3 Lists, Stacks, and Queues Abstract Data Types, Vectors Sections 3.1, 3.2, 3.3, 3.4 Abstract Data Types (ADT) Iterators Implementation of Vector.
Containers Overview and Class Vector
11 COS220 Concepts of PLs AUBG, COS dept Lecture 36 OOP The STL Standard Template Library Reference: MS Developer Studio, Visual C++, Lafore, Chap 15 STL,
DATA STRUCTURES AND ALGORITHMS Lecture Notes 12 Prepared by İnanç TAHRALI.
Main Index Contents 11 Main Index Contents Week 3 – The Vector Container.
Generic Programming Using the C++ Standard Template Library.
STL multimap Container. STL multimaps multimaps are associative containers –Link a key to a value –AKA: Hashtables, Associative Arrays –A multimap allows.
STL List // constructing lists #include int main () { // constructors used in the same order as described above: std::list first; // empty list of ints.
Templates code reuse - inheritance - template classes template classes - a class that is not data-type specific - eg. a class of Array of any type - intArray,
1. The term STL stands for ? a) Simple Template Library b) Static Template Library c) Single Type Based Library d) Standard Template Library Answer : d.
CS Midterm Study Guide Fall General topics Definitions and rules Technical names of things Syntax of C++ constructs Meaning of C++ constructs.
Chapter 9: Part 2: Vectors + Maps and STL Overview JPC and JWD © 2002 McGraw-Hill, Inc. Modified by S. Sudarshan.
An Introduction to STL. The C++ Standard Template Libraries  In 1990, Alex Stepanov and Meng Lee of Hewlett Packard Laboratories extended C++ with a.
CSCE 121: Introduction to Program Design and Concepts, Honors J. Michael Moore Spring 2015 Set 19: The STL: Containers and Iterators 1.
CS212: Object Oriented Analysis and Design Lecture 24: Introduction to STL.
Lecture 8-3 : STL Algorithms. STL Algorithms The Standard Template Library not only contains container classes, but also algorithms that operate on sequence.
ICOM 4035 – Data Structures Dr. Manuel Rodríguez Martínez Electrical and Computer Engineering Department.
Lecture 11 Standard Template Library Lists Iterators Sets Maps.
Exceptions & Templates
Prof. amr Goneid, AUC1 CSCE 110 PROGRAMMING FUNDAMENTALS WITH C++ Prof. Amr Goneid AUC Part 15. Dictionaries (1): A Key Table Class.
1 Chapter 3 Lists, Stacks, and Queues Reading: Sections 3.1, 3.2, 3.3, 3.4 Abstract Data Types (ADT) Iterators Implementation of Vector.
Vectors CSci 588: Data Structures, Algorithms and Software Design Fall 2011 All material not from online sources copyright © Travis Desell, 2011
Glenn Stevenson CSIS 113A MSJC CSIS 123A Lecture 3 Vectors.
1 Circular, Doubly-Linked Lists Node Composition List Class Pushing and Popping Values Insert and Erase at Arbitrary Locations List Implementation.
Vectors. Basics A vector is like an array, but more flexible A vector provides (constant time) random access to its elements A vector may be dynamically.
Vectors Updated 11/4/2003 by Kip Irvine. Copyright Kip Irvine Overview What is a vector? Declaring vector objects Inserting and removing items Using.
Vectors the better arrays. Why Vectors l vectors are implemented as a class (a template to be exact) l they serve the same purpose as arrays but using.
14-1 Computing Fundamentals with C++ Object-Oriented Programming and Design, 2nd Edition Rick Mercer Franklin, Beedle & Associates, 1999 ISBN
CS212: Object Oriented Analysis and Design
Exceptions, Templates, and the Standard Template Library (STL)
Vectors Holds a set of elements, like an array
Chapter 8 Arrays, Strings and pointers
Standard Template Library (STL)
Starting Out with C++ Early Objects Eighth Edition
Collections Intro What is the STL? Templates, collections, & iterators
Object Oriented Programming COP3330 / CGS5409
C++ Functions, Classes, and Templates
Chapter 9 One-Dimensional Arrays
STL - Algorithms.
C++ File Structure and Intro to the STL
Elements are always copied when they are put into a container
Standard Version of Starting Out with C++, 4th Edition
Lists - I The List ADT.
Lists - I The List ADT.
Exceptions, Templates, and the Standard Template Library (STL)
C++ File Structure and Intro to the STL
the Standard Template Library
Collections Intro What is the STL? Templates, collections, & iterators
An Introduction to STL.
Presentation transcript:

String class and STL

string class has the following constructors: string (const char *s) //initialize a string object with the string pointed by s string(size_type n,char c) // create a string object with n element every of them char c string() creates a default string object of 0 size

string(const char *s, size_type n) // initialize n byte of string with s template string (Iter begin, Iter end) //initialize a string // object to the values in the range [begin, end), begin and end act like pointer.

#include int main() { string one(“this is test one”); cout<<one<<endl; string two (20,’$’); string three(one); cout<<three<<endl; one+=“add something”; two=“change two”; three[0]=‘P’; string four; four=two+three; cout<<four; char s[ ]=“the take some part of string”; string five(s,20); string six(s+6,s+10); cout<<six<<endl; string seven(&five[6], &five[10]); cout<<seven<<endl; }

char input[100]; cin>>input; cin.getline(input,100); cin.get(input,100); string s; cin>>s; getline(cin,s); //read a line, discard \n cin.getline(input,100,’:’); // read up to :, discard : getline(cin,stuff,’:’); //read up to :, discard :

int main() { ifstream infile; infile.open(“test.txt”); if( infile.is_open()==false) { cout<<“not open”; exit(1); } string item; int count; getline(infile,item, ‘:’ ); while(infile ){ ++count; cout<<count<<“:”<<item<<endl; getline(infile,item,’:’); } infile.close(); return 0; }

string s1(“this is”); string s2(“is this”); char s3[20]=“that is”; if( s1 < s2) … if(s1==s3) … if(s3 !=s2)

Standard Template Library STL provides a collection of templates such as containers, iterators and algorithms. A container is a unit like an array that can hold several values.

vector template class // vector is a container #include vector vector v(5); // a vector of 5 int int n; cin>>n; vector scores(n); for(int i=0; i<n;i++) cout<<scores[i];

vector title(5); We can get the size of the vector by size(); // number of elements swap() exchange the contents of two containers begin() // return an iterator that refer to the first element in a container, end() returns an iterator that refers to the last element.

Iterator is a generalization of a pointer, in fact it can be a pointer. It can be an object for which operations such as ++, -- has been defined. Each container class defines a iterator. vector :: iterator pd; // pd an iterator vector scores; pd =scores.begin(); *pd=22.3; ++pd;

push_back(); Add an element to the of a vector, and increase vector size. vector scores; double temp; while( cin >> temp && temp >=0) scores.push_back(temp);

erase() // remove a given range of a vector. scores.erase(scores.begin(), scores.begin()+2); insert() takes three arguments //first give the position of which new elements are to be inserted. vector old; vector new;

old.insert(old.begin(),new.begin()+1,new.end()); More methods for_each(), random_shuffle(), sort() For_each() // can be used with any container class. It takes three argumnet, the first two are iterators defining the range and the third one in a pointer to a function.

vector V:: iterator pr; for( pr =V.begin(); pr !=V.end(); pr++) show(*pr); for_each( V.begin(), V.end(), show); //the function must NOT alter the element of the vector

The randon_shuffle() function takes two iterators that specify a range and rearrange the element in that range in the random order. Random_shuffle(V.begin(), V.end()); vector s; sort(s.begin(), s.end()); // it sorts in non-decreasing order. If the container elements are user-defined objects, then we should have an opeator<() function.

struct R{ string title; int rate; }; bool operator< ( const R & r1, const R & r2) { if( r1.title < r2.title ) return ; else if( r1.title == r2.title && r1.rate < r2.rate) return true; else return false; } vector book; sort(book.begin(), book.end());