removing whitspaces with STL algorithms

This is a discussion on removing whitspaces with STL algorithms within the C++ Programming forums, part of the General Programming Boards category; Hello friends, I'm refreshing my knowleadge about stl containers and algorithms and learning new things. I came accross a simple ...

  1. #1
    Registered User Micko's Avatar
    Join Date
    Nov 2003
    Posts
    712

    removing whitspaces with STL algorithms

    Hello friends,
    I'm refreshing my knowleadge about stl containers and algorithms and
    learning new things.
    I came accross a simple but interesting problem. Say, I need to remove whitespace
    characters from string. Here's my first attempt:
    Code:
    #include <iostream>
    #include <string> 
    #include <algorithm>
    #include <cctype>
    
    using namespace std;
    
    int main ( )
    {
    	string str = "ABC ND001";
    	remove_if ( str.begin ( ), str.end ( ), isspace );
    
    	cout << str << endl;
    	
    }
    if you execute this code youll see that result is: ABCND0011 which is not what I want
    because ND001 is simply shifted to the left and last 1 stayed in string.

    Now my solution to this problem is this:
    Code:
    #include <iostream>
    #include <string> 
    #include <algorithm>
    #include <iterator>
    #include <cctype>
    
    using namespace std;
    
    int main ( )
    {
    	string str = "ABC ND001";
    	string res;
    	remove_copy_if ( str.begin ( ), str.end ( ), back_inserter (res), isspace );
    
    	cout << res << endl;
    	
    }
    Now this is what I want.
    However, this solution requires iterator library (back_inserter), another string to
    store result and copying elements, so it's more problematic regarding performance.
    I wonder if there is more elegant way to do this...
    If someone has any suggestion, I would appreciate it.
    Thanks
    Gotta love the "please fix this for me, but I'm not going to tell you which functions we're allowed to use" posts.
    It's like teaching people to walk by first breaking their legs - muppet teachers! - Salem

  2. #2
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    21,711
    You do indeed want to use std::remove_if, but you also want to erase the elements that have been 'removed'.

    Code:
    #include <algorithm>
    #include <string>
    #include <cctype>
    #include <iostream>
    
    int main()
    {
    	std::string str = "ABC ND001";
    	str.erase(std::remove_if(str.begin(), str.end(), isspace), str.end());
    	std::cout << str << std::endl;
    }
    C + C++ Compiler: MinGW port of GCC
    Version Control System: Bazaar

    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  3. #3
    Registered User Micko's Avatar
    Join Date
    Nov 2003
    Posts
    712
    Interesting solution, thanks laserlight!
    Gotta love the "please fix this for me, but I'm not going to tell you which functions we're allowed to use" posts.
    It's like teaching people to walk by first breaking their legs - muppet teachers! - Salem

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Binary Search Trees Part III
    By Prelude in forum A Brief History of Cprogramming.com
    Replies: 16
    Last Post: 10-02-2004, 03:00 PM
  2. im extreamly new help
    By rigo305 in forum C++ Programming
    Replies: 27
    Last Post: 04-23-2004, 11:22 PM
  3. Prime Number Generator... Help !?!!
    By Halo in forum C++ Programming
    Replies: 9
    Last Post: 10-20-2003, 07:26 PM
  4. include question
    By Wanted420 in forum C++ Programming
    Replies: 8
    Last Post: 10-17-2003, 03:49 AM
  5. STL algorithms
    By PJYelton in forum C++ Programming
    Replies: 7
    Last Post: 11-12-2002, 02:27 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21