Добавил:
Studfiles2
Опубликованный материал нарушает ваши авторские права? Сообщите нам.
Вуз:
Предмет:
Файл:Лабораторная работа 22 / lab2_1 / test_associative_containers / test_associative_containers
.cpp#include <conio.h>
#include <stdio.h>
#include <process.h>
#include <ctime>
#include <string>
#include <sstream>
#include <iostream>
#include <map>
#include <set>
using namespace std;
void clrscr(){
system("cls");
}
template <class T>
inline string buildString(const T& t){
stringstream ss;
ss << t;
return ss.str();
}
template<class MapContainer>
void printMap(MapContainer& c){
cout << endl << "map: ";
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++){
string s = buildString<int>(j + 1);
cout << "map.insert(" << j << ";\"" << s << "\")" <<endl;
c.insert(make_pair(j, s));
}
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;
cout << "map.find(" << 3 << ") found ? " << (c.find(3) != c.end()) << endl;
getch();
cout << endl;
cout << "map.count(11)= " << c.count(11) << endl;
cout << "map.find(" << 11 << ") found ? " << (c.find(11) != c.end()) << endl;
getch();
cout << endl;
string s1 = "new value";
cout << "map.insert(" << 3 << ";\"" << s1 << "\")" <<endl;
c.insert(make_pair(3, s1));
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, string> Map;
typedef multimap<int, string> MMap;
int main(int argc, char* argv[])
{
clrscr();
cout << endl;
cout << "===============================================" << endl;
cout << "Testing STL associative containers: map, set..." << endl;
cout << "===============================================" << endl;
getch();
Map m;
MMap mm;
// test map
cout << endl;
cout << "------------" << endl;
cout << "Testing map:" << endl;
cout << "------------" << endl;
testMap<Map>(m);
// test multimap
cout << endl;
cout << "-----------------" << endl;
cout << "Testing multimap:" << endl;
cout << "-----------------" << endl;
testMap<MMap>(mm);
return 0;
}
Соседние файлы в папке test_associative_containers