Thread: removing whitspaces with STL algorithms

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

    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
    28,413
    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;
    }
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    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
    715
    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, 03:27 PM