2 unterschiedliche CStrings vergleichen
-
moin,
ich habe ein Problem, ich habe 2 CStrings str1, str2. In beiden CStrings stehen unterschiedliche Kürzel, alphabetisch sortiert, jeweils mit einem ";" als Trenner.
Also beispielsweise:
str1 = abc;def;gh;zyxv;zz
str2 = def;jkl;opq;rst;uv;zzBeide CStrings können unabhängig voneinander verschiedene Kürzel aufweisen, mein Ziel ist nun, die Kürzel zu finden, die in beiden CStrings vorhanden sind. Gibt es hierfür noch eine bessere Möglichkeit, als eine der beiden CStrings anhand der ";" erst zu zerlegen und jeden einzelnen der zerlegten Substring in dem anderen CString suchen zu lassen?
-
ich wuerde beide strings in die kuerzel auftrennen, nach dem alphabet sortieren und dann beide listen vergleichen. hast beim vergleichen am wenigsten aufwand.
Meep Meep
-
ja, so könnte man das machen.
Trööt Trööt
-
Man könnte die Kürzel aber auch gleich mit Hilfe der unique Funktion in eine Liste einfügen, dann kommt jedes Kürzel nur einmal vor.
Balla Balla
-
Wenn die doppelt vokommenden Kürzel nicht interessieren und nicht gespeichert werden, ist deine ( Balla Balla ) Lösung optimal.
Plem Plem
-
Wie soll unique dabei helfen - müsste man den Container ja vorher sortieren...?
unique schrieb:
Removes duplicate elements that are adjacent to each other in a specified range.
Wenn schon mit Containern dann ist hier wohl ein std::set angebracht.
@Trööte: ja, macht ungeheuer Sinn dein Posting.
-
std::string str("def;jkl;opq;rst;uv;zz;"); std::string str_two("abc;def;uv;yxz;ut;uv;"); std::map<std::string, std::size_t> items; for (std::size_t pos = str.find_first_of(';'); pos != std::string::npos; pos = str.find_first_of(';', pos + 1)) { std::string tmp(str.substr(0, pos)); items[tmp] = 0; for (std::size_t pos_found = str_two.find_first_of(tmp); pos_found != std::string::npos; pos = str_two.find_first_of(tmp, pos_found + 1)) ++items[tmp]; str.erase(0, pos); }Sowas?
-
sorry, dein Beitrag hatte ich zu spät gesehen, daher sieht meine Lösung nicht so schön aus
Aber es will nicht so wie ich 
also Ziel ist es, je nach Auftreten der Kürzel in Spalte astrProdStepProps[3] oder [4] den dahinterstehenden Mitarbeiter in der DB abzulegen, wenn das Kürzel, was übringens 2 bis 6 Buchstaben lang sein kann, nur in [3] steht soll eine 0 in die Zeile in der DB geschrieben werden, bei einem Eintrag in Zeile [4] eine 1 und wenn das Kürzel in [3] und [4] vorhanden ist, eine 2, jeweils als letzten Wert bei der entsprechenden VALUES Zeile.
// Copy Plan-Editors to a Help-Array for Both-Editors nStart = 0; if(!astrProdStepProps[3].IsEmpty()) { astrProdStepProps[3] += strLimiter; while (-1 != (nEnd = astrProdStepProps[3].Find(strLimiter,nStart))) { astrEmplPlanHelp[nIndexPlan++] = astrProdStepProps[3].Mid(nStart,nEnd - nStart); nStart = nEnd + strLimiter.GetLength(); } } // Copy Is-Editors to a Help-Array for Both-Editors nStart = 0; if(!astrProdStepProps[4].IsEmpty()) { astrProdStepProps[4] += strLimiter; while (-1 != (nEnd = astrProdStepProps[4].Find(strLimiter,nStart))) { astrEmplIsHelp[nIndexIs++] = astrProdStepProps[4].Mid(nStart,nEnd - nStart); nStart = nEnd + strLimiter.GetLength(); } } nStart = 0; if(!astrProdStepProps[3].IsEmpty()) { // astrProdStepProps[3] += strLimiter; while (-1 != (nEnd = astrProdStepProps[3].Find(strLimiter,nStart))) { for (nIndex=0; nIndex <= nIndexIs; nIndex++) { if (astrProdStepProps[3].Find(astrEmplIsHelp[nIndex], 0) != -1) { astrProdStepProps[3].Delete(nStart,nEnd - nStart+1); astrEmplBothHelp[nIndexBoth++] = astrEmplIsHelp[nIndex]; } } nStart = nEnd + strLimiter.GetLength(); } } nStart = 0; if(!astrProdStepProps[4].IsEmpty()) { // astrProdStepProps[4] += strLimiter; while (-1 != (nEnd = astrProdStepProps[4].Find(strLimiter,nStart))) { for (nIndex=0; nIndex <= nIndexPlan; nIndex++) { if (astrProdStepProps[4].Find(astrEmplPlanHelp[nIndex], 0) != -1) astrProdStepProps[4].Delete(nStart,nEnd - nStart+1); } nStart = nEnd + strLimiter.GetLength(); } } nStart = 0; if(astrProdStepProps[3].GetLength() > 1) { // astrProdStepProps[3] += strLimiter; while (-1 != (nEnd = astrProdStepProps[3].Find(strLimiter,nStart))) { // find the id of the qualification CString strIdPerson = sqlQuery.getPersonByToken(CSqlConnection::GetSqlDb(), astrProdStepProps[3].Mid(nStart,nEnd - nStart)); // insert a new record for the employee strSqlStat ="INSERT INTO tab_bearbeiterfertigungsschritt "; strSqlStat += "VALUES("+ astrProdStepProps[7] +", " + strIdPerson + ", 0);"; // execute Insert pdbSql->ExecuteSQL(strSqlStat); nStart = nEnd + strLimiter.GetLength(); } } nStart = 0; if(astrProdStepProps[4].GetLength() > 1) { // astrProdStepProps[4] += strLimiter; while (-1 != (nEnd = astrProdStepProps[4].Find(strLimiter,nStart))) { // find the id of the qualification CString strIdPerson = sqlQuery.getPersonByToken(CSqlConnection::GetSqlDb(), astrProdStepProps[4].Mid(nStart,nEnd - nStart)); // insert a new record for the employee strSqlStat ="INSERT INTO tab_bearbeiterfertigungsschritt "; strSqlStat += "VALUES("+ astrProdStepProps[7] +", " + strIdPerson + ", 1);"; // execute Insert pdbSql->ExecuteSQL(strSqlStat); nStart = nEnd + strLimiter.GetLength(); } } for (nIndex=0; nIndex <= nIndexBoth; nIndex++) { // find the id of the qualification CString strIdPerson = sqlQuery.getPersonByToken(CSqlConnection::GetSqlDb(), astrEmplBothHelp[nIndex]); // insert a new record for the employee strSqlStat ="INSERT INTO tab_bearbeiterfertigungsschritt "; strSqlStat += "VALUES("+ astrProdStepProps[7] +", " + strIdPerson + ", 2);"; // execute Insert pdbSql->ExecuteSQL(strSqlStat); }Im strLimiter steht das ";".
Sieht irgendwer einen logischen Fehler? Oder hätte einen Denkansatz? Ich habe mir das eigentlich nicht so schwer vorgestellt und so schwer kann das doch auch gar nicht sein ...
-
Alter Schwede ... Herbie... dein Code ist echt schwer lesbar. Änder das mal
// hier kannst du bestimmt auch CStringArray benutzen CString PoductionStep[4]; // ehemals astrProdStepProps[3] CString PlanHelp[4]; // ehemals astrEmplPlanHelp[4] (was auch immer das bedeuten soll) CString IsHelp[4]; // ehemals astrEmplIsHelp CString Limiter = ";"; // strLimiter //... // Copy Plan-Editors to a Help-Array for Both-Editors int Index = 0; int Start = 0; int End = 0; if(!PoductionStep[3].IsEmpty()) { PoductionStep[3] += Limiter; End = PoductionStep[3].Find(Limiter,nStart); while (End != -1) //(-1 != (nEnd = astrProdStepProps[3].Find(strLimiter,nStart))) { Index++; Temp[Index] = PoductionStep[3].Mid(Start,End - Start); Start = End + Limiter.GetLength(); End = PoductionStep[3].Find(Limiter,nStart); } } // Copy Is-Editors to a Help-Array for Both-Editors Start = 0; if(!PoductionStep[4].IsEmpty()) { PoductionStep[4] += strLimiter; End = PoductionStep[4].Find(Limiter, Start); while (End != -1) { Index++; IsHelp[Index] = PoductionStep[4].Mid(Start,End - Start); Start = End + Limiter.GetLength(); End = PoductionStep[4].Find(Limiter, Start); } } // ...