Click to See Complete Forum and Search --> : C++ stl check/verify function?
tecknophreak
12-12-2003, 05:46 PM
I know there's a transform function in STL, but is there a check sort of function in STL? I want to do something like:
string str("1234TT");
if (!check(str.begin(), str.end(), isdigit()) {
std::cout << "Not all digits" << std::endl;
}
mindcooler
12-13-2003, 12:11 PM
Maybe something like this:
#include <algorithm>
#include <cctype>
#include <iostream>
#include <string>
#include <vector>
bool check_string(const std::string& s);
int main(int /*argc*/, char** /*argv*/)
{
std::vector<std::string> v;
v.push_back("12345");
v.push_back("123a5");
std::vector<std::string>::iterator itr = v.begin();
while(itr != v.end())
{
if(check_string(*itr))
{
std::cout << *itr << " all digits." << std::endl;
}
else
{
std::cout << *itr << " not all digits." << std::endl;
}
++itr;
}
return 0;
}
bool check_string(const std::string& s)
{
size_t digit_count = std::count_if(s.begin(), s.end(), isdigit);
return digit_count == s.length();
}
Couldn't think of a way to do it using only call to standard library functions, but at least this program doesn't use a for loop to check all characters individually. =)
Edit: Minor spelling error
tecknophreak
12-13-2003, 03:07 PM
close enough!! :)
Thanks
709394
12-14-2003, 01:21 PM
@mindcooler,
Is there a difference if I write the iterator this way " itr++" in your while loop?(I believe it should work but I want to know if there are some hidden problems).
I saw in most tutorials, when going through each character of a string, they use ++iteratorVariable. Is this a preference or I have to prefix increment the iterator?
thanx!
Here is an example from: http://www.yolinux.com/TUTORIALS/LinuxTutorialC++STL.html
// Standard Template Library example
#include <iostream>
#include <list>
using namespace std;
// Simple example uses type int
main()
{
list<int> L;
L.push_back(0); // Insert a new element at the end
L.push_front(0); // Insert a new element at the beginning
L.insert(++L.begin(),2); // Insert "2" before position of first argument
// (Place before second argument)
L.push_back(5);
L.push_back(6);
list<int>::iterator i;
for(i=L.begin(); i != L.end(); ++i) cout << *i << " ";
cout << endl;
return 0;
}
bwkaz
12-14-2003, 02:34 PM
I don't think you can overload the postincrement operator...
Or maybe you can only overload both pre- and postincrement with one function? Which would mean that the return value would not be consistent with what postincrement normally does to integers (that is, it increments the value, then returns the value as it was before the increment).
Anyway, if it's one of those reasons, then you shouldn't use postincrement with iterators because either it won't exist (and the compiler might choose something else for you... say casting the iterator to an int if that's valid, which is a Bad Thing), or it'll return the wrong value if you're depending on the return value of it.
Stuka
12-14-2003, 10:15 PM
Actually, both pre- and post-increment/decrement operators may be overloaded. If a compiler doesn't optimize properly, however, the post- versions can invoke unneccessary constructors, which can be very expensive if the class being copied is large. For this reason, the pre- version is preferred for loop indices.
bwkaz
12-15-2003, 08:00 PM
Can they be overloaded separately, or only together? I've only ever been able to use <class>& <class>::operator++() when I do C++, but maybe there's another operator syntax for the postincrement?
I can see what you mean by the postincrement being slow, though. Didn't think of that.
mwinterberg
12-16-2003, 12:57 AM
bwkaz -- the postincrement/decrement operators just take an int as a parameter (never used) to differentiate itself from the "pre" versions.
Some of the iterator's on SGI's STL site have the function headers listed for both the pre and post increment/decrement operators, such as: http://www.sgi.com/tech/stl/ReverseBidirectionalIterator.html .
-Michael
bwkaz
12-16-2003, 08:18 PM
Oh, OK, thanks. :)
tecknophreak
12-17-2003, 09:02 AM
Ok, for some reason, I didn't think about this one at all or something before I posted. I could have just done:
std::string str("12345f");
if (static_cast<int>(str.find_first_not_of("0123456789", 0)) != -1) {
std::cout << "This string is not all digits" << std::endl;
}