Ich habe versucht, dieses Problem im Laufe des Monats zu behandeln. Ich muss viele Ersetzungen (über 10 Millionen) in einer großen Zeichenfolge (String ^) durchführen. Auch ich muss es schnell machen. Mein Weg war richtig, aber das Programm lief über 30 Minuten.Wie man viele kleine Änderungen in großen Saiten am schnellsten macht. Visual C++
Problem: Ich habe eine Tabelle mit Änderungen zu tun: [strWas1, strWillBe1, strWas2, strWillBe2, ..., strWas10^7, strWillBe10^7]
. Auch ich habe eine große Schnur, die einige von strWasN
enthalten kann, aber es kann auch something-elsestrWas1
enthalten und ich will es nicht ändern, weil "something-elsestrWas1
" nicht "strWas1
" ist.
Zum Beispiel String ist:
"ich zwei Hunde, drei notdogs, auch dogsikong, 5dogs, -Dogs DOGS, Hunde, Hunde, 33DoGs00."
Jetzt brauche ich alle isolierten "Hunde" von Buchstaben ("Hunde" ist strWas1) zu "Katzen" ("Katzen" ist strWillBe1) zu ändern. Ergebnis sein sollte:
"Ich habe zwei Katzen, drei notdogs, auch dogsikong, 5cats, -cats Katzen, Katzen, Katzen, 33cats00."
Mein letzter Versuch war:
array<String^>^ strArray = gcnew array<String^>(9999999);
strArray[0] = gcnew String("dogs");
strArray[1] = gcnew String("cats");
//...
strArray[9999998] = gcnew String("whatReplace");
strArray[9999999] = gcnew String("newText");
bool found = false;
int index;
bool doThis = true;
String^notAllowed = u8"aąbcćdeęfghijklłmnńoópqrsśtuvwxyzźżAĄBCĆDEĘFGHIJKLŁMNŃOÓPQRSŚTUVWXYZŹŻёйцукенгшщзхъфывапролджэячсмитьбюЁЙЦУКЕНГШЩЗХЪФЫВАПРОЛДЖЭЯЧСМИТЬБЮ";
String^text = u8"I have two dogs, three notdogs, also dogsikong, 5dogs, -dogs. DOGS, Dogs, DoGs, 33DoGs00";
for (int i = 0; i < 9999999; i+=2) {
while (found = text->Contains(strArray[i])) {
index = text->IndexOf(strArray[i]);
MessageBox::Show(index.ToString());
doThis = true;
if (index == 0) {
for (int j = 0; j < notAllowed->Length; j++) {
if (text->Substring(strArray[i]->Length, 1) == notAllowed->Substring(j, 1)) doThis = false;
}
}
else if (text->Length - index - strArray[i]->Length) {
for (int j = 0; j < notAllowed->Length; j++) {
if (text->Substring(index-1, 1) == notAllowed->Substring(j, 1)) doThis = false;
}
}
else {
for (int j = 0; j < notAllowed->Length; j++) {
if ((text->Substring(index - 1, 1) == notAllowed->Substring(j, 1)) || (text->Substring(index+strArray[i]->Length,1)== notAllowed->Substring(j, 1))) doThis = false;
}
}
if (doThis) {
text = text->Substring(0, index) + strArray[i + 1] + text->Substring(index + strArray[i]->Length, text->Length - index - strArray[i]->Length);
}
}
}
Aber das funktioniert für endlos
Neue Version (dank Vlad Feinstein):
array<String^>^ strArray = gcnew array<String^>(10);
strArray[0] = gcnew String("dogs");
strArray[1] = gcnew String("cats");
strArray[2] = gcnew String("dogs");
strArray[3] = gcnew String("cats");
strArray[4] = gcnew String("dogs");
strArray[5] = gcnew String("cats");
strArray[6] = gcnew String("dogs");
strArray[7] = gcnew String("cats");
strArray[8] = gcnew String("dogs");
strArray[9] = gcnew String("cats");
bool found = false;
int index;
bool doThis = true;
String^text = u8"I have two dogs, three notdogs, also dogsikong, 5dogs, -dogs. DOGS, Dogs, DoGs, 33DoGs00";
for (int i = 0; i < 10; i += 2)
{
int index = 0;
while ((index = text->ToLower()->IndexOf(strArray[i]->ToLower(), index)) != -1)
{
doThis = true;
// is there one more char?
if (index + strArray[i]->Length < text->Length)
{
if (Char::IsLetter(text[index+strArray[i]->Length]))
doThis = false;
}
// is there previous char?
if (index > 0)
{
if (Char::IsLetter(text[index - 1]))
doThis = false;
}
if (doThis)
text = text->Substring(0, index) + strArray[i + 1] +
text->Substring(index + strArray[i]->Length);
Debug::WriteLine(text);
index++;
}
}
Natürlich ist es immer noch nicht so schnelle Version. Schnelle Version schrieb David Yaw.
Haben Sie versucht, Gewinde zu verwenden? – perencia
Nein ... Ich habe noch nie von Threads gehört. Ich fange an zu programmieren. Dies ist meine erste größere Anwendung und das größte Problem. –
"_Aber das funktioniert endlos_" Warum iteriert Ihre 'for-Schleife' bis 9999999? –