Thread: transform help!!!

  1. #1
    Registered User
    Join Date
    Dec 2005
    Posts
    50

    Unhappy transform help!!!

    this maybe to easy for some of you but i want to do transform this string:

    this_is_a_string
    to this...

    this is a string
    i want the underscores to be replaced by spaces and vice versa... i want also to use transform stl (although lately stls hates me )
    for this to work but i seem not to do it quite right... i know that transform needs unary operators so i created one but it give me trouble because it needs a argument... here is my code

    my unary_operators
    Code:
        //unary functions.. for transforming  space to _
        struct trans_under : std::unary_function<char , char>
        {
            char operator () (char & in) const
            {
                if (in == '_'){
                    return ' ';
                }else{
                    return in;
                }
            }
        }tunder;
    
        //unary functions.. for transforming _ to space
        struct trans_space : std::unary_function<char , char>
        {
            char operator () (char & in) const
            {
                if (in == ' '){
                    return '_';
                }else{
                    return in;
                }
            }
        }tspace;
    and here is my transform...

    Code:
                    std::string str;
                    file >> str;
                    
                    //doesn't work because tunder needs parameters??
                    std::transform(str.begin(), str.end(), str.begin(), tunder());
    i look on bunch of stuffs in google and i find nothing with having your own unary operators.. it's always about lower case and upper case.

    please help and more power!

  2. #2
    Registered User
    Join Date
    Dec 2005
    Posts
    50
    i have answered my own question! i think.... i should have done this...
    Code:
    std::transform(str.begin(), str.end(), str.begin(), tunder);
    or this...
    Code:
    std::transform(str.begin(), str.end(), str.begin(), trans_under());
    i should pay more attention to the tutorials...

    now could anyone will be kind enough to explain to me why is this so? i thought that when i did tunder() i am invoking my overloaded operator()...

    and with regards with the solution, which one would be the better?

    thanks again! (you know, i might never find out the answer this quickly if i never tried to make this thread )

  3. #3
    Super Moderater.
    Join Date
    Jan 2005
    Posts
    374
    You don't really need to use transform()

    There are much simpler ways...

    http://www.cprogramming.com/tutorial/string.html

    Try using my_string.replace()

  4. #4
    Supermassive black hole cboard_member's Avatar
    Join Date
    Jul 2005
    Posts
    1,709
    If you're really crazy you could totally ignore the fact that there is a standard library solution and hack it together with char buffers. I've done it a few times.

    Now let's never speak of this again.
    Good class architecture is not like a Swiss Army Knife; it should be more like a well balanced throwing knife.

    - Mike McShaffry

  5. #5
    Registered User hk_mp5kpdw's Avatar
    Join Date
    Jan 2002
    Location
    Northern Virginia/Washington DC Metropolitan Area
    Posts
    3,817
    There's also replace_if:

    Code:
    #include <iostream>
    #include <string>
    #include <functional>
    #include <algorithm>
    using namespace std;
    
    int main()
    {
        string str = "This_is_a_string";
    
        cout << str << endl;
    
        replace_if(str.begin(),str.end(),bind1st(equal_to<char>(),'_'),' ');
    
        cout << str << endl;
    
        return 0;
    }
    Output:
    Code:
    This_is_a_string
    This is a string
    [edit]
    You can even use for_each:
    Code:
    #include <iostream>
    #include <string>
    #include <functional>
    #include <algorithm>
    using namespace std;
    
    template<char char_to_find,char char_to_replace>
    class replaceit
    {
    public:
        operator()(char& c)
        {
            if( c == char_to_find ) c = char_to_replace;
        }
    };
    
    
    int main()
    {
        string str = "This_is_a_string";
    
        cout << str << endl;
    
        for_each(str.begin(),str.end(),replaceit<'_',' '>());
    
        cout << str << endl;
    
        return 0;
    }
    Should output the same thing.
    [/edit]
    Last edited by hk_mp5kpdw; 01-16-2006 at 12:59 PM.
    "Owners of dogs will have noticed that, if you provide them with food and water and shelter and affection, they will think you are god. Whereas owners of cats are compelled to realize that, if you provide them with food and water and shelter and affection, they draw the conclusion that they are gods."
    -Christopher Hitchens

  6. #6
    Registered User
    Join Date
    Dec 2005
    Posts
    50
    i tried looking at string.replace() it seems that i may have a rather harder time to implement it.

    i don't like to use char buffers so i do it using STL.

    i never knew replace_if until now. i think it's good from what i have read.

    thanks!

  7. #7
    semi-colon generator ChaosEngine's Avatar
    Join Date
    Sep 2005
    Location
    Chch, NZ
    Posts
    597
    Quote Originally Posted by hk_mp5kpdw
    There's also replace_if:


    [edit]
    You can even use for_each:
    Code:
    #include <iostream>
    #include <string>
    #include <functional>
    #include <algorithm>
    using namespace std;
    
    template<char char_to_find,char char_to_replace>
    class replaceit
    {
    public:
        operator()(char& c)
        {
            if( c == char_to_find ) c = char_to_replace;
        }
    };
    
    
    int main()
    {
        string str = "This_is_a_string";
    
        cout << str << endl;
    
        for_each(str.begin(),str.end(),replaceit<'_',' '>());
    
        cout << str << endl;
    
        return 0;
    }
    Should output the same thing.
    [/edit]
    just to be pedantic, that's technically incorrect. the standard dictates that for_each should be a non mutating operation, but I don't know any implementation that actually enforces this, probably because it's a stupid idea.
    "I saw a sign that said 'Drink Canada Dry', so I started"
    -- Brendan Behan

    Free Compiler: Visual C++ 2005 Express
    If you program in C++, you need Boost. You should also know how to use the Standard Library (STL). Want to make games? After reading this, I don't like WxWidgets anymore. Want to add some scripting to your App?

  8. #8
    Registered User
    Join Date
    Dec 2005
    Posts
    50
    i learn i could also do this
    Code:
    std::replace(str.begin(), str.end(), '_', ' ');

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. 2d Cubik program in C
    By joot43 in forum C Programming
    Replies: 4
    Last Post: 05-25-2009, 07:17 AM
  2. Using Opengl to transform images
    By Cogman in forum Game Programming
    Replies: 2
    Last Post: 01-16-2009, 06:19 PM
  3. how to transform integer to char..
    By transgalactic2 in forum C Programming
    Replies: 7
    Last Post: 01-09-2009, 08:34 AM
  4. Fourier Transform
    By srikanthreddy in forum C++ Programming
    Replies: 2
    Last Post: 04-09-2005, 08:36 AM
  5. fourier transform in C
    By blinky in forum C Programming
    Replies: 4
    Last Post: 02-25-2004, 12:27 PM