Empty verification with chain split

advertisements
vector<string> SplitString (string aString,char *sep)
{
  vector<string> vec;
  char * cstr,*val,*p;
  string str = aString;
  cstr = new char [str.size()+1];
  strcpy (cstr, str.c_str());
  p=strtok (cstr,sep);
  while(p!=NULL)
  {
    vec.push_back(p);
    p=strtok(NULL,sep);

  }delete[] cstr;return vec; }

This is my code to string split. I sent the below string to split with separator '&'

"f0=fname0&l0=lname0&f1=fname1&l1=lname1&f2=fname2&l2=lname2&f3=&l3=".

I got result in the vector as below.

f0=fname0 l0=lname0 f1=fname1 l1=lname1 f2=fname2 l2=lname2 f3= l3=

Now I again the sent the resulted strings with separator '='. Its working fine with "l2=lname2". But for "f3=" and "l3=" My separator at last position of the string. So I couldn't find the value is null or not . I want to know whether the value ( left side of '=' is name and right side one is value ) is empty or not. How can i check this.


How about boost? I think it is easiest way to split string.

#include <algorithm>
#include <iterator>
#include <boost/algorithm/string.hpp>
using namespace std;
using namespace boost;

int main()
{
    string s("f0=fname0&l0=lname0&f1=fname1&l1=lname1&f2=fname2&l2=lname2&f3=&l3=");

    vector<string> v1;
    split(v1, s, is_any_of("&"), token_compress_on);
    copy(v1.begin(), v1.end(), ostream_iterator<string>(cout, "\n"));    

    for (auto i : v1) {
        vector<string> v2;
        split(v2, i, is_any_of("="), token_compress_on); 

        copy(v2.begin(), v2.end(), ostream_iterator<string>(cout, "\n===\n"));
    }
}