1 / 14

Multidimensional Arrays, and STL containers: vectors and maps

Multidimensional Arrays, and STL containers: vectors and maps. Objectives. Learn how to declare multidimensional arrays Vectors Maps Illustrate how and when to use multidimensional arrays Vectors Maps. Motivations.

farhani
Télécharger la présentation

Multidimensional Arrays, and STL containers: vectors and maps

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


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

  2. Objectives • Learn how to declare • multidimensional arrays • Vectors • Maps • Illustrate how and when to use • multidimensional arrays • Vectors • Maps 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 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 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]<< “”; } } Computer programming

  6. Visualizing 2D arrays • A table with columns (vertical) and rows (horizontal) const int distances[6][6] 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. 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 Computer programming

  9. 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 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() Computer programming

  10. Vectors: example #include <iostream> #include <vector> using namespace std; int main() { int n; cin >> n; //read the size from the keyboard vector<double> 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; } 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<typeName1,typeName2> mapName 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 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. Computer programming

  14. 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; //iterator for (itr=dictionary.begin();itr!=dictionary.end();++itr) cout << itr->first << "--->"<<itr->second<<endl; return 0; } Computer programming

More Related