Добавил:
Studfiles2
Опубликованный материал нарушает ваши авторские права? Сообщите нам.
Вуз:
Предмет:
Файл:Лабораторная работа 22 / lab2_1 / lab2_1
.cpp
#include <conio.h>
#include <stdio.h>
#include <process.h>
#include <ctime>
#include "stdafx.h"
#include "TextInTriangle.h"
#include "Hash.h"
#include "utils.h"
#include <string>
#include <iostream>
#include <vector>
#include <list>
#include <deque>
#include <stack>
#include <queue>
#include <map>
#include <set>
using namespace std;
void clrscr(){
system("cls");
}
Shape* createRandomShape(){
int random = (rand()%4)+1;
switch(random){
case 1:
return new Point();
case 2:
return new Line();
case 3:
return new Triangle();
case 4:
return new TextInTriangle();
default:
return new Point();
}
}
template<class MapContainer>
void printMap(MapContainer& c){
cout << endl << "map: " << endl;
for(MapContainer::iterator it = c.begin(); it != c.end(); it++)
cout << (*it).first << ":" << (*it).second << endl;
cout << endl;
}
template<class MapContainer>
void testMap(MapContainer& c){
cout << "map.empty() = " << c.empty() << endl;
cout << "map.size() = " << c.size() << endl;
cout << endl << "push 10 elements into map" << endl;
getch();
for(long j = 0; j < 10; j++){
Shape* s1 = createRandomShape();
cout << "map.insert(" << j << "; random shape)" <<endl;
c.insert(make_pair(j, s1));
}
getch();
cout << endl;
cout << "map.empty() = " << c.empty() << endl;
cout << "map.size() = " << c.size() << endl;
getch();
cout << endl;
cout << "map.count(3) = " << c.count(3) << endl;
MapContainer::iterator f3 = c.find(3);
if (f3 != c.end())
cout << "map.find(" << 3 << ") found: " << (*f3).second << endl;
else
cout << "map.find(" << 3 << ") not found " << endl;
getch();
cout << endl;
cout << "map.count(11) = " << c.count(11) << endl;
MapContainer::iterator f11 = c.find(11);
if (f11 != c.end())
cout << "map.find(" << 11 << ") found: " << (*f11).second << endl;
else
cout << "map.find(" << 11 << ") not found " << endl;
getch();
cout << endl;
Shape* s = createRandomShape();
cout << "map.put(" << 3 << "; random shape)" <<endl;
c.insert(make_pair(3, s));
cout << "map.count(3) = " << c.count(3) << endl;
getch();
printMap<MapContainer>(c);
getch();
cout << "map.erase(3)" << endl;
c.erase(3);
getch();
printMap<MapContainer>(c);
getch();
};
typedef map<int, Shape*> Map;
int main(int argc, char* argv[])
{
clrscr();
Map h;
testMap<Map>(h);
return 0;
}
Соседние файлы в папке lab2_1