Presentation is loading. Please wait.

Presentation is loading. Please wait.

Computer programming 1 Multidimensional Arrays, and STL containers: vectors and maps.

Similar presentations


Presentation on theme: "Computer programming 1 Multidimensional Arrays, and STL containers: vectors and maps."— Presentation transcript:

1 Computer programming 1 Multidimensional Arrays, and STL containers: vectors and maps

2 Computer programming 2 Objectives Learn how to declare – multidimensional arrays – Vectors – Maps Illustrate how and when to use – multidimensional arrays – Vectors – Maps

3 Computer programming 3 Motivations Write a program that, given two cities, will tell us the distance between the two cities How to represent an image? How to represent a matrix? How to represent a two or a three dimensional space? Use multidimensional arrays

4 Computer programming 4 Multidimensional arrays Declaration typeName name[SIZE_1][SIZE_2]…[SIZE_n] Examples double distances[5][5]; double surface[100][100]; int threeDimension[10][10][10]; Initialization double distances[5][5]={{0,1,2,3,4,5},{2,3}}; double myArray[][2]={{2,3},{4,5}};//a 2x2 array

5 Computer programming 5 Multidimensional arrays Initialize and reference/access a multidimensional array using for loops – nested for loops Outer loop counting through the first dimension The next Inner loop counting through the second dimension And so on. double surface[10][10]; //initialize to 0 for (int i=0; i<10; i++){ cout <<endl; for (int j=0; j<10; j++){ surface[i][j]=1/(i+j+1.); cout << ++surface[i][j]<< “”; }

6 Computer programming 6 Visualizing 2D arrays A table with columns (vertical) and rows (horizontal) const int distances[6][6]

7 Computer programming 7 STL containers and algorithms STL containers – A collection of data structures that can hold any kind of data Vectors Maps sets Queues Lists – They are classes. So, several methods are already defined on them sort find STL algorithms – A collection of algorithms that can be used to manipulate STL containers. – E.g. copy, sort, find, min, max, etc.

8 Computer programming 8 STL containers: vectors Arrays that we dealt with so far have a size that must be known before the compiling time Arrays do not check whether the index is out of bounds When we implement a function we must pass – The name of the array – The bounds on each dimension To manipulate arrays, you need to implement the functions to do so Arrays are not classes; they are not self-contained objects Vectors allows us to solve all these problems

9 Computer programming 9 Vectors A vector is a class template similar to a dynamic array. Examples of a vector declaration vector vectorName; vector vectorName(size); Inserting an element at the end of a vector vectorName.push_back(…) Access an element you can either use [] or at vectorName[…];//does not check for “out of bound” vectorName.at(…);//checks for “out of bound” The method size returns the number of elements in the vector; you can use it in a for loop vectorName.size()

10 Computer programming 10 Vectors: example #include using namespace std; int main() { int n; cin >> n; //read the size from the keyboard vector vec(n); //declare a vector of n elements for(int i=0;i<vec.size();i++) //size is n vec[i]=1./(i+1); vec.push_back(1.0); for(int i=0;i<vec.size();i++) //size is n+1 cout << vec[i]<<“ “; cout << endl; return 0; }

11 Computer programming 11 Maps Arrays and vectors use integer indices. Can we use other types, such as strings, as indices? – Yes; use maps! Maps: a set of pairs (key, value) – Keys cannot be duplicated – Values can be duplicated – Recall the concept of map/function from math! Declaring a map Map mapName

12 Computer programming 12 Maps Initialize and access a map using [] or insert To go through a map use a for loop and iterators – Iterators allow us to go through STL containers – To declare an iterator STLContainer::iterator itr – To access the first element in the container use itr.begin() – To increment an iterator use the operator ++ – To detect the end of the user itr.end() (the location after the last element) – itr->first is the key – itr->second is the value

13 Computer programming 13 Problem Write a program that counts the number of occurrences of words that the users enters. Compute the probability of each word.

14 Computer programming 14 Maps: Example #include using namespace std; int main() { map dictionary; string str; cin >> str; while (str != "q") { dictionary[str]++; cin >> str; } dictionary.insert(pair ("bel",++dictionary["bel"])); map ::iterator itr; //iterator for (itr=dictionary.begin();itr!=dictionary.end();++itr) cout first " second<<endl; return 0; }


Download ppt "Computer programming 1 Multidimensional Arrays, and STL containers: vectors and maps."

Similar presentations


Ads by Google