Benutzer-Werkzeuge

Webseiten-Werkzeuge


edv:prg:cpp:crashkurs:part_04_stl_4a

C++ Crashkurs / Übung 7

STL - Verwendung von "std::string".

Ueb4a.h:

// File : Ueb4a.h
//
 
#include "..\stdafx.h"
 
#include "Ueb4a.VerketteteListe.h"
 
#include <string>
 
using namespace Ueb4a;
 
template<class T>
void ListeAusgeben (const CListe<T>& liste, const char* pszHeader)
{
	std::cout << pszHeader << std::endl;
	for (auto iter = liste.begin(); iter != liste.end(); ++iter)
	{
		std::cout << "    " << *iter << std::endl;
	}
}
 
 
int _ueb (int argc, _TCHAR* argv[])
{
	auto pListe1 = new CListe<std::string>();
 
	pListe1->ElementAnhaengen("47-11");
	pListe1->ElementAnhaengen("0-8-15");
	pListe1->ElementAnhaengen("Die Antwort auf alle Fragen...");
 
	ListeAusgeben(*pListe1, "Liste mit Zeichenketten:");
 
	delete pListe1;
 
	// ------------
 
	auto pListe2 = new CListe<std::string>();
	char buffer[1024];
 
	strcpy_s(buffer, sizeof(buffer), "47-11");
	pListe2->ElementAnhaengen(buffer);
 
	strcpy_s(buffer, sizeof(buffer), "0-8-15");
	pListe2->ElementAnhaengen(buffer);
 
	strcpy_s(buffer, sizeof(buffer), "Die Antwort auf alle Fragen...");
	pListe2->ElementAnhaengen(buffer);
 
	ListeAusgeben(*pListe2, "Liste mit _dynamischen_ Zeichenketten:");
 
	delete pListe2;
 
	// ------------
 
	auto pListe3 = new CListe<std::string>();
 
	std::string buffer2;
	for (int i = 0; i < 5; ++i)
	{
		buffer2 = std::to_string(i + 1);
		pListe3->ElementAnhaengen("Eintrag #" + buffer2 + "...");
	}
 
	ListeAusgeben(*pListe3, "Liste mit berechneten Zeichenketten:");
 
	delete pListe3;
 
	return 0;
 
}
 
// EOF

Ueb4a.VerketteteListe.h:

// File : Ueb4a.VerketteteListe.h
//
 
namespace Ueb4a
{
	template<class TElement>
	class CListe
	{
	private:
		class CListenElement
		{
		friend CListe;
 
		private:
			TElement nInhalt;
			CListenElement*	pNaechster;
 
		public:
			CListenElement (TElement newInhalt)
			{
				nInhalt = newInhalt;
				pNaechster = nullptr;
			}
 
			~CListenElement ()
			{
				delete pNaechster;
				pNaechster = nullptr;
			}
 
			TElement GetInhalt ()
			{
				return nInhalt;
			}
 
			CListenElement* GetNaechster ()
			{
				return pNaechster;
			}
		};
 
		class CListeIterator
		{
		private:
			CListenElement* element;
 
		public:
			CListeIterator (CListenElement* pElem)
			{
				element = pElem;
			}
 
			bool operator!=(CListeIterator another)
			{
				return (this->element != another.element);
			}
 
			CListeIterator& operator++()
			{
				element = element->GetNaechster();
 
				return *this;
			}
 
			TElement operator*()
			{
				return element->GetInhalt();
			}
		};
 
	public:
		CListenElement* pStart;
 
		CListe ()
		{
			pStart = nullptr;
		}
 
		CListe (const CListe& other)
		{
			pStart = nullptr;
 
			for (auto iter = other.begin(); iter != other.end(); ++iter) ElementAnhaengen(*iter);
		}
 
		~CListe ()
		{
			delete pStart;
			pStart = nullptr;
		}
 
		CListe& operator=(const CListe& other)
		{
			if (pStart != nullptr) delete pStart;
			pStart = nullptr;
 
			for (auto iter = other.begin(); iter != other.end(); ++iter) ElementAnhaengen(*iter);
 
			return *this;
		}
 
		CListeIterator begin () const
		{
			return CListeIterator(pStart);
		}
 
		CListeIterator end () const
		{
			return CListeIterator(nullptr);
		}
 
		void ElementAnhaengen (TElement nInhalt)
		{
			auto pNeu = new CListenElement(nInhalt);
 
			if (pStart == nullptr)
			{
				pStart = pNeu;
			}
			else
			{
				auto pLetztes = pStart;
				while (pLetztes->GetNaechster() != nullptr) pLetztes = pLetztes->GetNaechster();
				pLetztes->pNaechster = pNeu;
			} // if
		}
 
	};
}
 
// EOF

Programm.cpp: - Der Einstiegspunkt

// Programm.cpp : Defines the entry point for the console application.
//
 
#include "stdafx.h"
 
#include "Ueb4a.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_4a.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