Download presentation
Presentation is loading. Please wait.
1
Compound Data CSCE 121 J. Michael Moore
2
Large Sets of Data What to do?
3
Datatypes Simple Compound int, double, bool, etc. Homogeneous Heterogeneous Array Vector ?
4
Group of homogeneous elements accessed by an index.
Array/Vector Group of homogeneous elements accessed by an index. Vector access with ary.at(index) Bounds checking Array and Vector access with ary[index] No bounds checking
5
Array Really a pointer Variable name is a pointer to the first element
For ary[2] Conceptually: address of ary + 2*sizeof(array datatype) Actually: ary + 2 (math is done automatically) Pointer arithmetic Must know size of array at compile time Allows access to elements outside of array bounds Security problem
6
Vector Preferred over arrays Bounds checking
We’ll use vectors instead of arrays for now. Bounds checking Prevents access outside array bounds Resizable #include <vector>
7
Vector Must say what type it is e.g. vector<int>
Define size at beginning vector<int> vec = vector<int>(SIZE); vector<int> vec = vector<int>(SIZE,INIT_VAL); Let vector grow as needed vector<int> vec; vec.push_back(7); Increases size by one
8
Vector vector<int> vec; vec.push_back(7);
vec.erase(vec.begin()+index); vec.at(index); // range checking & harder to read vec[index]; // no range checking & easier to read
9
Parallel Vectors Book calls them multiple vectors.
Conceptually a set of items with multiple types of information People Name Age Weight Separate vectors for name, age and weight. Each index maps to the same person. Better way is to use a heterogeneous datatype (e.g. classes)
10
Multi-dimensional Vectors
Matrix calculations Multi-dimensional data A vector of vectors vector< vector<int> > ary2d; Access ary2d.at(i).at(j) // safer ary2d[i][j] More later
Similar presentations
© 2025 SlidePlayer.com. Inc.
All rights reserved.