bokarinho Δημοσ. 5 Μαρτίου 2008 Δημοσ. 5 Μαρτίου 2008 Χρειάστηκε να γράψω για ένα πρόγραμμα μου μία ανάλογη ρουτίνα με την strtok της C, σε C++ STL για να μπορώ να κάνω parsing σε ένα string και να πάρω τα tokens του, δηλαδή τα κομμάτια του string χωρίς την ύπαρξη των διαφόρων delimiters που βρίσκονται μέσα στο string και διαχωρίζουν τα κομμάτια μεταξύ τους. Παραθέτω την ρουτίνα όπως έχει για μελλοντική χρήση από τους δικούς μας insomniac φίλους μιας και το token parsing είναι δημοφιλές. Κάλλιστα το πρόγραμμα μπορεί να επεκταθεί με μερικές ρουτίνες που διαβάζουν από ένα αρχείο κειμένου γραμμές σε ένα πίνακα από strings και κατόπιν parsing με την ρουτίνα που έχει υλοποιηθεί. Κώδικας: > /* STL strtok.*/ //--------------------------------------------------------------------------- #include <iostream> #include <vector> #include <string> #include <algorithm> using namespace std; //--------------------------------------------------------------------------- /* Token Creation. */ /* Input: A C++ String with special delimiter characters in it. */ /* Output: A string vector with the tokens, i.e the words from the string without the delimiters. */ void STL_strtok(const string &pszStr, vector<string> &strVec, const string &pszDel = " ") { /* Skip any kind of delimiter in order to find the first letter. */ string::size_type startPos = pszStr.find_first_not_of(pszDel, 0); /* From this position find the first delimiter. */ string::size_type finishPos = pszStr.find_first_of(pszDel, startPos); /* Loop while you can not find any letter or any delimiter letter. */ while(string::npos != startPos || string::npos != finishPos) { /* Push the string into the vector array of strings. */ strVec.push_back(pszStr.substr(startPos, finishPos - startPos)); /* Find the position of the next letter. */ startPos = pszStr.find_first_not_of(pszDel, finishPos); /* Find the position of the next delimiter. */ finishPos = pszStr.find_first_of(pszDel, startPos); } } /* Print a vector of strings. */ void PrintStrVector(vector<string> nVec) { /* Create an iterator. */ vector<string>::iterator nIter; /* Loop and print. */ for(nIter = nVec.begin(); nIter != nVec.end(); nIter++) cout << *nIter << endl; cout << endl; } int main(int argc, char* argv[]) { /* Create a vector of strings. */ vector<string> strVec; /* The string to use. */ string pszStr("Me and my monkey the dream and the gun, i am hoping my monkey wont point the gun to anyone"); /* Use the function to tokenize. */ STL_strtok(pszStr, strVec, " ,"); /* Print the results in the screen. */ PrintStrVector(strVec); /* Free memory. */ strVec.~vector<string>(); /* Exit. */ cout << "Hit enter to continue...." << endl; cin.get(); return 0; } //--------------------------------------------------------------------------- Εκτυπωμένα αποτελέσματα: Me and my monkey the dream and the gun i am hoping my monkey wont point the gun to anyone Hit enter to continue.... * Το πρόγραμμα έχει υλοποιηθεί στον CodeGear C++ Builder 2007 της Borland και έχει δοκιμαστεί σε περιβάλλον Windows XP Pro SP2 Greek. ** Το string που γίνεται tokenized είναι οι στίχοι από ένα αγαπημένο μου τραγούδι.
FrAcTaL-gR Δημοσ. 5 Μαρτίου 2008 Δημοσ. 5 Μαρτίου 2008 Ζητώ συγνώμμη για την άγνοια μου, αλλά ήθελα να ρωτήσω κάτι. Τι είναι το "parsing"? Εχω δει τον όρο αυτό και αλλού, αλλά δεν έβγαλα νόημα.
HalfAsleep Δημοσ. 5 Μαρτίου 2008 Δημοσ. 5 Μαρτίου 2008 Ζητώ συγνώμμη για την άγνοια μου, αλλά ήθελα να ρωτήσω κάτι.Τι είναι το "parsing"? Εχω δει τον όρο αυτό και αλλού, αλλά δεν έβγαλα νόημα. Συντακτικη ανάλυση. Βασικα, αναλυεις μια σειρα απο "tokens", ωστε να ελεγξεις την δομη τους στα πλαισια μιας δοθεισας γραμματικης (η βασικη δουλεια που κανει ενας compiler για μια χ γλωσσα).
bokarinho Δημοσ. 7 Μαρτίου 2008 Μέλος Δημοσ. 7 Μαρτίου 2008 Παραθέτω ακόμα μία συνάρτηση που μοιάζει με την STL_strtok που αναλύθηκε προηγουμένως η οποία με την σειρά της κάνει κάτι ανάλογο, κάνει split σε tokens με βάση τον splitCharacter και αποθηκεύει τα tokens σε ένα πίνακα από strings. Προσοχή τον τελεστή της αναφοράς διότι χωρίς αυτόν το πρόγραμμα δεν θα δουλέψει. Αυτό γιατί; Ξέρει να μου πει κανείς; Κώδικας: > /* STL strtok - STL split.*/ //--------------------------------------------------------------------------- #include <iostream> #include <vector> #include <string> #include <algorithm> using namespace std; //--------------------------------------------------------------------------- /* Token Creation. */ /* Input: A C++ String with special delimiter characters in it. */ /* Output: A string vector with the tokens, i.e the words from the string without the delimiters. */ void STL_strtok(const string &pszStr, vector<string> &strVec, const string &pszDel = " ") { /* Skip any kind of delimiter in order to find the first letter. */ string::size_type startPos = pszStr.find_first_not_of(pszDel, 0); /* From this position find the first delimiter. */ string::size_type finishPos = pszStr.find_first_of(pszDel, startPos); /* Loop while you can not find any letter or any delimiter letter. */ while(string::npos != startPos || string::npos != finishPos) { /* Push the string into the vector array of strings. */ strVec.push_back(pszStr.substr(startPos, finishPos - startPos)); /* Find the position of the next letter. */ startPos = pszStr.find_first_not_of(pszDel, finishPos); /* Find the position of the next delimiter. */ finishPos = pszStr.find_first_of(pszDel, startPos); } } /* Print a vector of strings. */ void PrintStrVector(vector<string> nVec) { /* Create an iterator. */ vector<string>::iterator nIter; /* Loop and print. */ for(nIter = nVec.begin(); nIter != nVec.end(); nIter++) cout << *nIter << endl; cout << endl; } /* Split a string into tokens. Split is done by finding the spliting charachter. */ void STL_split(const string &pszStr, const char splitterChar, vector<string> &nVecStr) { /* Variables. */ string::size_type i = 0; string::size_type j = pszStr.find(splitterChar); while(j != string::npos) { /* Push the string into the vector. */ nVecStr.push_back(pszStr.substr(i, j-i)); /* Advance i to the next state. */ i = ++j; /* Find next delimit char. */ j = pszStr.find(splitterChar, j); /* If char is not found copy the left into the vector of strings. */ if(j == string::npos) nVecStr.push_back(pszStr.substr(i, pszStr.length())); } } int main(int argc, char* argv[]) { /* Create a vector of strings. */ vector<string> strVec; /* The string to use. */ string pszStr("Me and my monkey the dream and the gun, i am hoping my monkey wont point the gun to anyone"); string pszInput("Are|you|ready|to|play the game?"); /* Use the function to tokenize. */ /* STL_strtok(pszStr, strVec, " ,"); */ STL_split(pszInput, '|', strVec); /* Print the results in the screen. */ PrintStrVector(strVec); /* Free memory. */ strVec.~vector<string>(); /* Exit. */ cout << "Hit enter to continue...." << endl; cin.get(); return 0; } //--------------------------------------------------------------------------- Αποτελέσματα: Are you ready to play the game? Hit enter to continue....
Προτεινόμενες αναρτήσεις
Αρχειοθετημένο
Αυτό το θέμα έχει αρχειοθετηθεί και είναι κλειστό για περαιτέρω απαντήσεις.