{[ promptMessage ]}

Bookmark it

{[ promptMessage ]}

slides-3

# slides-3 - Data Structures CSCI 102 Housekeeping Names of...

This preview shows pages 1–10. Sign up to view the full content.

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"

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

View Full Document
2 Data Structures - CSCI 102 Copyright © William C. Cheng CS102 C++ Data Types & I/O Bill Cheng http://merlot.usc.edu/cs102-s11
Topics to cover 3 Data Structures - CSCI 102 Copyright © William C. Cheng C++ Data Types & I/O 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)

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

View Full Document
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;
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

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

View Full Document
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)
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/

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

View Full Document
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 }
C style strings are character arrays ( char[] ) See previous discussion of why we don’t like arrays Why do we need the string class?

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

View Full Document
This is the end of the preview. Sign up to access the rest of the document.

{[ snackBarMessage ]}

### Page1 / 35

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

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

View Full Document
Ask a homework question - tutors are online