CSCE 121 Chapter 4
Jump to navigation
Jump to search
« previous | Friday, September 3, 2010 | next »
Objects, types, and values
complex<int> = z(1, 2); // ordered pairs
Type Safety
- object will only be used according to its type
- static type safety checks types at compilation
- dynamic type safety checks types at runtime
C++ is not completely statically/dynamically type-safe
EX:
int main() {
int a = 20000;
char c = a; // squeezing a big int into a 1 byte value
int b = c; // getting the int value of c
return 0;
}
Auto-initialization is "random" — always assign to initial value
Computation
- Abstractions: we know that it works, and that's all we want to know
- Algorithms work well, and we know why they work well (we can prove it)
- A logical arithmetical or computational procedure that, if correctly applied, ensures the solution of a problem
- Heuristics work well in practice, but we don't know why
Vectors
Code:
vector<int> nums;
nums.push_back(1);
// OR
vector<char> alphabet(26);
// OR
vector<bool> lights(3, true);
Monday, September 6, 2010
cin can be read into vector:
vector<double> temps;
double temp;
while (cin >> temp) temps.push_back(temp);
Built-in functions
- size()
- The number of elements in the vector
- begin()
- The first item of a vector
- end()
- The last item of a vector
- begin() and end() are good for sorting:
sort(v.begin(), v.end())
- push_back(item)
- Add item to the end of an array.