<?xml version="1.0" encoding="UTF-8"?><rss xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:content="http://purl.org/rss/1.0/modules/content/" xmlns:atom="http://www.w3.org/2005/Atom" version="2.0"><channel><title><![CDATA[copy algortihmus mit Liste]]></title><description><![CDATA[<p>Hallo Leute!</p>
<p>Die Liste ist Private-Member der Klasse Calendar!<br />
Ich hätte mal eine kurze Frage zu folgender Funktion:</p>
<pre><code class="language-cpp">void Calendar::PrintAllEntries(bool ascending) const{

Print(this-&gt;CalendarList, &quot;Ausgabe der Liste mit Algorithm&quot;);		
}

// Hilfsfunktionen für Ausgabe
void Print(list&lt;CalendarEntry&gt; const &amp; list, string const &amp; s){			

cout &lt;&lt; endl &lt;&lt; s &lt;&lt; endl;
copy(list.begin(), list.end(), ostream_iterator&lt;CalendarEntry&gt; (cout, &quot; &quot;));
cout &lt;&lt; endl;
}
</code></pre>
<p>Mein Problem ist jetzt aber, dass ich beim Copy algorithmus ja nicht per ostream iterator die Struktur ausgeben kann, sondern eig will ich ja ausgeben:</p>
<p>Struktur.String<br />
Struktur.Datum</p>
<p>wie mache ich das jetzt am geschicktesten? <img
      src="https://www.c-plusplus.net/forum/plugins/nodebb-plugin-emoji/emoji/emoji-one/1f642.png?v=ab1pehoraso"
      class="not-responsive emoji emoji-emoji-one emoji--slightly_smiling_face"
      title="=)"
      alt="🙂"
    /></p>
<p>Danke im Vorraus allen Helfern!</p>
]]></description><link>https://www.c-plusplus.net/forum/topic/303644/copy-algortihmus-mit-liste</link><generator>RSS for Node</generator><lastBuildDate>Mon, 10 Aug 2026 07:23:51 GMT</lastBuildDate><atom:link href="https://www.c-plusplus.net/forum/topic/303644.rss" rel="self" type="application/rss+xml"/><pubDate>Sat, 19 May 2012 09:19:41 GMT</pubDate><ttl>60</ttl><item><title><![CDATA[Reply to copy algortihmus mit Liste on Sat, 19 May 2012 09:19:41 GMT]]></title><description><![CDATA[<p>Hallo Leute!</p>
<p>Die Liste ist Private-Member der Klasse Calendar!<br />
Ich hätte mal eine kurze Frage zu folgender Funktion:</p>
<pre><code class="language-cpp">void Calendar::PrintAllEntries(bool ascending) const{

Print(this-&gt;CalendarList, &quot;Ausgabe der Liste mit Algorithm&quot;);		
}

// Hilfsfunktionen für Ausgabe
void Print(list&lt;CalendarEntry&gt; const &amp; list, string const &amp; s){			

cout &lt;&lt; endl &lt;&lt; s &lt;&lt; endl;
copy(list.begin(), list.end(), ostream_iterator&lt;CalendarEntry&gt; (cout, &quot; &quot;));
cout &lt;&lt; endl;
}
</code></pre>
<p>Mein Problem ist jetzt aber, dass ich beim Copy algorithmus ja nicht per ostream iterator die Struktur ausgeben kann, sondern eig will ich ja ausgeben:</p>
<p>Struktur.String<br />
Struktur.Datum</p>
<p>wie mache ich das jetzt am geschicktesten? <img
      src="https://www.c-plusplus.net/forum/plugins/nodebb-plugin-emoji/emoji/emoji-one/1f642.png?v=ab1pehoraso"
      class="not-responsive emoji emoji-emoji-one emoji--slightly_smiling_face"
      title="=)"
      alt="🙂"
    /></p>
<p>Danke im Vorraus allen Helfern!</p>
]]></description><link>https://www.c-plusplus.net/forum/post/2213024</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/2213024</guid><dc:creator><![CDATA[Eteru]]></dc:creator><pubDate>Sat, 19 May 2012 09:19:41 GMT</pubDate></item><item><title><![CDATA[Reply to copy algortihmus mit Liste on Sat, 19 May 2012 09:39:54 GMT]]></title><description><![CDATA[<p>Ich hab dich so verstanden:</p>
<pre><code class="language-cpp">class calender
{
private:
	string str;
	string datum;
public:
	calender(const string&amp; str, const string&amp; datum) : str(str), datum(datum) {}
	friend ostream&amp; operator&lt;&lt;(ostream&amp; out, const calender&amp; c)
	{
		return out &lt;&lt; c.str &lt;&lt; ','&lt;&lt; c.datum &lt;&lt; '\n';
	}
};

int main()
{
	list&lt;calender&gt; l;
	l.push_back(calender(&quot;string&quot;,&quot;datum&quot;));
	l.push_back(calender(&quot;string2&quot;,&quot;datum2&quot;));
	copy(l.begin(),l.end(),ostream_iterator&lt;calender&gt;(cout));

	return 0;
}
</code></pre>
]]></description><link>https://www.c-plusplus.net/forum/post/2213026</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/2213026</guid><dc:creator><![CDATA[[[global:guest]]]]></dc:creator><pubDate>Sat, 19 May 2012 09:39:54 GMT</pubDate></item><item><title><![CDATA[Reply to copy algortihmus mit Liste on Sat, 19 May 2012 10:23:55 GMT]]></title><description><![CDATA[<p>Ah mein Fehler, ich habe eine Info unterschlagen! Bei dem Datum handelt es sich wiederum um ein Unterobjekt mit eigener Printfunktion und Vergleichsfunktion Compare.</p>
<p>Der Vektor soll zuerst nach Objekt &quot;Datum&quot; sortiert werden und danach möglichst elegant mittels Sort-Algorithmus richtig sortiert ausgegeben werden.</p>
<p>Hier mal zum Lesen den Codes des Objektes &quot;Datum&quot;, für alle dies interessiert:</p>
<pre><code class="language-cpp">// Header

#ifndef Date_H
#define Date_H

#include &lt;iostream&gt;

class Date {
	public:
		// Default-Constructor
		Date();

		// Constructor
		Date(int const day, int const month, int const year);

		//Destruktor
		~Date();

		// Accessor functions getDay, getMonth, getYear
		int GetDay() const;
		int GetMonth() const;
		int GetYear() const;

		// Comparison function
		int Compare(const Date &amp;d) const;

		// Write date to an output stream
		void Print(std::ostream &amp;out) const;

	private:
		int mDay;
		int mMonth;
		int mYear;
	};

	// Output operator
	inline std::ostream &amp;operator&lt;&lt;(std::ostream &amp;out, Date const &amp;date) {
		date.Print(out);
		return out;
	}

#endif
</code></pre>
<pre><code class="language-cpp">// Modul

/ Default - CTOR anlegen
Date::Date() : mDay(0), mMonth(0), mYear(0)	
{
	cout &lt;&lt; &quot;+++ Objekt mit Adresse: &quot; &lt;&lt; hex &lt;&lt; this &lt;&lt; &quot; erzeugt. +++&quot; &lt;&lt; endl;
}

//CTor MIT Werten
Date::Date(int const day, int const month, int const year) : mDay(day), mMonth(month), mYear(year)
{
	cout &lt;&lt; &quot;+++ Objekt mit Adresse: &quot; &lt;&lt; hex &lt;&lt; this &lt;&lt; &quot; erzeugt. +++&quot; &lt;&lt; endl;
}

// DTOR
Date::~Date() {
	cout &lt;&lt; &quot;+++ Objekt mit Adresse: &quot; &lt;&lt; hex &lt;&lt; this &lt;&lt; &quot; zerstört. +++&quot; &lt;&lt; endl;
}

// Accessor functions getDay
int Date::GetDay() const
{
	return mDay;
}

// Accessor functions getMonth
int Date::GetMonth() const
{
	return mMonth;
}

// Accessor functions getYear
int Date::GetYear() const
{
	return mYear;
}

// Comparison function (returns -1 if current date (this) is before d, +1 if it is after d, 0 if it is equal)
int Date::Compare(const Date &amp;d) const {

	if ( this-&gt;mYear &lt; d.mYear) {
		return -1;
	}
	else if (this-&gt;mYear &gt; d.mYear) {
		return 1;
	}
	else if (this-&gt;mYear == d.mYear){

		if (this-&gt;mMonth &lt; d.mMonth){
			return -1;
		}
		else if(this-&gt;mMonth &gt; d.mMonth){
			return 1;
		}
		else if (this-&gt;mMonth == d.mMonth) {

			if (this-&gt;mDay &lt; d.mDay) {
				return -1;
			}
			else if (this-&gt;mDay &gt; d.mDay) {
				return 1;
			}
			else if (this-&gt;mDay &lt; d.mDay){
				return 0;
			}
		}
	}
	return 0;
}

// Write date to an output stream
void Date::Print(std::ostream &amp;out) const {
	out &lt;&lt; &quot;---------------------------------&quot; &lt;&lt; endl;
	out &lt;&lt; setfill('0') &lt;&lt; setw(2) &lt;&lt; dec &lt;&lt; mDay &lt;&lt; &quot;.&quot;;
	out &lt;&lt; setfill('0') &lt;&lt; setw(2) &lt;&lt; dec &lt;&lt; mMonth &lt;&lt; &quot;.&quot;;
	out &lt;&lt; setfill('0') &lt;&lt; setw(4) &lt;&lt; dec &lt;&lt; mYear &lt;&lt; endl;
	out &lt;&lt; &quot;---------------------------------&quot; &lt;&lt; endl;
}
</code></pre>
]]></description><link>https://www.c-plusplus.net/forum/post/2213041</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/2213041</guid><dc:creator><![CDATA[Eteru]]></dc:creator><pubDate>Sat, 19 May 2012 10:23:55 GMT</pubDate></item><item><title><![CDATA[Reply to copy algortihmus mit Liste on Sat, 19 May 2012 10:25:24 GMT]]></title><description><![CDATA[<p>Und hier noch die Klasse kalender:</p>
<pre><code class="language-cpp">struct CalendarEntry {
	std::string Note;
	Date Datum;
 };

typedef std::list &lt;CalendarEntry&gt; DataList;
typedef std::list&lt;CalendarEntry&gt;::iterator ListIterator;

class Calendar {

public:
		// Constructors, Destructor, assignment operator (if necessary)
		Calendar();
		~Calendar();

		// Assignment operator 
		Calendar &amp;operator =(Calendar const &amp;cal);

		// Adds an entry to the calendar. If an entry for the given date already
		// exists, it is replaced.
		void AddEntry(Date const &amp;date, std::string const &amp;text);

		// Deletes all entries from the calendar
		void Clear();

		// Prints all entries, sorted by their date, either ascending or
		// descending, depending on the parameter
		void PrintAllEntries(bool ascending=true) const;

private:
		DataList CalendarList;
};
</code></pre>
]]></description><link>https://www.c-plusplus.net/forum/post/2213042</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/2213042</guid><dc:creator><![CDATA[Eteru]]></dc:creator><pubDate>Sat, 19 May 2012 10:25:24 GMT</pubDate></item><item><title><![CDATA[Reply to copy algortihmus mit Liste on Sat, 19 May 2012 10:30:57 GMT]]></title><description><![CDATA[<p>So und jetzt NOCH EINMAL mein Problem erläutert!</p>
<p>Ich weiß nicht, wie ich den COPY-ALGORITHMUS anwenden muss / modifizieren muss, damit er funktioniert^^</p>
<p>Das ist mein Problem! ATM so wie ich ihn in der Printfunktion schreibe, funzt das mitm ostream_iterator nicht!</p>
<p>Die Ausgabe auf cout muss ja auf die Strukturunterobjekte erfolgen und nicht auf die struktur selber! Aber der Compiler meckert immer^^</p>
]]></description><link>https://www.c-plusplus.net/forum/post/2213045</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/2213045</guid><dc:creator><![CDATA[Eteru]]></dc:creator><pubDate>Sat, 19 May 2012 10:30:57 GMT</pubDate></item><item><title><![CDATA[Reply to copy algortihmus mit Liste on Sat, 19 May 2012 12:27:58 GMT]]></title><description><![CDATA[<p>Dir fehlt eigentlich nur der Ausgabeoperator für CalendarEntry. Aber der Reihe nach: Zuerst wird mal Date aufgeräumt. Im zweiten Konstruktor sind die const-Qualifizierer bei den Parametern überflüssig, weil die Werte by value übergeben werden. Deine Compare-Funktion ist so kompliziert aufgeschrieben, dass dir nicht mal dein Fehler beim letzten if aufgefallen ist. Ich würde sowieso einen operator&lt; statt Compare definieren, sodass std::sort direkt drauf funktioniert:</p>
<pre><code class="language-cpp">bool operator&lt;(const Date&amp; lhs, const Date&amp; rhs)
{
  if(lhs.year != rhs.year)
    return lhs.year &lt; rhs.year;
  if(lhs.month != rhs.month)
    return lhs.month &lt; rhs.month;
  return lhs.day &lt; rhs.day;
}
</code></pre>
<p>Statt</p>
<pre><code class="language-cpp">typedef std::list &lt;CalendarEntry&gt; DataList;
typedef std::list&lt;CalendarEntry&gt;::iterator ListIterator;
</code></pre>
<p>nimmst du besser</p>
<pre><code class="language-cpp">typedef std::vector &lt;CalendarEntry&gt; DataList;
typedef DataList::iterator ListIterator;
</code></pre>
<p>Auf diese Weise passt ListIterator immer zum DataList-Container. Außerdem ist std::vector in 99% der Fälle die richtige Wahl, auch wenn dir irgendwelche Theoretiker mal gesagt haben, dass man bei std::list gut einfügen und entfernen kann. Bei vector funktioniert dann auch das Kopieren und Sortieren schnell genug. Dein Ausgabeoperator macht dann so etwas:</p>
<pre><code class="language-cpp">DataList calendarCopy(CalendarList);
sort(calendarCopy.begin(), calendarCopy.end());
copy(calendarCopy.begin(), calendarCopy.end(), ostream_iterator&lt;CalendarEntry&gt;(out, &quot; &quot;));
</code></pre>
<p>Edit: Achja, in deiner Print-Funktion änderst du mit Manipulatoren den Zustand des ostream-Objekts. Den Zustand solltest du zu Beginn der Funktion kopieren und beim Verlassen der Funktion zurücksetzen.</p>
]]></description><link>https://www.c-plusplus.net/forum/post/2213087</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/2213087</guid><dc:creator><![CDATA[Michael E.]]></dc:creator><pubDate>Sat, 19 May 2012 12:27:58 GMT</pubDate></item><item><title><![CDATA[Reply to copy algortihmus mit Liste on Sat, 19 May 2012 14:33:24 GMT]]></title><description><![CDATA[<p>Hey, schonmal danke für deine Hilfe so weit!</p>
<p>Leider ist in der Aufgabe definiert, dass ich eine Liste verwenden muss, könnte ich frei wählen, hätte ich auch einen Vector verwendet!</p>
<p>Die Compare-Funktion war teilweise auch so vorgegeben, aber deine idee mit dem Operator Owerloading werd ich glatt übernehmen ist viel eleganter <img
      src="https://www.c-plusplus.net/forum/plugins/nodebb-plugin-emoji/emoji/emoji-one/1f609.png?v=ab1pehoraso"
      class="not-responsive emoji emoji-emoji-one emoji--winking_face"
      title=";)"
      alt="😉"
    /></p>
<p>Ich probier mal nachher ein paar Sachen aus und meld mich wieder falls noch fragen auftreten <img
      src="https://www.c-plusplus.net/forum/plugins/nodebb-plugin-emoji/emoji/emoji-one/1f609.png?v=ab1pehoraso"
      class="not-responsive emoji emoji-emoji-one emoji--winking_face"
      title=";)"
      alt="😉"
    /></p>
]]></description><link>https://www.c-plusplus.net/forum/post/2213145</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/2213145</guid><dc:creator><![CDATA[Eteru]]></dc:creator><pubDate>Sat, 19 May 2012 14:33:24 GMT</pubDate></item><item><title><![CDATA[Reply to copy algortihmus mit Liste on Sat, 19 May 2012 14:43:03 GMT]]></title><description><![CDATA[<pre><code class="language-cpp">copy(calendarCopy.begin(), calendarCopy.end(), ostream_iterator&lt;CalendarEntry&gt;(out, &quot; &quot;));
</code></pre>
<p>Eben genau das, dachte ich, dass ich eben NICHT machen darf, weil ich ja keinen CalenderEntry auf der Konsole ausgeben kann, da es sich dabei ja um eine Struktur handelt! So wie ich die Zeile grad verstehe, gibt der doch dann Strukturen auf outstream aus, was ja eigentlich nicht geht oder?^^</p>
<p>will ja eigentlich sukzessive den String und die Printfunktion des Date-Objektes an jeder Stelle der Liste ausgeben!</p>
]]></description><link>https://www.c-plusplus.net/forum/post/2213149</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/2213149</guid><dc:creator><![CDATA[Eteru]]></dc:creator><pubDate>Sat, 19 May 2012 14:43:03 GMT</pubDate></item><item><title><![CDATA[Reply to copy algortihmus mit Liste on Sat, 19 May 2012 16:05:06 GMT]]></title><description><![CDATA[<p>Eteru schrieb:</p>
<blockquote>
<p>So wie ich die Zeile grad verstehe, gibt der doch dann Strukturen auf outstream aus, was ja eigentlich nicht geht oder?^^</p>
</blockquote>
<p>Klar geht das, wenn du den entsprechenden Operator überlädst. <code>struct</code> ist fast dasselbe wie <code>class</code> , lediglich die Default-Sichtbarkeit (public/private) und Default-Vererbung (public/private) sind verschieden. Schreib also so etwas:</p>
<pre><code class="language-cpp">ostream&amp; operator&lt;&lt;(ostream&amp; out, const CalendarEntry&amp; entry)
{
  out &lt;&lt; entry.Date &lt;&lt; &quot; (&quot; &lt;&lt; entry.Note &lt;&lt; &quot;)&quot;;
  return out;
}
</code></pre>
]]></description><link>https://www.c-plusplus.net/forum/post/2213184</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/2213184</guid><dc:creator><![CDATA[Michael E.]]></dc:creator><pubDate>Sat, 19 May 2012 16:05:06 GMT</pubDate></item><item><title><![CDATA[Reply to copy algortihmus mit Liste on Tue, 22 May 2012 12:15:14 GMT]]></title><description><![CDATA[<p>Tut mir Leid konnte eine Weile lang nicht ins Internet!</p>
<p>Vielen dank für den Tipp mit dem Operator Owerloading! Hat mein Problem gelöst!<br />
Funktioniert jetzt alles wunderbar!</p>
]]></description><link>https://www.c-plusplus.net/forum/post/2214128</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/2214128</guid><dc:creator><![CDATA[Eteru]]></dc:creator><pubDate>Tue, 22 May 2012 12:15:14 GMT</pubDate></item></channel></rss>