Thread: using boost::replace_all( )

  1. #1
    Registered User
    Join Date
    Jul 2013
    Location
    Germany
    Posts
    499

    using boost::replace_all( )

    Code:
    void NameScore::split_string()
    {
        std::vector<std::string> v_split;
        
        for (auto it = name_list.begin(); it!=name_list.end(); ++it)
        {
            boost::replace_all(*it,",");
        }
    }
     void NameScore::sort_data()
    {
        std::sort(name_list.begin(), name_list.end());
    }
    I have read in several places about boost and it just what I need and it is failing. Funny because I have seen it used the same way I am using it in examples.

    I am trying to break apart a mega string.

  2. #2
    Registered User
    Join Date
    Jul 2013
    Location
    Germany
    Posts
    499
    Code:
    void NameScore::split_string()
    {
        for (int i=0; i<name_list.size(); i++)
        {
            if (name_list[i]==" "" "||",")
            {
                name_list[i]="";
            }
        }
    }

  3. #3
    Registered User
    Join Date
    Oct 2006
    Posts
    3,445
    boost::replace_all takes no less than three parameters, not two.

    in your second example, what do you think

    Code:
    if (name_list[i]==" "" "||",")
    will do?
    What can this strange device be?
    When I touch it, it gives forth a sound
    It's got wires that vibrate and give music
    What can this thing be that I found?

  4. #4
    Lurking whiteflags's Avatar
    Join Date
    Apr 2006
    Location
    United States
    Posts
    9,612
    I seem to remember this problem. To be honest I think replace_all() is not the answer; boost::split() is. After all, you just want to tokenize a string of names separated by commas. I see no reason to actually substitute the commas with a different string (including a null string), like replace_all() would do.

  5. #5
    Registered User
    Join Date
    Jul 2013
    Location
    Germany
    Posts
    499
    Here is the issue. Since the string is a mega string the value is 1 so if I call size I get 1. However just to check I put an arbitrary number in there and none of the mega string values changed. Then again how could they if the whole thing is 1. Thing string is made up of 55,000 names in the format of:

    "MARY","PATRICIA","LINDA","BARBARA","ELIZABETH","J ENNIFER","MARIA","SUSAN","MARGARET","DOROTHY","LIS A","NANCY","KAREN","BETTY","HELEN","SANDRA","DONNA ","CAROL","RUTH","SHARON","MICHELLE","LAURA","SARA H","KIMBERLY","DEBORAH","JESSICA","SHIRLEY","CYNTH IA","ANGELA","MELISSA","BRENDA","AMY","ANNA","REBE CCA","VIRGINIA","KATHLEEN","PAMELA","MARTHA","DEBR A","AMANDA","STEPHANIE","CAROLYN","CHRISTINE","MAR IE","JANET","CATHERINE","FRANCES","ANN","JOYCE","D IANE","ALICE","JULIE","HEATHER","TERESA"



  6. #6
    Registered User
    Join Date
    Jul 2013
    Location
    Germany
    Posts
    499
    @whiteflags, yes I posted this issue before and have now gotten around to working on it again.
    Another issue is that is giving me an error and I have the algorithm library
    Last edited by jocdrew21; 04-17-2014 at 02:55 PM.

  7. #7
    Lurking whiteflags's Avatar
    Join Date
    Apr 2006
    Location
    United States
    Posts
    9,612
    So you need to split the string. I'm only saying now that the advice to use boost::replace_all() was bad advice from whoever gave it to you. Use boost::split() or some other method that you were shown.

  8. #8
    Registered User
    Join Date
    Oct 2006
    Posts
    3,445
    a combination of std::stringstream and std::getline always works great for me when I have only a single delimiter.
    What can this strange device be?
    When I touch it, it gives forth a sound
    It's got wires that vibrate and give music
    What can this thing be that I found?

  9. #9
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    Quote Originally Posted by Elkvis View Post
    a combination of std::stringstream and std::getline always works great for me when I have only a single delimiter.
    But boost is more convenient
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  10. #10
    Lurking whiteflags's Avatar
    Join Date
    Apr 2006
    Location
    United States
    Posts
    9,612
    Quote Originally Posted by Elysia View Post
    But boost is more convenient
    I dunno, in this instance I really think all options are pretty equal.

  11. #11
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    What, you'd rather write 4 lines instead of 2? It's also more expressive, conveying at a glance what it is doing.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  12. #12
    Registered User
    Join Date
    Jul 2013
    Location
    Germany
    Posts
    499
    Convenient? I have been trying to figure out how to get boost into my library. I am using Xcode 5. I have Visual Studio too but I don't really use it, except for assembly language.

    However I will say it is a very good compiler.

    I tried using stringstream but that was giving me issues too.

  13. #13
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    Are you implying you cannot get code using boost::replace_all/boost::split to work? If so, post your attempts.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  14. #14
    Registered User
    Join Date
    Jul 2013
    Location
    Germany
    Posts
    499
    No my compiler doesn't have the library. Xcode 5

  15. #15
    Lurking whiteflags's Avatar
    Join Date
    Apr 2006
    Location
    United States
    Posts
    9,612
    Did you not understand the pseudocode I gave you in the other thread? What happened when you tried to implement that? Maybe we should concentrate on fixing that.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Do you use Boost?
    By sarah22 in forum C++ Programming
    Replies: 4
    Last Post: 06-04-2010, 02:15 PM
  2. How to set Boost up with MinGW?
    By TriKri in forum Windows Programming
    Replies: 4
    Last Post: 05-07-2010, 05:27 AM
  3. boost optional
    By l2u in forum C++ Programming
    Replies: 0
    Last Post: 09-02-2007, 05:42 PM
  4. boost
    By siavoshkc in forum C++ Programming
    Replies: 14
    Last Post: 08-30-2006, 10:58 PM
  5. erasing elements from a boost::ptr_vector (boost n00b)
    By Marcos in forum C++ Programming
    Replies: 2
    Last Post: 04-04-2006, 12:54 PM