CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 8 of 8
  1. #1
    Join Date
    Sep 2003
    Posts
    815

    which operation is more efficient?

    I have a long string (char *) and I need to parse
    I'm going to do the parsing on a std:string
    should I do

    string sTmp = myCharString;
    //my parsing (substr mostly)

    or for each parsing operation (have at least 10 of them)
    xxx =((string)myCharString).substr(0,4));

  2. #2
    Join Date
    Dec 2002
    Posts
    47
    I assume you mean the choices are:

    a. determining where each the substring occurs and then extracting it.
    b. repeatedly copying the whole string and then removing the unwanted portions.

    I suggest "a", less heap activity that way.

    -rick

  3. #3
    Join Date
    Sep 2003
    Posts
    815

    not exactly

    choice b is coping once by:
    string sTmp = myCharString;

    and then do the parsing (about 10 statments)

  4. #4
    Join Date
    Dec 2002
    Posts
    47
    So you're just copying the C string into an STL string so you can use the STL string methods?

    How often do you need to copy the string?

    If this is inside a loop, I'd look for something else.

    char *s = "Some,data,I'd,like,to,parse.";

    std::string strSome, strData,strLike;

    strSome.assign(s + 0, s + 4);
    strData.assign(s + 5, s + 9);
    strLine.assign(s + 14, s + 18);

    I don't know what kind of parsing you are doing so I'm just using numeric offsets here.

    -rick

  5. #5
    Join Date
    Sep 2003
    Posts
    815
    I looked at http://www.sgi.com/tech/stl/basic_string.html
    it says there that assugn is Synonym for operator=

    this is what I'm doing:

    option 1:

    for(List::iterator it = pList->begin(); it != pList->end(); it++)
    {
    string sData = (*it)->pszData;//pszData is char *
    //manipulation over sData (mostly subStr)
    //about 10 statments (in each loop)

    }

    option 2:

    for(List::iterator it = pList->begin(); it != pList->end(); it++)
    {
    ((string) (*it)->pszData).substr(....);
    //about 10 times like that in each loop
    }

  6. #6
    Join Date
    Dec 2002
    Posts
    47
    Yikes! Option 1 - that way you only copy the string once per iteration. The compiler might not be smart enough to do common subexression elimination for option 2.

    ... or Option 3:

    Code:
    for(List::iterator it = pList->begin(); it != pList->end(); it++)
    {
      const char *str = (*it)->pszData;
      // perform parsing directly on str, don't use std::string methods.
    }
    -rick
    Last edited by rfmobile; October 12th, 2003 at 07:01 AM.

  7. #7
    Join Date
    Sep 2003
    Posts
    815
    rfmobile

    about option 3:

    how do I do operation like substr?
    How do I do +?

    Thanks

  8. #8
    Join Date
    Dec 2002
    Posts
    47
    Method like substr(int s, int e) accepts indexes but STL string methods like pointers so you can use pointers to first and last character of substring.

    Pass the pointers to the assign method on a new std::string.

    Suppose you were parsing for a substring between two commas ...

    Code:
    std::string strParse;
    std::string strFound;
    
    const char *s = strParse.begin();
    // look for first comma.
    while (s != strParse.end() && *s != ',') s++;
    const char *e = s;
    if (*e == ',') e++;
    // look for second comma.
    while (e != strParse.end() && *e != ',') e++;
    // place text between commas in strFound.
    strFound.assign(s, e);
    -rick

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured