Remove puncuation from strings in a set?

This is a discussion on Remove puncuation from strings in a set? within the C++ Programming forums, part of the General Programming Boards category; After searching the board I found a nifty way to remove punctuation from a string str.erase(std::remove_if(str.begin(),str.end(),isp unct),str.end()); I've been trying ...

  1. #1
    Slime Dragoon_42's Avatar
    Join Date
    Feb 2003
    Location
    Vancouver
    Posts
    90

    Remove puncuation from strings in a set?

    After searching the board I found a nifty way to remove punctuation from a string

    str.erase(std::remove_if(str.begin(),str.end(),isp unct),str.end());

    I've been trying to adap this to strings in a set:

    Code:
    std::set<string>::iterator itr=Words.begin();
    	while(itr!=Words.end())
    	{
    		*itr.erase(std::remove_if(*itr.begin(),*itr.end(),ispunct),*itr.end());
    		itr++;
    	}
    but I get the following errors:
    biblio.cpp: In function `int main()':
    biblio.cpp:33: error: `begin' undeclared (first use this function)
    biblio.cpp:33: error: (Each undeclared identifier is reported only once for
    each function it appears in.)
    biblio.cpp:33: error: `end' undeclared (first use this function)
    biblio.cpp:33: error: `erase' undeclared (first use this function)

    What am I doing wrong?

  2. #2
    Code Goddess Prelude's Avatar
    Join Date
    Sep 2001
    Posts
    9,796
    Member access binds more tightly than indirection. Use parentheses to get what you want:
    Code:
    (*itr).erase(std::remove_if((*itr).begin(),(*itr).end(),ispunct),(*itr).end());
    Or, if your iterator is smart:
    Code:
    itr->erase(std::remove_if(itr->begin(),itr->end(),ispunct),itr->end());
    Last edited by Prelude; 02-29-2004 at 03:30 PM.
    My best code is written with the delete key.

  3. #3
    Slime Dragoon_42's Avatar
    Join Date
    Feb 2003
    Location
    Vancouver
    Posts
    90
    That gave me a bunch of horrible looking errors. What if I used:
    Code:
    std::remove_if(std::istream_iterator<string>(fin),std::istream_iterator<string>(),ispunct);
    before I even entered the strings into the set? I'm trying to only use stream iterators in the program.

  4. #4
    Code Goddess Prelude's Avatar
    Join Date
    Sep 2001
    Posts
    9,796
    >That gave me a bunch of horrible looking errors.
    So post the errors and a minimal program that gives you those errors. If you don't thenthe best I can do is try to break code that I write, find similarities, and the fix my problem in the hopes that your problem is the same.

    Let me stress that the program you post should be minimal. I see too many people either posting their entire program (pages of code that I have no interest in), or compress the source file and attach it (I don't download attachments just to find myself in a debugging situation).
    My best code is written with the delete key.

  5. #5
    Slime Dragoon_42's Avatar
    Join Date
    Feb 2003
    Location
    Vancouver
    Posts
    90
    Thanks! My program is minimal to begin with, mostly because I really seem to suck at programming today. What parameters do I need for ispunct()?

    biblio.cpp
    Code:
    #include<iostream>
    #include <fstream>
    #include <set>
    #include<iterator> /*istream_iterator definition*/
    #include<algorithm>
    using std::cout;
    using std::string;
    
    int main()
    {
    	std::ifstream fin;
    	std::set<string> Words;
    	
    	fin.open("Assignment5input.txt");
    	
    	if(fin.fail())
    	{
    		cout<<"Error opening Assignment5input.txt";
    		return -1;
    	}
    	
    	std::copy(std::istream_iterator<string>(fin),std::istream_iterator<string>(),std::inserter(Words,Words.begin()));
    	
    	std::set<string>::iterator itr=Words.begin();
    	while(itr!=Words.end())
    	{
    		itr->erase(std::remove_if(itr->begin(),itr->end(),ispunct()),itr->end());
    		itr++;
    	}
    	return 0;
    }
    Errors:
    c:/djgpp/include/ctype.h: In function `int main()':
    c:/djgpp/include/ctype.h:18: error: too few arguments to function `int
    ispunct(int)'
    biblio.cpp:33: error: at this point in file
    biblio.cpp:33: error: passing `const std::string' as `this' argument of `
    __gnu_cxx::__normal_iterator<typename _Alloc::pointer,
    std::basic_string<_CharT, _Traits, _Alloc> > std::basic_string<_CharT,
    _Traits, _Alloc>::erase(__gnu_cxx::__normal_iterator<typena me
    _Alloc::pointer, std::basic_string<_CharT, _Traits, _Alloc> >,
    __gnu_cxx::__normal_iterator<typename _Alloc::pointer,
    std::basic_string<_CharT, _Traits, _Alloc> >) [with _CharT = char, _Traits =

    std::char_traits<char>, _Alloc = std::allocator<char>]' discards qualifiers
    c:/djgpp/lang/cxx/3.32/bits/stl_algo.h: In function `_RandomAccessIter
    std::find_if(_RandomAccessIter, _RandomAccessIter, _Predicate,
    std::random_access_iterator_tag) [with _RandomAccessIter =
    __gnu_cxx::__normal_iterator<const char*, std::basic_string<char,
    std::char_traits<char>, std::allocator<char> > >, _Predicate = int]':
    c:/djgpp/lang/cxx/3.32/bits/stl_algo.h:318: instantiated from `_InputIter std:
    :find_if(_InputIter, _InputIter, _Predicate) [with _InputIter = __gnu_cxx::__nor
    mal_iterator<const char*, std::basic_string<char, std::char_traits<char>, std::a
    llocator<char> > >, _Predicate = int]'
    c:/djgpp/lang/cxx/3.32/bits/stl_algo.h:1085: instantiated from `_ForwardIter s
    td::remove_if(_ForwardIter, _ForwardIter, _Predicate) [with _ForwardIter = __gnu
    _cxx::__normal_iterator<const char*, std::basic_string<char, std::char_traits<ch
    ar>, std::allocator<char> > >, _Predicate = int]'
    biblio.cpp:33: instantiated from here
    c:/djgpp/lang/cxx/3.32/bits/stl_algo.h:252: error: `__pred' cannot be used as a

    function
    c:/djgpp/lang/cxx/3.32/bits/stl_algo.h:318: instantiated from `_InputIter std:
    :find_if(_InputIter, _InputIter, _Predicate) [with _InputIter = __gnu_cxx::__nor
    mal_iterator<const char*, std::basic_string<char, std::char_traits<char>, std::a
    llocator<char> > >, _Predicate = int]'
    c:/djgpp/lang/cxx/3.32/bits/stl_algo.h:1085: instantiated from `_ForwardIter s
    td::remove_if(_ForwardIter, _ForwardIter, _Predicate) [with _ForwardIter = __gnu
    _cxx::__normal_iterator<const char*, std::basic_string<char, std::char_traits<ch
    ar>, std::allocator<char> > >, _Predicate = int]'
    biblio.cpp:33: instantiated from here
    c:/djgpp/lang/cxx/3.32/bits/stl_algo.h:255: error: `__pred' cannot be used as a

    function
    c:/djgpp/lang/cxx/3.32/bits/stl_algo.h:258: error: `__pred' cannot be used as a

    function
    c:/djgpp/lang/cxx/3.32/bits/stl_algo.h:261: error: `__pred' cannot be used as a

    function
    c:/djgpp/lang/cxx/3.32/bits/stl_algo.h:267: error: `__pred' cannot be used as a

    function
    c:/djgpp/lang/cxx/3.32/bits/stl_algo.h:270: error: `__pred' cannot be used as a

    function
    c:/djgpp/lang/cxx/3.32/bits/stl_algo.h:273: error: `__pred' cannot be used as a

    function
    c:/djgpp/lang/cxx/3.32/bits/stl_algo.h: In function `_OutputIter
    std::remove_copy_if(_InputIter, _InputIter, _OutputIter, _Predicate) [with
    _InputIter = __gnu_cxx::__normal_iterator<const char*,
    std::basic_string<char, std::char_traits<char>, std::allocator<char> > >,
    _OutputIter = __gnu_cxx::__normal_iterator<const char*,
    std::basic_string<char, std::char_traits<char>, std::allocator<char> > >,
    _Predicate = int]':
    c:/djgpp/lang/cxx/3.32/bits/stl_algo.h:1087: instantiated from `_ForwardIter s
    td::remove_if(_ForwardIter, _ForwardIter, _Predicate) [with _ForwardIter = __gnu
    _cxx::__normal_iterator<const char*, std::basic_string<char, std::char_traits<ch
    ar>, std::allocator<char> > >, _Predicate = int]'
    biblio.cpp:33: instantiated from here
    c:/djgpp/lang/cxx/3.32/bits/stl_algo.h:1018: error: `__pred' cannot be used as
    a function
    c:/djgpp/lang/cxx/3.32/bits/stl_algo.h:1019: error: assignment of read-only
    location
    c:/djgpp/lang/cxx/3.32/bits/stl_iterator.h: In constructor `
    __gnu_cxx::__normal_iterator<_Iterator, _Container>::__normal_iterator(const

    __gnu_cxx::__normal_iterator<_Iter, _Container>&) [with _Iter = const char*,

    _Iterator = char*, _Container = std::basic_string<char,
    std::char_traits<char>, std::allocator<char> >]':
    biblio.cpp:33: instantiated from here
    c:/djgpp/lang/cxx/3.32/bits/stl_iterator.h:598: error: invalid conversion from
    `const char* const' to `char*'

  6. #6
    Code Goddess Prelude's Avatar
    Join Date
    Sep 2001
    Posts
    9,796
    Two problems. First, you don't include <string>. Second, you try to call ispunct instead of pass it to a function. To pass the address of a function as an argument, don't use the function call operator, just the function name:
    Code:
    #include <iostream>
    #include <fstream>
    #include <set>
    #include <iterator> /*istream_iterator definition*/
    #include <algorithm>
    #include <string>
    using std::cout;
    using std::string;
    
    int main()
    {
    	std::ifstream fin;
    	std::set<string> Words;
    	
    	fin.open("Assignment5input.txt");
    	
    	if(fin.fail())
    	{
    		cout<<"Error opening Assignment5input.txt";
    		return -1;
    	}
    	
    	std::copy(std::istream_iterator<string>(fin),std::istream_iterator<string>(),std::inserter(Words,Words.begin()));
    	
    	std::set<string>::iterator itr=Words.begin();
    	while(itr!=Words.end())
    	{
    		itr->erase(std::remove_if(itr->begin(),itr->end(),ispunct),itr->end());
    		itr++;
    	}
    	return 0;
    }
    My best code is written with the delete key.

  7. #7
    Slime Dragoon_42's Avatar
    Join Date
    Feb 2003
    Location
    Vancouver
    Posts
    90
    That gives me the same errors. Could I jsut use:
    Code:
    std::remove_if(itr->begin(),itr->end(),ispunct);
    instead of:
    Code:
    itr->erase(std::remove_if(itr->begin(),itr->end(),ispunct),itr->end());

  8. #8
    Code Goddess Prelude's Avatar
    Join Date
    Sep 2001
    Posts
    9,796
    I can't fix a problem I can't reproduce. Sorry. Maybe someone who has access to Dev-C++ will be able to help you.
    My best code is written with the delete key.

  9. #9
    Slime Dragoon_42's Avatar
    Join Date
    Feb 2003
    Location
    Vancouver
    Posts
    90
    Well, thank you for all your help! Maybe being the odd one out using DJGPP isn't really a good thing. Thanks for all your time!

  10. #10
    Registered User manofsteel972's Avatar
    Join Date
    Mar 2004
    Posts
    317

    ispunct doesn't take 0 parameters

    You don't pass any data to ispunct() it takes an int as an argument

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. C help for network animator
    By fastshadow in forum Tech Board
    Replies: 7
    Last Post: 03-17-2006, 02:44 AM
  2. Find all possible subset of a given set
    By Downnin in forum C++ Programming
    Replies: 7
    Last Post: 11-09-2005, 01:03 PM
  3. My graphics library
    By stupid_mutt in forum C Programming
    Replies: 3
    Last Post: 11-26-2001, 05:05 PM

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