Ich versuche, dieses Problem zu lösen:Wie komme ich aus einer Schleife in C++?
Schreiben Sie ein Programm, um zu zählen, wie oft jedes einzelne Wort in seiner Eingabe erscheint.
Dies ist der Code so weit:
#include <algorithm>
#include <iomanip>
#include <ios>
#include <iostream>
#include <string>
#include <vector>
#include <iterator>
using std::cin;
using std::sort;
using std::cout;
using std::streamsize;
using std::endl;
using std::string;
using std::setprecision;
using std::vector;
int main()
{
cout << "Enter words: ";
vector<string> lista;
vector<string> listaCitita;
string x;
while(cin >> x)
{
lista.push_back(x);
}
int listaSize = lista.size();
for(int i = 0; i <= listaSize -1; i++)
{
int x = 0;
int counter = 0;
vector<string>::iterator it = find(listaCitita.begin(), listaCitita.end(), lista[i]);
vector<string>::iterator itu = find(lista.begin(), lista.end(), lista[i]);
if(it != listaCitita.end())
{
break;
}
while(x <= listaSize -1)
{
if(lista[i] == lista[x])
{
counter++;
x++;
if(itu != lista.end())
{
}
else
{
listaCitita.push_back(lista[i]);
}
}
else
{
x++;
}
}
cout << "The string: '" << lista[i] << "' appeared " << counter << " times" << endl;
}
return 0;
}
Was ich versuche, das zu tun ist, wenn der es schon, wie oft das Wort zeigte gedruckt wurde, würde es nicht wieder gedruckt werden, wie oft gezeigt. Deshalb habe ich einen zweiten Vektor (listaCitita) gemacht, wo ich die Elemente, die bereits durchlaufen wurden, hinzufüge. Das Problem ist, dass es nicht aus der for-Schleife ausbricht, wenn ich das tue.
if(it != listaCitita.end())
{
break;
}
Es tut mir leid für mein schlechtes Englisch. Danke.
Was lässt Sie denken, dass es nicht von der Schleife bricht? Fügen Sie das Debugging print 'cout << hinzu (listaCitita.end() == it?" It = end ":" it! = End ") << endl;' vor dem 'if (it! = ListaCitita.end()) '. Add cout << "Es bricht!" << endl; 'vor dem' break; '. Füge 'cout <<" nicht brechen hinzu! " << endl' nach dem '}'. Schau was passiert. – user31264
Habe es getan. Es ist nicht gebrochen. – furthergarden
Bitte senden Sie den Auszug aus der Ausgabe. – user31264