03a_cpp_types

03a_cpp_types - Data Structures - CSCI 102 CS102 C+ Data...

Info iconThis preview shows pages 1–9. Sign up to view the full content.

View Full Document Right Arrow Icon
1 Data Structures - CSCI 102 Copyright © William C. Cheng CS102 Bill Cheng http://merlot.usc.edu/cs102-s11
Background image of page 1

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon
Topics to cover 2 Data Structures - CSCI 102 Copyright © William C. Cheng Data Types Arrays (review Ch 9) Vectors (Ch 22) Strings (Ch 8) I/O Streams I/O (Ch 3) Console I/O I/O Manipulators File I/O String Stream Enumerations (Ch 8)
Background image of page 2
Keeping collections of many pieces of the same data type What are arrays good for? 3 Data Structures - CSCI 102 Copyright © William C. Cheng C++ Arrays e.g. I want to store 100 integers: int n[100]; Each value is called out explicitly by its index (indexes start at 0!): Read an array value: cout << "5th value = " << n[4] << endl; Write an array value n[2] = 255;
Background image of page 3

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon
Arrays need a contiguous block of memory You must pass the size around with the array Unfortunately C++ arrays can be tricky. .. 4 Data Structures - CSCI 102 Copyright © William C. Cheng C++ Arrays Arrays are difficult/costly to resize Arrays don’t know their own size Arrays don’t do bounds checking Potential for buffer overflow security holes e.g. http://wiibrew.org/wiki/Twilight_Hack Arrays are not automatically initialized You have to decay them to pointers Arrays can’t be directly returned from a function
Background image of page 4
Arrays are a fixed size. Resizing is a pain. This compiles: Why do we need the vector class? int stuff[5]; cout << stuff[-1] << " and " << stuff[100]; 5 Data Structures - CSCI 102 Copyright © William C. Cheng C++ Vectors Arrays don’t know their size (no bounds checking) Automatic resizing to fit data How can vectors help? Sanity checking on bounds They do everything arrays can do, but more safely (sometimes at the cost of performance) It behaves more like a queue (as someone leaves a queue, everyone else shuffles forward) Does a vector behave like an array?
Background image of page 5

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon
You almost always want to pass vectors by reference Otherwise the whole thing gets copied Usage notes on vectors 6 Data Structures - CSCI 102 Copyright © William C. Cheng C++ Vectors Vector insertion using push_back() makes a copy of the element you’re inserting Using [ ] or at( ) gets a reference to the element in the vector (return values modify the original vector) In our course, vector is the only thing you are allowed to use from the STL in your code vector is in the C++ Standard Template Library (STL) See http://www.cplusplus.com/reference/stl/
Background image of page 6
7 Data Structures - CSCI 102 Copyright © William C. Cheng C++ Vectors #include <iostream> #include <vector> using namespace std; int main() { vector<int> myNumbers; //put some values in the vector for(int i=0; i < 5; i++) { myNumbers.push_back(i); } cout << "The vector has " << myNumbers.size() << " elements." << endl; cout << "The value at index 0 is " << myNumbers.at(0) << endl; myNumbers.clear(); //empty out the vector }
Background image of page 7

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon
C style strings are character arrays ( char[] ) See previous discussion of why we don’t like arrays Why do we need the string class? 8
Background image of page 8
Image of page 9
This is the end of the preview. Sign up to access the rest of the document.

This note was uploaded on 02/17/2011 for the course CSI 102 taught by Professor Billcheng during the Spring '11 term at USC.

Page1 / 49

03a_cpp_types - Data Structures - CSCI 102 CS102 C+ Data...

This preview shows document pages 1 - 9. Sign up to view the full document.

View Full Document Right Arrow Icon
Ask a homework question - tutors are online