slides-3

slides-3 - Data Structures - CSCI 102 Housekeeping...

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 Housekeeping (1/20/2011) Lab #2 (Streams) posted HW1 (Video Game Database) posted Blinky YouTube video Due by 11:45pm on Mon, 1/31/2011 To be done next week during your lab session Names of TA responsible for labs are posted See class web page Questions? Class video link -> Favorites -> "Blinky - Pointer Fun"
Background image of page 1

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

View Full DocumentRight Arrow Icon
2 Data Structures - CSCI 102 Copyright © William C. Cheng CS102 Bill Cheng http://merlot.usc.edu/cs102-s11
Background image of page 2
Topics to cover 3 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 3

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

View Full DocumentRight Arrow Icon
Keeping collections of many pieces of the same data type What are arrays good for? 4 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 4
Arrays need a contiguous block of memory You must pass the size around with the array Unfortunately C++ arrays can be tricky. .. 5 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 5

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

View Full DocumentRight Arrow Icon
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]; 6 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)
Background image of page 6
You almost always want to pass vectors by reference Otherwise the whole thing gets copied Usage notes on vectors 7 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 7

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

View Full DocumentRight Arrow Icon
8 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 8
Image of page 9
This is the end of the preview. Sign up to access the rest of the document.

Page1 / 35

slides-3 - Data Structures - CSCI 102 Housekeeping...

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