2016-04-20 2 views
-1

(C++) Ich habe einen Vektor von Strings verschiedener "Kategorien". Jede Kategorie ist durch eine "00" -Zeichenkette im Vektor getrennt.Trennen Sie einen Vektor (oder Array) von Strings, die durch eine "00" getrennt sind

So wie:

"Pizza" "IceCream" "Brot" "00" "Windows" "Mac" "Linux" "Unix" "00" "Raining" „Snowing“ „00“ „Wandern“ „00“

ich versuche, herauszufinden, wie man diese Kategorien in einen Vektor von Strings mit alle Kategorie trennen s in einem String wie:

"Pizza IceCream Bread" "Windows Mac Linux Unix" "Raining Snowing" "Wandern"

EDIT: alldoubles ist der Vektor Name und seine Größe ist Totalsize

Versuch: der Versuch, in eine Warteschlange hinzuzufügen, wenn die Zeichenfolge nicht 00 ist

int loc = 0; //counter 
    queue<string> tmp; 
    string cur; 
    while (loc<totalsize) 
    { 
     while (cur != "00") 
     { 
      tmp.push(cur); //add to queue when not 00 
      loc = loc + 1; 
      cur = alldoubles[loc]; 
     } 

     while (tmp.empty()!=false) //unload the queue into a string res 
     {       // after an 00 was found and cout it 
      string res; 
      res = res + tmp.front(); 
      tmp.pop(); 
      cout << "RES " << res << endl; 
     } 
    } 

EDIT: jetzt ich RES und die RES Pizza, die nicht alle Lebensmittel mit diesem Code ist

int loc = 0; 
queue<string> tmp; 
string cur; 
while (loc<totalsize) 
{ 
    while (cur != "00") 
    { 
     tmp.push(cur); 
     cur = alldoubles[loc]; 
     loc = loc+1; 
    } 

    while (!tmp.empty()) 
    { 
     string res; 
     res = res + tmp.front(); 
     tmp.pop(); 
     cout << "RES " << res << endl; 
    } 
} 
+4

uns zeigen, was Sie meinen Versuch –

+0

Ok Ich habe versucht, aber es funktioniert nicht. Es bleibt in einer Endlosschleife stecken und ich habe ein paar Dinge ausprobiert, die nicht funktionierten. – fman

+0

Wo weisen Sie totalsize einen Wert zu? – FCo

Antwort

2
// Example program 
#include <iostream> 
#include <string> 
#include <vector> 
#include <algorithm> 

int main() 
{ 
    std::vector<std::string> vv = { "one", "00", "two", "three", "00", "four" , "five", "six", "00", "seven" }; 

    std::string const splitter("00"); 

    // !!!! add 00 to the end 
    vv.push_back(splitter); 

    auto bb = vv.begin(); 
    auto it = std::find(vv.begin(), vv.end(), splitter); 

    // finds groups and prints them one by one 
    while (it != vv.end()) 
    { 
     std::string accumulator; 
     for(auto it2 = bb; it2 != it; ++it2) 
      accumulator += *it2; 

     bb = ++it; 

     it = std::find(bb, vv.end(), splitter); 
     std::cout << accumulator << std::endl; 
    } 
} 

Diese Drucke:

one 
twothree 
fourfivesix 
seven 
+0

Das ist es! danke – fman

2

Ersetzen Sie while (tmp.empty()!=false) durch while(!tmp.empty()). Zumindest .... dann bekommst du etwas unter cout.

Sie müssen auch cur mit alldouble[0] vor der Verwendung initiieren.

Schließlich sollte res außerhalb der letzten while-Schleife deklariert werden, dann, nachdem die Schleife ausgeführt wird, sollte es enthalten, was Sie erwartet haben. Übrigens, vergessen Sie nicht, Leerzeichen hinzuzufügen, wenn Sie die Elemente zu einer letzten Zeichenfolge verketten.

Es sollte auch möglich sein, res on the fly zu konstruieren, während "00" innerhalb der ersten Schleife überprüft wird. Zweiter while würde dann nicht mehr benötigt werden.

+0

Oh, ich habe jetzt etwas auf cout bekommen. Wie hat das funktioniert? Ich bekomme "RES" – fman

+0

leer gibt wahr, wenn der Container leer ist .... und es ist nie leer in Ihrem Fall ... also solange Bedingung immer falsch ist ... – jpo38

+0

Ihre ursprüngliche Schleife läuft nur wenn 'tmp.empty() ! = false "was ist, wenn' tmp.empty() ''wahr' ist. – FCo

1

Wenn Sie Zugriff auf C++ 11 haben, gibt es einen idiomatischen Weg mit algorithm und iterator aus der Standardbibliothek.

std::vector<std::string> stuff = {"Pizza", "IceCream", "Bread", "00", "Windows", "Linux", "Mac", "00", "Raining", "Snowing", "00"}; 
std::vector<std::vector<std::string>> results; 

//find occurance of "00" 
auto t = std::find(stuff.begin(), stuff.end(), "00"); 
//save previous position for copy starting point 
auto t_old = stuff.begin(); 
while (t != stuff.end()) { 
    //temporary vector to hold the strings in this segment 
    auto accum = std::vector<std::string>((t - t_old)); 
    //copy from last "00" to next "00" into accum 
    std::copy(t_old, t, accum.begin()); 
    //bump copy point to new location of next element after last found "00" 
    t_old = t+1; 
    //find next "00" 
    t = std::find(t+1, stuff.end(), "00"); 
    results.push_back(accum); 
} 

Diese Lösung ist nicht perfekt und nicht versucht, die Fäden zusammen zu verketten, aber es ist eine gute Alternative Ansatz, der viele potentielle Gefahren vermeidet.

1

ist hier eine mögliche Lösung:

int main() 
{ 
    vector<string> vec { "Pizza", "IceCream", "Bread", "00", 
         "Windows", "Mac", "Linux", "Unix", "00", 
         "Raining", "Snowing", "Hiking", "00" }; 

    int count {}; 
    string cat1; 
    string cat2; 
    string cat3; 
    for_each(begin(vec), end(vec), [&](string str) { 
     if (str == "00") { 
      ++count; 
      return; 
     } 
     if (count == 0) { 
      for (auto& c : str) 
       cat1.push_back(c); 
      cat1.push_back(' '); 
     } 
     if (count == 1) { 
      for (auto& c : str) cat2.push_back(c); 
      cat2.push_back(' '); 
     } 
     if (count == 2) { 
      for (auto& c : str) cat3.push_back(c); 
      cat3.push_back(' '); 
     } 
    }); 

    cout << cat1 << '\n'; 
    cout << cat2 << '\n'; 
    cout << cat3 << '\n'; 
} 
+0

Danke für die Hilfe – fman

Verwandte Themen