CS 144 Advanced C++ Programming February 7 Class Meeting

Slides:



Advertisements
Similar presentations
LECTURE 17 C++ Strings 18. 2Strings Creating String Objects 18 C-string C++ - string \0 Array of chars that is null terminated (‘\0’). Object.
Advertisements

Strings.
What is a pointer? First of all, it is a variable, just like other variables you studied So it has type, storage etc. Difference: it can only store the.
1 CS 105 Lecture 8 Strings; Input Failure Mon, Mar 7, 2011, 3:39 pm.
Copyright © 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Chapter 8 Strings and Vectors.
 2003 Prentice Hall, Inc. All rights reserved. 1 Arrays –Structures of related data items –Static entity (same size throughout program) A few types –Pointer-based.
Chapter 8 Arrays and Strings
CS 117 Spring 2002 Review for Exam 3 arrays strings files classes.
CMPE-013/L: “C” Programming Gabriel Hugh Elkaim – Spring 2013 CMPE-013/L Arrays and Strings Gabriel Hugh Elkaim Spring 2013.
ACS 168 Structured Programming Using the Computer Spring 2002 Joaquin Vila Prepared by Shirley White.
Chapter 8 Strings and Vectors (8.1 and 8.2). An Array of characters Defined as: char firstName[20]; char firstName[] = {‘T’, ‘i’, ‘m’}; // an array of.
CSIS 123A Lecture 6 Strings & Dynamic Memory. Introduction To The string Class Must include –Part of the std library You can declare an instance like.
CS Midterm Study Guide Fall General topics Definitions and rules Technical names of things Syntax of C++ constructs Meaning of C++ constructs.
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.
Copyright © 2008 Pearson Addison-Wesley. All rights reserved. Chapter 8 Strings and Vectors.
Copyright © 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Chapter 8 Strings and Vectors.
1 Cannon_Chapter9 Strings and the string Class. 2 Overview  Standards for Strings  String Declarations and Assignment  I/O with string Variables 
12/15/2015Engineering Problem Solving with C++, Second Edition, J. Ingber 1 Engineering Problem Solving with C++, Etter Chapter 6 One-Dimensional Arrays.
1 Character Strings (Cstrings) Reference: CS215 textbook pages
CSC 270 – Survey of Programming Languages
13. Strings. String Literals String literals are enclosed in double quotes: "Put a disk in drive A, then press any key to continue\n“ A string literal.
Chapter 8 Strings and Vectors. Slide 8- 2 Overview 8.1 An Array Type for Strings 8.2 The Standard string Class 8.3 Vectors.
Array. Array is a group of data of the same type. Array elements have a common name –The array as a whole is referenced through the common name Individual.
Strings. String Literals String literals are enclosed in double quotes: "Put a disk in drive A, then press any key to continue\n“ A string literal may.
Slide 1 Chapter 9 Strings. Slide 2 Learning Objectives  An Array Type for Strings  C-Strings  Character Manipulation Tools  Character I/O  get, put.
Strings. Using strings as abstract value A string is a sequence of characters e.g. “Hello, World!” Early version of C++ followed the older C language.
EC-111 Algorithms & Computing Lecture #10 Instructor: Jahan Zeb Department of Computer Engineering (DCE) College of E&ME NUST.
CMPE Data Structures and Algorithms in C++ September 9 Class Meeting Department of Computer Engineering San Jose State University Fall 2016 Instructor:
Lecture Overview Linked List quiz Finish last class' topic:
Introduction Programs which manipulate character data don’t usually just deal with single characters, but instead with collections of them (e.g. words,
Pointers and Dynamic Arrays
CS31 Discussion Jie(Jay) Wang Week6 Nov.4.
Chapter 8 Strings and Vectors
Computer Programming BCT 1113
CMPE Data Structures and Algorithms in C++ September 7 Class Meeting
Arrays in C.
CMPE 180A Data Structures and Algorithms in C++ February 15 Class Meeting Department of Computer Engineering San Jose State University Spring 2018 Instructor:
String in C++ A string is an array of characters which contains a non-printing null character ‘\0’ ( with ASCII value 0 ) marking its end. A string.
Object Oriented Programming COP3330 / CGS5409
Arrays and Strings Chapter 9.
C Stuff CS 2308.
7 Arrays.
Learning Objectives String Class.
Pointers, Dynamic Data, and Reference Types
Arrays Kingdom of Saudi Arabia
Pointers and Pointer-Based Strings
Week 9 – Lesson 1 Arrays – Character Strings
Chapter 9 One-Dimensional Arrays
10.1 Character Testing.
C-Strings, Strings, Vectors, STL
Strings and Streams Professor Hugh C. Lauer CS-2303, System Programming Concepts (Slides include materials from The C Programming Language, 2nd edition,
String What it is Why it’s useful
Chapter 9 Strings Copyright © 2008 Pearson Addison-Wesley. All rights reserved.
Engineering Problem Solving with C++, Etter
7 Arrays.
CS 144 Advanced C++ Programming February 5 Class Meeting
Arrays Arrays A few types Structures of related data items
COP 3330 Object-oriented Programming in C++
CS 144 Advanced C++ Programming February 12 Class Meeting
CS 144 Advanced C++ Programming February 21 Class Meeting
Today’s Objectives 28-Jun-2006 Announcements
CS-161 Computer Programming Lecture 15 & 16: Arrays II
CS31 Discussion 1H Fall18: week 6
CS 144 Advanced C++ Programming March 21 Class Meeting
Data Structure(s) A way of storing and organizing data in a computer so that it can be used efficiently. e.g. Arrays Linked Lists stacks Queues Trees.
CS 144 Advanced C++ Programming April 30 Class Meeting
8.3 Vectors Copyright © 2008 Pearson Addison-Wesley. All rights reserved. 1.
CS 144 Advanced C++ Programming April 30 Class Meeting
Dr. Khizar Hayat Associate Prof. of Computer Science
4.1 Introduction Arrays A few types Structures of related data items
Presentation transcript:

CS 144 Advanced C++ Programming February 7 Class Meeting Department of Computer Engineering San Jose State University Spring 2019 Instructor: Ron Mak www.cs.sjsu.edu/~mak

Character I/O Recall that the operator >> used on cin skips blanks. To read all characters from an input stream, including blanks, use the get function: Use the put function to output any character to an output stream: cout.put(ch); char ch; ... cin.get(ch);

Character I/O, cont’d You can use built-in function getline to read an entire input line, including blanks, into a string variable: string text; getline(cin, text);

Predefined Character Functions Some very useful Boolean functions that test a character: isupper(ch) islower(ch) isalpha(ch) isdigit(ch) isspace(ch) toupper(ch) tolower(ch)

The eof Function Boolean function eof (end of file) tests whether or not an input stream has read the entire file. Example: Function eof returns true only after an attempt was made to read past the end of the input file. It doesn’t warn you that you’re about to read past the end of the input file. If the function returns true, it’s telling you that you have already read past the end of the input file. if (in_stream.eof()) ...

Arrays An array variable can have multiple values. All values must be the same data type. Declare an array variable by indicating how many elements (values). Example: Use subscripts to access array elements. Subscript values for an array can range from 0 ... n-1 where n is equal to the number of elements in the array. int a[6];

Initialize an Array You can initialize an array when you declare it: If you initialize an array this way, you can leave off the array size. You can initialize the array with assignments: Or with a loop: int ages[] = {12, 9, 7, 2}; int ages[4]; ages[0] = 12; ages[1] = 9; ages[2] = 7; ages[3] = 2; int ages[4]; for (int i = 0; i < 4; i++) ages[i] = 0;

Array Function Parameters To pass an entire array to a function, indicate that a parameter is an array with []. Example: Also pass the array size separately. Arrays are implicitly passed by reference. Make the array parameter const to indicate that the function does not change the array. void sort(double a[], int size); double average(const double a[], int size);

Multidimensional Arrays A multidimensional array is an array of arrays. Example: A two-dimensional array: Each element of page is itself an array of 100 characters. Use multiple subscripts to access an element of a multidimensional array. Example: page[i][j] to access the jth character of the ith row. What is page[k]? char page[30][100];

Quiz! Look in Canvas under “Assignments” or “Quizzes” Quiz 1 – Feb 7 You will have 15 minutes to complete and submit the quiz.

C Strings Traditional C programs used arrays of characters to represent strings: A C string is always terminated by the null character \0. Therefore, the array size was one greater than the number of characters in the string. The greeting character array above has size 14. char greeting[] = "Hello, world!";

C Strings, cont’d You cannot assign a string value to a C string array variable: Illegal: Instead, you use the strcpy (“string copy”) function: Warning: Do not copy past the end of the destination string! greeting = "Good-bye!"; strcpy(greeting, "Good-bye!");

C Strings, cont’d To compare two C strings, use the strcmp (“string compare”) function: It returns: a negative value if str1 comes alphabetically before str2 zero if they contain the same characters a positive value if str1 comes alphabetically after str2. strcmp(str1, str2);

The Standard C++ string Class C++ programs use the standard string class: You can initialize string variables when you declare them: You can assign to string variables: #include <string> using namespace std; string noun, s1, s2, s3; string verb("go"); noun = "computer";

The Standard string Class, cont’d String concatenation: String comparisons with == != < <= > >= Lexicographic comparisons as expected. Strings automatically grow and shrink in size. A string keeps track of its own size. Use the member function at to safely access a character of a string: s1.at(i) s1[i] is dangerous if you go beyond the length. s1 = s2 + " and " + s3;

The Standard string Class, cont’d Many useful member functions : str.length() str.at(i) str.substr(position, length) str.insert(pos, str2) str.erase(pos, length) str.find(str1) str.find(str1, pos) str.find_first_of(str1, pos) str.find_first_not_of(str1, pos)

Vectors A vector is a kind of array whose length can dynamically grow and shrink. Vectors are part of the C++ Standard Template Library (STL). Like an array, a vector has a base type, and all its elements are of that type. Different declaration syntax from arrays: An array on steroids! vector<double> salaries; vector<bool> truth_table(10); vector<int> ages = {12, 9, 7, 2};

Vectors, cont’d Index into a vector like an array: ages[i] Use the at member function for safer access: v.at(i) Use with a standard for loop: Or with a ranged for loop: for (int i = 0; i < ages.size(); i++) { cout << ages[i] << endl; } for (int age : ages) { cout << age << endl; }

Vectors, cont’d Append new values to the end of a vector: Vector assignment: v1 = v2; Element-by-element assignment of values. The size of v1 can change to match the size of v2. salaries.push_back(100000.0); salaries.push_back(75000.0); salaries.push_back(150000.0); salaries.push_back(200000.0);

Vectors, cont’d Size of a vector: The current number of elements that the vector contains: v.size() Capacity of a vector: The number of elements for which memory is currently allocated: v.capacity() Change the size: Explicitly set the capacity: Bump up the capacity by 10: See: http://www.cplusplus.com/reference/vector/vector/ v.resize(24) v.reserve(32) v.reserve(v.size() + 10)