Multidimensional Arrays, and STL containers: vectors and maps
description
Transcript of Multidimensional Arrays, and STL containers: vectors and maps
Computer programming1
Multidimensional Arrays, and STL containers: vectors and maps
Computer programming2
Objectives
Learn how to declare– multidimensional arrays– Vectors– Maps
Illustrate how and when to use– multidimensional arrays– Vectors– Maps
Computer programming3
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
Computer programming4
Multidimensional arrays
DeclarationtypeName name[SIZE_1][SIZE_2]…[SIZE_n]
Examplesdouble distances[5][5];
double surface[100][100];
int threeDimension[10][10][10];
Initializationdouble distances[5][5]={{0,1,2,3,4,5},{2,3}};
double myArray[][2]={{2,3},{4,5}};//a 2x2 array
Computer programming5
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]<< “”;
}
}
Computer programming6
Visualizing 2D arrays
A table with columns (vertical) and rows (horizontal) const int distances[6][6]
Computer programming7
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.
Computer programming8
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
Computer programming9
Vectors
A vector is a class template similar to a dynamic array. Examples of a vector declaration
vector<typeName> vectorName;vector<typeName> vectorName(size);
Inserting an element at the end of a vector vectorName.push_back(…)
Access an element you can either use [] or atvectorName[…];//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 loopvectorName.size()
Computer programming10
Vectors: example
#include <iostream>#include <vector>
using namespace std;
int main() {int n;cin >> n; //read the size from the keyboardvector<double> vec(n); //declare a vector of n elementsfor(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;
}
Computer programming11
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 mapMap<typeName1,typeName2> mapName
Computer programming12
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
Computer programming13
Problem
Write a program that counts the number of occurrences of words that the users enters. Compute the probability of each word.
Computer programming14
Maps: Example
#include <iostream>#include <vector>#include <map>using namespace std;
int main() {
map<string, int> dictionary;string str;cin >> str;while (str != "q") {
dictionary[str]++;cin >> str;
}dictionary.insert(pair<string,int>("bel",++dictionary["bel"]));map<string,int>::iterator itr; //iteratorfor (itr=dictionary.begin();itr!=dictionary.end();++itr) cout << itr->first << "--->"<<itr->second<<endl;return 0;
}