Benutzer-Werkzeuge

Webseiten-Werkzeuge


edv:prg:cpp:crashkurs:part_04_stl_4c

C++ Crashkurs / Übung 9

Weitere Ausbaustufe mit Templates.

Ueb4c.h:

// File : Ueb4c.h
//
 
#include "..\stdafx.h"
 
#include <string>
#include <vector>
#include <list>
#include <map>
 
// Damit man nicht für jeden Typ eine eigene Bearbeitungsfunktion schreiben muss...
 
//template<class T>
//void ListeAusgeben (const std::vector<T>& liste, const char* pszHeader)
//{
//	std::cout << pszHeader << std::endl;
//	for (auto iter = liste.begin(); iter != liste.end(); ++iter)
//	{
//		std::cout << "    " << *iter << std::endl;
//	}
//}
//
//template<class T>
//void ListeAusgeben (const std::list<T>& liste, const char* pszHeader)
//{
//	std::cout << pszHeader << std::endl;
//	for(auto iter = liste.begin(); iter != liste.end(); ++iter)
//	{
//		std::cout << "    " << *iter << std::endl;
//	}
//}
 
// ...man legt auch für die Argumente ein Template an:
template<class T>
void ListeAusgeben (const T& container, const char* pszHeader)
{
	std::cout << pszHeader << std::endl;
	// ! korrekt, es geht aber besser
	//for (auto iter = container.begin(); iter != container.end(); ++iter)
	// ! so ist sicherer, weil auch mit Objekten funktioniert, die keine Methoden begin() und end() haben
	for (auto iter = std::begin(container); iter != std::end(container); ++iter)
	{
		std::cout << "    " << *iter << std::endl;
	}
}
 
 
// Für Typen mit mehreren Werten
template<class _Ty1, class _Ty2>
std::ostream& operator << (std::ostream& strm, const std::pair<_Ty1, _Ty2>& pair)
{
	strm << "key = " << pair.first << "; value = " << pair.second;
	return strm;
}
 
 
int _ueb (int argc, _TCHAR* argv[])
{
	auto pVector = std::vector<std::string>(); // Vector mit Strings
 
	//pVector->ElementAnhaengen("47-11");
	//pVector->ElementAnhaengen("0-8-15");
	//pVector->ElementAnhaengen("Die Antwort auf alle Fragen...");
	pVector.push_back("47-11");
	pVector.push_back("0-8-15");
	pVector.push_back("Die Antwort auf alle Fragen...");
 
	//ListeAusgeben(*pVector, "vector<>");
	ListeAusgeben(pVector, "vector<>");
 
	std::cout << pVector[2];
 
	//delete pVector;
 
	// -----
 
	auto pList = std::list<std::string>(); // Liste mit Strings
 
	//pList->ElementAnhaengen("47-11");
	//pList->ElementAnhaengen("0-8-15");
	//pList->ElementAnhaengen("Die Antwort auf alle Fragen...");
	pList.push_back("47-11");
	pList.push_back("0-8-15");
	pList.push_back("Die Antwort auf alle Fragen...");
 
	ListeAusgeben(pList, "list<>");
 
	//delete pList;
 
	auto pMap = std::map<std::string, int>(); // Map mit Strings/Zahlen
 
	pMap["AA"] = 123;
	pMap["BB"] = 456;
	pMap["CC"] = 789;
 
	ListeAusgeben(pMap, "map<>");
 
	auto pMap2 = std::map<std::string, std::string>(); // Map mit Strings/Strings
 
	pMap2["AA"] = "aaaaaaaaaa";
	pMap2["BB"] = "xxxxxxxxxxxx";
	pMap2["CC"] = "cccccccccccccc";
 
	ListeAusgeben(pMap2, "map2<>");
 
	return 0;
}
 
// EOF

Programm.cpp: - Der Einstiegspunkt

// Programm.cpp : Defines the entry point for the console application.
//
 
#include "stdafx.h"
 
#include "Ueb4c.h"
 
int _tmain (int argc, _TCHAR* argv[])
{
	return _ueb(argc, argv);
}
 
// EOF

stdafx.h:

// stdafx.h : include file for standard system include files,
// or project specific include files that are used frequently, but
// are changed infrequently
//
 
#pragma once
 
#include "targetver.h"
 
#include <stdio.h>
#include <tchar.h>
 
// TODO: reference additional headers your program requires here
 
#include <malloc.h>
#include <iostream>
 
// EOF

targetver.h:

// Including SDKDDKVer.h defines the highest available Windows platform.
 
// If you wish to build your application for a previous Windows platform, include WinSDKVer.h and
// set the _WIN32_WINNT macro to the platform you wish to support before including SDKDDKVer.h.
 
#pragma once
 
#include <SDKDDKVer.h>

Stand: 19.09.2014 — : Jürgen Kreick

EOF

edv/prg/cpp/crashkurs/part_04_stl_4c.txt · Zuletzt geändert: 2020/01/11 01:23 von 127.0.0.1

Donate Powered by PHP Valid HTML5 Valid CSS Driven by DokuWiki