Public paste
Undefined
By: Guest | Date: Oct 6 2010 18:35 | Format: C++ | Expires: never | Size: 1.25 KB | Hits: 790

  1. #include <iostream>
  2. #include <fstream>
  3. #include <string>
  4. #include <vector>
  5.  
  6. using namespace std;
  7.  
  8. vector<string> explode( const string &delimiter, const string &explodeme);
  9.  
  10. int main(int argc, char *argv)
  11. {
  12.              string fileName = "accounts.txt";
  13.              ifstream inFile(fileName.c_str());
  14.              string fileData((istreambuf_iterator < char> (inFile)), istreambuf_iterator < char> ());
  15.              vector<string> v = explode("n", fileData);
  16.              for(int i=0; i<v.size(); i++)
  17.              {cout << v[i] <<endl;}
  18.              cin.get();
  19.              return 0;
  20. }
  21.  
  22.  
  23.  
  24. vector<string> explode( const string &delimiter, const string &str)
  25. {
  26.     vector<string> arr;
  27.  
  28.     int strleng = str.length();
  29.     int delleng = delimiter.length();
  30.     if (delleng==0)
  31.         return arr;//no change
  32.  
  33.     int i=0;
  34.     int k=0;
  35.     while( i<strleng )
  36.     {
  37.         int j=0;
  38.         while (i+j<strleng && j<delleng && str[i+j]==delimiter[j])
  39.             j++;
  40.         if (j==delleng)//found delimiter
  41.         {
  42.             arr.push_back(  str.substr(k, i-k) );
  43.             i+=delleng;
  44.             k=i;
  45.         }
  46.         else
  47.         {
  48.             i++;
  49.         }
  50.     }
  51.     arr.push_back(  str.substr(k, i-k) );
  52.     return arr;
  53. }