Presentation is loading. Please wait.

Presentation is loading. Please wait.

Lecture 2: A Tour of the Standard Library

Similar presentations


Presentation on theme: "Lecture 2: A Tour of the Standard Library"— Presentation transcript:

1 Lecture 2: A Tour of the Standard Library
Object Oriented Programming (C++) Lecture 2: A Tour of the Standard Library

2 Contents This chapter gives a quick tour of key library facilities
to give us an idea what can be done using C++ and its standard library. Useful library types, such as string, vector,list, and map, are presented as well as the most common ways of using them. The standard library facilities described in this book are part of every complete C++ implementation.

3 2.2 Hello, world! The minimal C++ program is: int main() {}
It defines a function called main. (1) Every C++ program must have a function named main() for a console program, or WinMain() for a window GUI program. (1) Every C++ program begins from the main() function or the WinMain() function.

4 The First C++ Program #include <iostream.h> int main() {
cout << "Hello, world!\n"; } The output is: Hello, world!

5 2.3 The Standard Library Namespace
The standard library is defined in a namespace (§2.4, §8.2) called std. #include<string> // make the standard string //facilities accessible using namespace std; // make std names available //without std:: prefix string s= "Ignorance is bliss!"; // ok: string is std::string

6 2.4 Output The output is: the value of i is 10 void h () { int i = 10;
cout << "the value of i is "; cout << i ; cout << ´\n´; } The output is: the value of i is 10

7 2.5 Strings The output is: Hello, world!
The standard library provides a string type. It provides a variety of useful string operations, such as concatenation. eg: string s1= "Hello"; string s2= "world"; void m1() { string s3 = s1+ ", " + s2+ "!\n"; cout << s3; } The output is: Hello, world!

8 String Operation(continue)
Comparation string incantation; void respond(const string & answer) { if (answer == incantation) {…} } The first argument is an index into the string (a position), the second argument is the length of the desired substring. Substrings manipulation string name= "Niels Stroustrup"; void m3() { string s = name.substr(6,10) ; // s = "Stroustrup" name.replace(0,5,"Nicholas") ; // name becomes //"Nicholas Stroustrup"}

9 C-Style Strings A C-style string is a zero-terminated array of characters (§5.2.2). The c_str() function extract the value of a string in the form of a C-style string. eg: void f () { string name=“this is a test”; printf ("name: %s\n",name.c_str()) ; } c-style function

10 2.6 Input The standard library offers istreams for input. istreams deal with character string representations of built-in types and can easily be extended to cope with user-defined types. void f () { int i; cin >> i ; //read an integer into i double d; cin >> d; // read a double-precision, floating-point // number into d }

11 2.7 Containers A class with the main purpose of holding objects is commonly called a container.(一个以保存一批对象为主要用途的类被称为一个容器) (1)Vector a built-in array has a fixed size. If we choose a large size, we waste space; if we choose a smaller size, the array will overflow. The standard library provides a vector (§16.3)to overcome this. struct Entry { string name; int number; }; Entry phone_ book[1000] ; void print_ entry(int i) //simple use { cout << phone_ book[i].name << ´ ´ << phone_ book[i].number << ´\n´;}

12 Containers(cont.) vector<Entry> phone_book(1000) ;
void print_ entry(int i) // simple use, exactly as for array { cout << phone_ book[i].name << ´ ´ << phone_ book[i].number << ´\n´; } void add_ entries(int n) / / increase size by n phone_ book.resize(phone_ book.size()+n) ; The vector member function size() gives the number of elements.

13 Containers(cont.) set, multiset, map, multimap (2)List
Eg: list<Entry> phone_book; (3)Map Eg: map<string,int> phone_book; (4)Standard Containers Including: vector, list, queue, stack, deque, priority_ queue, set, multiset, map, multimap

14 2.8 Algorithms data structures, to sort them, print them, extract subsets, remove elements, search for objects, etc. need algorithms. the standard library provides the most common algorithms for containers in addition to providing the most common container types.

15 2.9 Math A lot of numerical work is done in C++.
The standard library supports: a family of complex number types(复数) along the lines of the complex class described in §2.5.2. (2) Vector Arithmetic (3) Basic Numeric Support

16 2.10 Standard Library Facilities
The facilities provided by the standard library can be classified like this(标准库功能分类如下): [1] Basic run-time language support (e.g., for allocation and run- time type information); [2] The C standard library (with very minor modifications to minimize violations of the type system); [3] Strings and I/O streams (with support for international character sets and localization); [4] A framework of containers (such as vector, list, and map) and algorithms using containers (general traversals, sorts, merges); [5] Support for numerical computation (complex numbers plus vectors with arithmetic operations);

17 The End. Any Question?


Download ppt "Lecture 2: A Tour of the Standard Library"

Similar presentations


Ads by Google