Presentation is loading. Please wait.

Presentation is loading. Please wait.

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

Similar presentations


Presentation on theme: "Introduction to Data Structure, Spring 2007 Slide- 1 California State University, Fresno Introduction to Data Structure Standard Template Library Ming."— Presentation transcript:

1 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

2 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

3 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); }

4 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);

5 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;

6 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???

7 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);

8 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?

9 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

10 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)

11 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.

12 Introduction to Data Structure, Spring 2007 Slide- 12 California State University, Fresno Reference List of all functions of C++ Strings: http://www.cppreference.com/cppstring/index.html C-Style String functions: http://www.cplusplus.com/ref/cstring/


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

Similar presentations


Ads by Google