kleines ordnungsproblem
-
Ich hab gerade ein Programm geschrieben das mir in einem Feld abcheckt wie oft ein element darin vorkommt. Das Feld besteht aus Strings und den inhalt des felder lasse ich für jede position in eine variable einlesen. Die Variable wird dann überprüft ob sie den gesuchten String (wird von mir definiert) enthält. Das ganze steht in einer for schleife um eben jedes element im feld in die variable zu bringen. Am schluss soll mir das Teil ausgeben wie oft der gesuchte string enthalten war. Aber wegen der for schleife merkt sich das programm immer nur die letzte abfrage. Sprich: das ergebnis ist entweder eins oder null...
Ich denke mal meine Erklärung war etwas konfus, deshalb poste ich lieber den Programmcode... Wäre toll wenn wer wüsste wie ich einfach ausgeben kann wie oft der gesuchte string drinsteht.
Programmcode:
#include <iostream> #include <algorithm> #include <functional> #include <string> #include <vector> using namespace std; void main() { string a,b; string feld[5] = {"111", "000", "010", "101", "110"}; int ausgabe; for(int x = 0; x<5; x++) { a = feld[x]; // cout << a << "\n"; // } const int VECTOR_SIZE = 8 ; // Define a template class vector of strings typedef vector<string > StringVector ; //Define an iterator for template class vector of strings typedef StringVector::iterator StringVectorIt ; StringVector NamesVect(VECTOR_SIZE) ; //vector containing names string value("111") ; // stores the value used // to count matching elements StringVectorIt start, end, it ; int result = 0 ; // stores count of elements // that match value. ausgabe = result; // Initialize vector NamesVect NamesVect[0] = a; start = NamesVect.begin() ; // location of first // element of NamesVect end = NamesVect.end() ; // one past the location // last element of NamesVect // print content of NamesVect cout << "NamesVect { " ; for(it = start; it != end; it++) cout << *it << " " ; cout << " }\n" << endl ; // Count the number of elements in the range [first, last +1) // that match value. result = count(start, end, value) ; // print the count of elements that match value // cout << "Number of elements that match \"111\" = " // << result << endl ; } cout << "Im feld vorkommende <111>: " << ausgabe << "\n"; }
<hume sagt>Code-Tags eingefügt</hume sagt>
-
Hi,
also ich hab mal dein Programm so kurz überflogen...
cout << "Im feld vorkommende <111>: " << ausgabe << "\n";
wird - so wie ich sehe - immer 0 ausgeben, da du zuerst
result jedesmal auf 0 setzt,int result = 0 ;
dann result dem int ausgabe zuweist
ausgabe = result;
und danach weist du ausgabe gar keinen Wert mehr zu. daher bleibt der Wert in ausgabe immer 0.
Aber falls es immernoch nicht gehen sollte, kannst ja wieder in count(STL Sample) nachsehen, es sieht deinem Programm so verblüffend ähnlich :p Dann wirst deinen Fehler noch finden...
Gruß Kitty
-
Danke ich werds versuchen.
Hmm, könnte daran liegen, dass es nur eine umgeschriebene Version von count ist
Thx auf alle Fälle mal
-
So, das Programm funktionier jetzt halbwegs, nur hat sich ein anderes Problem ergeben.
Ich schreibe ja den inhalt von einem feld in ein zweites. Dabei checke ich ab ob der inhalt schon im 2ten feld drin ist. Wenn er schon drin ist, erhöhe ich meinen hit counter um 1 und füge den inhalt nicht ins 2te feld ein. Wenn der inhalt nicht drin ist, erhöhe ich meinen miss um 1 und schreibe ihn ins 2te feld.
Problem ist: Wenn was dazu gefügt wird, wird der misses counter für jedes element um 1 erhöht, nicht nur um 1. Wie kann ich das unterbinden?Code:
#include <iostream> #include <algorithm> #include <functional> #include <string> #include <vector> #include <stdlib.h> #include <stdio.h>/* #include "stdafx.h" #include <windows.h> #include <iostream.h> #include <dos.h> #include <windows.h> #include <time.h> */ using namespace std; void main() { string a; string feld[5] = {"111", "111", "010", "111", "110"}; string feld2[5]; int ausgabe; int counter[5]; int misstest; int hittest; int hits = 0; int misses = 0; int y = 0; feld2[y] = feld[0]; for(int x = 1; x<6; x++) { // cout << a << "\n"; // } const int VECTOR_SIZE = 8 ; // Define a template class vector of strings typedef vector<string > StringVector ; //Define an iterator for template class vector of strings typedef StringVector::iterator StringVectorIt ; StringVector NamesVect(VECTOR_SIZE) ; //vector containing names string value(feld[x]) ; // stores the value used // to count matching elements StringVectorIt start, end, it ; //Wenn Result 1 ist, ist das gesuchte schon enthalten!!! int result; // stores count of elements // that match value. // Initialize vector NamesVect for(int lol = 0; lol<5; lol++){ NamesVect[lol] = feld2[lol]; } start = NamesVect.begin() ; // location of first // element of NamesVect end = NamesVect.end() ; // one past the location // last element of NamesVect // print content of NamesVect cout << "NamesVect { " ; for(it = start; it != end; it++) cout << *it << " " ; cout << " }\n" << endl ; // Count the number of elements in the range [first, last +1) // that match value. result = count(start, end, value) ; // print the count of elements that match value // counter[x] = result; if(result == 1){ hits = hits + 1; } else{ misses = misses + 1; feld2[y+1] = feld[x]; y = y+1; } } // cout << "Im feld vorkommende <111>: " << ausgabe << "\n"; cout << "Anzahl der Hits: " << hits << "\n"; cout << "Anzahl des Misses: " << misses << "\n"; }
<hume sagt>Code-Tags eingefügt</hume sagt>
-
und was ist mit codetags ???????
-
häh?
kurz gesagt, ich bin so ziemlich neu in C++ also kenn ich mich net sonderlich aus^^
-
wenn du einen Beitrag erstellst/antwortest, sind unter deinem Eingabefeld so viele Smilys. Dadrunter ist gleich links "C/C++". Da klickste einmal drauf, schreibst deinen C/C++ Code und klickst wieder auf den Button == codetags
-
Okay, gut zu wissen, nur was bringen die Dinger? Und so nebenbei, hat wer ne Idee wie ich es zambring das das Teil mir für jedes mal durchchecken nur einen Miss statt einen für jedes element wos nicht drin vorkommt ausgibt?