| | |
Short code - it works on VC++ Express but not on Bloodshed!!
Please support our C++ advertiser: Intel Parallel Studio Home
![]() |
c++ Syntax (Toggle Plain Text)
#include <cstdlib> #include <iostream> #include <string> #include <algorithm> using namespace std; int main() { string foo = "123"; if(count_if(foo.begin(), foo.end(), isdigit) == foo.size()) { cout << "\nGreat!\n"; } cin.get() }
Does not work on bloodshed v4.9.9.2!!!
It outputs the following error:
•
•
•
•
no matching function for call to `count_if(__gnu_cxx::__normal_iterator<char*, std::basic_string<char, std::char_traits<char>, std::allocator<char> > >, __gnu_cxx::__normal_iterator<char*, std::basic_string<char, std::char_traits<char>, std::allocator<char> > >, <unknown type>)'
Last edited by fesago90; Jan 14th, 2007 at 9:12 pm.
http://www.ulteo.com
Ulteo - Taste a bit of freedom
Ulteo - Taste a bit of freedom
I'm surprised that it even compiled on VC++ with a mistake like this:
You also might want to read this:
http://www.sgi.com/tech/stl/count_if.html
I'm using g++, and it supports neither
C++ Syntax (Toggle Plain Text)
cin.get() }
http://www.sgi.com/tech/stl/count_if.html
•
•
•
•
The new count interface uses the iterator_traits class, which relies on a C++ feature known as partial specialization. Many of today's compilers don't implement the complete standard; in particular, many compilers do not support partial specialization. If your compiler does not support partial specialization, then you will not be able to use the newer version of count, or any other STL components that involve iterator_traits.
count() nor count_if(). "Technological progress is like an axe in the hands of a pathological criminal."
All my posts may be freely redistributed under the terms of the MIT license.
All my posts may be freely redistributed under the terms of the MIT license.
•
•
•
•
c++ Syntax (Toggle Plain Text)
#include <cstdlib> #include <iostream> #include <string> #include <algorithm> using namespace std; int main() { string foo = "123"; if(count_if(foo.begin(), foo.end(), isdigit) == foo.size()) { cout << "\nGreat!\n"; } cin.get() }
Does not work on bloodshed v4.9.9.2!!!
It outputs the following error:
But it does work on VC++ Express 2005... (obviously including stdafx.h)
C++ Syntax (Toggle Plain Text)
#include <cstdlib> #include <iostream> #include <string> #include <algorithm> using namespace std; struct predicate { int operator()( int x ) { return isdigit( x ); } }; int main() { string foo = "123"; if(count_if(foo.begin(), foo.end(), predicate()) == foo.size()) { cout << "\nGreat!\n"; } cin.get(); }
Last edited by Ravalon; Jan 14th, 2007 at 9:35 pm.
It's hard to be humble when you're as gifted as I am at pretending to be an expert.
•
•
•
•
I'm surprised that it even compiled on VC++ with a mistake like this:
You also might want to read this:C++ Syntax (Toggle Plain Text)
cin.get() }
http://www.sgi.com/tech/stl/count_if.html
I'm using g++, and it supports neithercount()norcount_if().
That was my mistake when pasting the code and editing the code tags, sorry for any confusion
.And yes, that code works, mind to explain the structure please?
http://www.ulteo.com
Ulteo - Taste a bit of freedom
Ulteo - Taste a bit of freedom
•
•
•
•
And yes, that code works, mind to explain the structure please?
If your compiler declares a library function with C linkage, like isdigit() in this case, you can't use it as a template argument type because functions with C++ linkage are expected. The linkage difference includes things like name mangling that make a function with C linkage hard to use safely, so I guess they just don't allow it.
My solution was to create a function object with guaranteed C++ linkage and have it return a call to isdigit(). There's nothing special about a structure, anything that forces C++ linkage to a function-like thing works just as well. You could also do it with just a regular C++ function for example.
C++ Syntax (Toggle Plain Text)
#include <cstdlib> #include <iostream> #include <string> #include <algorithm> using namespace std; int predicate( int x ) { return isdigit( x ); } int main() { string foo = "123"; if(count_if(foo.begin(), foo.end(), predicate) == foo.size()) { cout << "\nGreat!\n"; } cin.get(); }

Oh, and isdigit() is declared in <cctype>, not <cstdlib>.
It's hard to be humble when you're as gifted as I am at pretending to be an expert.
![]() |
Similar Threads
- C++ code help!!! (C++)
- ASP Code Works on Local 127.0.0.1 but Not on ISP's Server (ASP)
- URGENT: Need help on I/O code! (Java)
Other Threads in the C++ Forum
- Previous Thread: c++ dvd shop
- Next Thread: Setprecision help please
| Thread Tools | Search this Thread |
Tag cloud for C++
api application array arrays based beginner binary bmp c++ c/c++ calculator char char* class classes code coding compile compiler console conversion convert count data database delete deploy developer display dll dynamiccharacterarray email encryption error file format forms fstream function functions game generator givemetehcodez graph homeworkhelp iamthwee ifstream image input int java lib list loop looping loops map math matrix memory multiple newbie news number numbertoword output pointer problem program programming project python random read recursion recursive reference return rpg simple sorting spoonfeeding string strings struct template templates text tree url variable vector video visual visualstudio win32 windows winsock wordfrequency wxwidgets






