CS138 Lecture Notes - Lecture 6: Type Safety, Bounds Checking, Call Stack

22 views3 pages
Data Lecture 6
January 24, 2013
Vector Review
Vector is a CONTAINER data structure from C++ STD
o Like a C-style array except…
Generic and type-safe
Index bounds checking is possible
Can be resized on-the-fly
Either one element at a time or in chunks
o Other STL data structures:
Deque
List
Map
Set
Iterating through a vector
o Simple, normal, numerical approach
o Abstract, powerful but confusing way w/ STL iterators
Useful vector API
o v.at(i)
o v[i]
o v.size()
o v.capacity()
o v.empty()
o v.resize(int)
o v.reserve(int)
o v.push_back(..)
o v.pop_back(..)
o v.front()
o v.back()
o v.begin()
o v.end()
int main (...) {
vector <string> v;
v.push_back ("Reimer");
v.push_back ("Scrivens");
v.push_back ("Luongo");
for (int i=0;i<v.size();i++) {
cout<<v.at(i)<<endl;
}
v.pop_back();
v.pop_back();
string s=v.back();
cout<<s<<endl;
v.push_back("Palmateer");
for (vector<string>::const_iterator i=v.begin(); i!=v.end();i++) {
cout<<(*i)<<endl;
}
}
Unlock document

This preview shows page 1 of the document.
Unlock all 3 pages and 3 million more documents.

Already have an account? Log in

Document Summary

Vector is a container data structure from c++ std: like a c-style array except . Either one element at a time or in chunks: other stl data structures: Iterating through a vector: simple, normal, numerical approach, abstract, powerful but confusing way w/ stl iterators. C/c++/java/python memory model: in c/c++, program is binary. Compiled from source code: rte may be bundled into binary (statically linked libs) or not (dynamically linked libs, in java/c#, we compile source to byte code . A virtual machine (jvm/clr) acts as runtime/abstract os: in python/perl/scheme/racket/lisp, run time is interpreter that runs raw source code. Creating struct/class instances in c+: direct (static) instantiation. However, this can be a problem if we want to create linked structures that live beyond current procedure: dynamic instantiation (usually thru a ptr) Must remember to delete storage when you are done. C++ pointers: are basically a strange number with strange arithmetic, can point to just about any kind of thing.

Get access

Grade+
$10 USD/m
Billed $120 USD annually
Homework Help
Class Notes
Textbook Notes
40 Verified Answers
Study Guides
Booster Classes
Class+
$8 USD/m
Billed $96 USD annually
Homework Help
Class Notes
Textbook Notes
30 Verified Answers
Study Guides
Booster Classes