Introduction to Data Structure, Spring 2007 Slide- 1 California State University, Fresno Introduction to Data Structure Standard Template Library Ming.

Slides:



Advertisements
Similar presentations
Copyright © 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Slide 7- 1 Overview 7.1 Introduction to Arrays 7.2 Arrays in Functions 7.3.
Advertisements

 2000 Deitel & Associates, Inc. All rights reserved. Chapter 19 - Class string and Stream Processing Outline 19.1Introduction 19.2 string Assignment and.
14 Templates. OBJECTIVES In this chapter you will learn:  To use function templates to conveniently create a group of related (overloaded) functions.
Introduction to Data Structure, Spring 2007 Slide- 1 California State University, Fresno Introduction to Data Structure Introduction of Concepts Ming Li.
計算機概論實習 How to Use string Template class basic_string String manipulation (copying, searching, etc.) typedef basic_string string; Also typedef.
CS Oct 2006 Chap 6. Functions General form; type Name ( parameters ) { … return value ; }
Introduction to Data Structure, Spring 2007 Slide- 1 California State University, Fresno Introduction to Data Structure C Programming Concepts Ming Li.
General Computer Science for Engineers CISC 106 Lecture 34 Dr. John Cavazos Computer and Information Sciences 05/13/2009.
Introduction to Data Structure, Fall 2006 Slide- 1 California State University, Fresno Introduction to Data Structure Chapter 7 Ming Li Department of.
Main Index Contents 11 Main Index Contents Selection Sort Selection SortSelection Sort Selection Sort (3 slides) Selection Sort Alg. Selection Sort Alg.Selection.
Programming Pointers. COMP104 Lecture 32 / Slide 2 Pointers l Pointers are objects whose values are the locations of other objects l Pointers are memory.
Introduction to Data Structure, Spring 2007 Slide- 1 California State University, Fresno Introduction to Data Structure Sort, Search, and Running Time.
Introduction to Data Structure, Fall 2006 Slide- 1 California State University, Fresno Introduction to Data Structure Chapter 8 Ming Li Department of.
Main Index Contents 11 Main Index Contents Lecture 4 CSN 331 – Fall 2004 Chapter 15 Fibonacci (again) Dynamic Programming Permutations Eight Queens BacktrackingSummary.
Lecture 11 Standard Template Library Stacks, Queue, and Deque Lists Iterators Sets Maps.
Templates & Generic Programming Junaed Sattar November 12, 2008 Lecture 10.
Containers Overview and Class Vector
One Dimensional Arrays (Part2) Sorting Algorithms Searching Algorithms Character Strings The string Class. 1.
1 Sorting Algorithms (Basic) Search Algorithms BinaryInterpolation Big-O Notation Complexity Sorting, Searching, Recursion Intro to Algorithms Selection.
 2006 Pearson Education, Inc. All rights reserved Arrays.
1 Liang, Introduction to C++ Programming, (c) 2007 Pearson Education, Inc. All rights reserved X 1 Chapter 9 Classes & Objects type string is.
Chapter 6 One-Dimensional Arrays ELEC 206 Computer Tools for Electrical Engineering.
ECE 264 Object-Oriented Software Development Instructor: Dr. Honggang Wang Fall 2012 Lecture 26: Exam 2 Preview.
Lecture 5: Part 2 Classes and Data Abstraction. Objects Models of things in the real world Defined in classes  Class name is the object name Example:
© 2005 Pearson Education, Inc., Upper Saddle River, NJ. All rights reserved. Data Structures for Java William H. Ford William R. Topp Chapter 5 Generic.
Chapter 13 – C++ String Class. String objects u Do not need to specify size of string object –C++ keeps track of size of text –C++ expands memory region.
Chapter 7 Templates. Objectives Introduction Function Templates Class Templates Standard Template Library.
CS31: Introduction to Computer Science I Discussion 1A 4/16/2010 Sungwon Yang
 2003 Prentice Hall, Inc. All rights reserved.m ECE 2552 Dr. Këpuska based on Dr. S. Kozaitis Summer Chapter 15 - Class string and String Stream.
Lecture 11 Standard Template Library Lists Iterators Sets Maps.
Introduction to Analysis of Algorithms CS342 S2004.
CMSC 202 Lesson 2 C++ Primer. Warmup Create an array called ‘data’ Define a constant called DATA_SIZE with value 127 Write a loop to fill the array with.
String Class. C-style and C++ string Classes C-style strings, called C-strings, consist of characters stored in an array ( we’ll look at them later) C++
12/15/2015Engineering Problem Solving with C++, Second Edition, J. Ingber 1 Engineering Problem Solving with C++, Etter Chapter 6 One-Dimensional Arrays.
C++ String Class nalhareqi©2012. string u The string is any sequence of characters u To use strings, you need to include the header u The string is one.
 2003 Prentice Hall, Inc. All rights reserved. 1 Chapter 15 - Class string and String Stream Processing Outline 15.1 Introduction 15.2 string Assignment.
String Class Mohamed Shehata 1020: Introduction to Programming.
SMIE-121 Software Design II School of Mobile Information Engineering, Sun Yat-sen University Lecture.
 2008 Pearson Education, Inc. All rights reserved. 1 Arrays and Vectors.
Arrays, Vectors, and Strings Allocation and referencing.
Chapter 11 Standard C++ Strings and File I/O Dept of Computer Engineering Khon Kaen University.
LECTURE 6 cs201 C++ Strings.
STL string class Programming Team 2/15/2006. string constructors string() = empty string string(int l, char ch) = string of length l, of repeated ch string(char*
Strings Jarret Raim C Strings Same as arrays of characters. Use pointers. Require static declarations (compile time). No bounds checking. No easy.
Prof. amr Goneid, AUC1 CSCE 110 PROGRAMMING FUNDAMENTALS WITH C++ Prof. Amr Goneid AUC Part 15. Dictionaries (1): A Key Table Class.
Computer Programming 2 Lab (1) I.Fatimah Alzahrani.
ECE 264 Object-Oriented Software Development Instructor: Dr. Honggang Wang Fall 2012 Lecture 20: Container classes; strings.
Glenn Stevenson CSIS 113A MSJC CSIS 123A Lecture 3 Vectors.
Introduction to Data Structure, Fall 2006 Slide- 1 California State University, Fresno Introduction to Data Structure Chapter 4 Ming Li Department of Computer.
13. string Operations Intro Programming in C++ Computer Science Dept Va Tech August, 2002 © Barnette ND & McQuain WD 1 Accessing String Elements.
Lecture 17: 4/4/2003CS148 Spring CS148 Introduction to Programming II Ayman Abdel-Hamid Department of Computer Science Old Dominion University Lecture.
Main Index Contents 11 Main Index Contents Stacks Further Stack Examples Further Stack Examples Pushing/Popping a Stack Pushing/Popping a Stack Class StackClass.
Templates יום רביעי 08 יוני 2016 יום רביעי 08 יוני 2016 יום רביעי 08 יוני 2016 יום רביעי 08 יוני 2016 יום רביעי 08 יוני 2016 יום רביעי 08 יוני 2016 יום.
Strings.
CS Computer Science IA: Procedural Programming
Engineering 1020: Introduction to Programming Fall 2018
Lists in Python.
מחרוזות-String בשפת C++ ישנו תפקיד מיוחד למערך מסוג char רצף של תווים הנמצאים במערך מסוג char המסתיימת בתו אפס (הכוונה לאפס ממש '0\' , ולא לתו '0')
Programming with ANSI C ++
String class and its objects
Chapter 9 Objects and Classes
Engineering Problem Solving with C++, Etter
7 Arrays.
CS150 Introduction to Computer Science 1
Introduction to Computer Science
Podcast Ch22a Title: Array-based Binary Trees
Today’s Objectives 28-Jun-2006 Announcements
Chapter 15 - Class string and String Stream Processing
character manipulation
Presentation transcript:

Introduction to Data Structure, Spring 2007 Slide- 1 California State University, Fresno Introduction to Data Structure Standard Template Library Ming Li Department of Computer Science California State University, Fresno Spring 2006

Introduction to Data Structure, Spring 2007 Slide- 2 California State University, Fresno Template Syntax Template function syntax includes the keyword template followed by a non-empty list of formal types enclosed in angle brackets. In the argument list, each type is preceded by the keyword typename, and types are separated by commas. Format: template

Introduction to Data Structure, Spring 2007 Slide- 3 California State University, Fresno Template Syntax - Example template T GetMax (T a, T b) { T result; result = (a>b)? a : b; return (result); } template T GetMin (T a, U b) { T result; result = (a>b)? a : b; return (result); }

Introduction to Data Structure, Spring 2007 Slide- 4 California State University, Fresno Template Syntax - Example int main () { int i=5, j=6, k; long l=10, m=5, n; k=GetMax (i,j); n=GetMax (l,m); cout << k << endl; cout << n << endl; return 0; } k=GetMax (i,l);

Introduction to Data Structure, Spring 2007 Slide- 5 California State University, Fresno Template Syntax - Example template void selectionSort(T arr[], int n) { int smallIndex; // index of smallest element in the sublist int pass, j; T temp;

Introduction to Data Structure, Spring 2007 Slide- 6 California State University, Fresno Template Syntax - Example for (pass = 0; pass < n-1; pass++) { smallIndex = pass; for (j = pass+1; j < n; j++) if (arr[j] < arr[smallIndex]) smallIndex = j; if (smallIndex != pass) { temp = arr[pass]; arr[pass] = arr[smallIndex]; arr[smallIndex] = temp; } } } How to use this template???

Introduction to Data Structure, Spring 2007 Slide- 7 California State University, Fresno String Operations (STL) Declaration: string s1; string s2(“a string”); string s3 = “initial value”; string s4(s3); Can specify size of the string string s5 ("small value", 100);

Introduction to Data Structure, Spring 2007 Slide- 8 California State University, Fresno String Operations (STL) Getting size and capacity s3.size() – returns number of characters s3.length() – returns number of characters s3.capacity() – returns the capacity of the string Resetting size and capacity s3.resize(50) – change the size to 50 s3.reserve(200) – change the capacity to 200 Checking the size s3.empty() – Is s3 empty?

Introduction to Data Structure, Spring 2007 Slide- 9 California State University, Fresno String Operations (STL) Access s.c_str(); - return C_style string s.substr(start, len); – returns a substring c = s[i]; - return an element c = s.at(i); - return an element Comparison if(s1 == s2) - return true if s1 equals s2 i = s.compare(s2) – return 0 if s>s1 i = s.compare(start, len, s1, start1, len1) compare two different strings

Introduction to Data Structure, Spring 2007 Slide- 10 California State University, Fresno String Operations (STL) Altering s[i] = c; s.at(i) = c; s.append(s1); - concatenate s1 to s s.assign(s1, start, len) – change content of s s.clear() – empty s s.erase(50,14) – erase 14 characters after the 50 th character s.insert(10, s1) – insert s1 to s at index 10 s.replace(i1,n1,s1,i2,n2)

Introduction to Data Structure, Spring 2007 Slide- 11 California State University, Fresno String Operations (STL) Search i = s.find(c); Position of leftmost occurrence of char c. i = s.find(s1); Position of leftmost occurrence of s1. i = s.rfind(s1); As find, but right to left. i = s.find_first_of(s1); Position of first char in s which is in s1 set of chars. i = s.find_first_not_of(s1); Position of first char of s not in s1 set of chars. i = s.find_last_of(s1); Position of last char of s in s1 set of chars. i = s.find_last_not_of(s1); Position of last char of s not in s1 set of chars.

Introduction to Data Structure, Spring 2007 Slide- 12 California State University, Fresno Reference List of all functions of C++ Strings: C-Style String functions: