Original geschrieben von DanDanger
...und da kommen dann solche Sachen an:
"Text1 23 45#Text2 19 23#"
Damit meine Text-Parsing Funktion des vorherigen Posts greift, müsste ich also den String in seine beiden "Teilstrings" (durch '#' getrennt) zerlegen,
Das mußt du nicht,da die Daten in einer geordneten Reihenfolge kommen.
Für Daten der Form n-mal "Text 23 213#",kannst du einfach so parsen
Code:
#include <vector>
#include <iostream>
#include <string>
#include <sstream>
using namespace std;
struct Data
{
string text;
int zahl1,zahl2;
};
int main()
{
string Text = "Text1 23 45#Text2 19 23#Text3 23 34#";
stringstream stream(Text);
char c;
typedef vector<Data> DVEC;
typedef DVEC::const_iterator DVECITER;
DVEC vec;
while(!stream.fail())
{
Data temp;
stream >> temp.text >> temp.zahl1 >> temp.zahl2 >> c;
if(!stream.fail())
vec.push_back(temp);
}
for(DVECITER i = vec.begin();i != vec.end();++i)
cout << i->text << " " << i->zahl1 << " " << i->zahl2 << endl;
}
Aber du kannst den String auch zerlegen
Code:
#include <string>
#include <iostream>
using namespace std;
int main()
{
string Text = "Text1 23 45#Text2 19 23#";
string::size_type pos = Text.find_first_of('#');
string text1 = Text.substr(0,pos + 1);
string text2 = Text.substr(pos + 1,string::npos);
cout << text1 << endl << text2 << endl;
}
Lesezeichen