Beispiele für Predicate
-
Kann mir vielleicht jemand mal ein beispiel für ein predicate geben, bzw. wie müsste folgendes predicate (_1) aussehen?
const int size = 6; int a[size] = { 2, 5, 1, 3, 6, 4 }; std::ostream_iterator<int> int_out_iter(std::cout, " "); std::remove_copy_if(a, a+size, int_out_iter, _1 > 3); std::cout << " (should be: 2 1 3)" << std::endl;
danke!
-
class above { private: int what; public: above(int what) : what(what) {} bool operator()(int num) { return what<num; } };
und dann einfach
std::remove_copy_if(a, a+size, int_out_iter, above(3));
-
okay...
und wie schaffe ich es, dass das so wie in dem beispiel von mir geht...
also die 3 über den operator > geparst wird???
-
Original erstellt von esskar:
okay...
und wie schaffe ich es, dass das so wie in dem beispiel von mir geht...
also die 3 über den operator > geparst wird???ein perdicate ist eine Klasse mit ueberladenen operator()
siehe mein beispiel
-
class _1 { private: int m_pred; public: _1(const int & p) : m_pred(p) {} bool operator ()(const int & a) { return a > m_pred; } };
bzw.
class _1 { private: int m_pred; public: _1(const int & p) : m_pred(p) {} bool operator > (const int & a) { return a > m_pred; } };
gehts z.b. nicht!
-
const int& - ist das moderne kunst??
naja, variante 2 kann nicht gehen, da du operator() statt operator< ueberladen musst.
und was geht bei variante 1 nicht?
_l ist uebrigens kein gut gewaehlter name
-
class _1 { private: int m_pred; public: _1(int p) : m_pred(p) {} bool operator () (int a) { return a > m_pred; } };
$ g++ -c placeholder.cpp
placeholder.cpp: In functionint main()': placeholder.cpp:30: parse error before
>' tokenwobei
std::remove_copy_if(a, a+size, int_out_iter, _1 > 3);
die zeile 30 ist!
-
schau dir noch mal mein beispiel an:
std::remove_copy_if(a, a+size, int_out_iter, above(3));
und dann vergleiche das mit deiner version:
std::remove_copy_if(a, a+size, int_out_iter, _1 > 3);faellt dir der unterschied auf?
-
Original erstellt von Shade Of Mine:
**schau dir noch mal mein beispiel an:
std::remove_copy_if(a, a+size, int_out_iter, above(3));
und dann vergleiche das mit deiner version:
std::remove_copy_if(a, a+size, int_out_iter, _1 > 3);faellt dir der unterschied auf?**
ja...
die aufgabenstellung verlangt aber meine variante...
sorry!!!
-
deine variante??
die geht aber nicht weil es ein syntax fehler ist!
remove_copy_if verlangt nunmal ein predicate und keinen boolgeile Loesung waere ja:
class _2_ { private: int _; public: _2_(int _) : _(_) {} bool operator()(int _) { return this->_<_; } }; struct _1_ { friend _2_ operator>(_1_ _, int __) { return _2_(__); } }; //und dann: int main() { const int __ = 6; int _[__] = { 2, 5, 1, 3, 6, 4 }; std::ostream_iterator<int> ___(std::cout, " "); _1_ _1; std::remove_copy_if(_, _+__, ___, _1 > 3); std::cout << " (should be: 2 1 3)" << std::endl; }
-
es muss aber irgendwie gehen...
hier mal die aufgabenstellungThe following call should find the first element in container c that is greater than 3:
std::find(c.begin(), c.end(), _1 > 3);To make this work, the expression _1 > 3 must create a Predicate that returns true if its argument is greater than 3. This is achieved by defining the placeholder variable _1 (a placeholder for the argument of the predicate) and its operator> appropriately.
Implement the placeholder variables _1 and _2 to make the following test program (in placeholder.C) to work. Note that the expression _1 > _2 creates a Binary Predicate.placeholder.C
int main() { const int size = 6; int a[size] = { 2, 5, 1, 3, 6, 4 }; float b[size] = { 4.0, 6.5, 4.5, 1.0, 5.0, 2.5 }; std::ostream_iterator<int> int_out_iter(std::cout, " "); std::ostream_iterator<float> float_out_iter(std::cout, " "); std::cout << "Part (a): "; std::remove_copy_if(a, a+size, int_out_iter, _1 > 3); std::cout << " (should be: 2 1 3)" << std::endl; std::cout << "Part (b): "; std::remove_copy_if(b, b+size, float_out_iter, _1 > 4.0); std::cout << " (should be: 4 1 2.5)" << std::endl; std::cout << "Part (c): "; std::remove_copy_if(b, b+size, float_out_iter, _1 > 4); std::cout << " (should be: 4 1 2.5)" << std::endl; std::cout << "Part (d): "; std::sort(a, a+size, _1 > _2); std::copy(a, a+size, int_out_iter); std::cout << " (should be: 6 5 4 3 2 1)" << std::endl; }
The placeholders should work with any type, not just with float and int.
-
Ein Fall für Boost::lambda http://www.boost.org/libs/lambda/doc/index.html .
-
Original erstellt von <boost>:
Ein Fall für Boost::lambda http://www.boost.org/libs/lambda/doc/index.html .pfft. selberschreiben.
-
bin verzweifelt...
-
shade of mine bietet dir eine lösung an
-
Original erstellt von <shades freund>:
shade of mine bietet dir eine lösung anstimmt. ich will mal dennoch konstruktiv sein:
class X1 {} _1; class X2 {} _2; template<class T> class left_less { int x; public: left_less(T z) : x(z) {} bool operator() (const T &v) { return x < v; } }; template<class T> left_less<T> operator<(const T &a, X1) { return left_less<T>(a); }
ist zwar eigentlich schon zuviel... aber egal (ungetestet übrigens)
-
cool...
erstmal danke, @all!
-
[ Dieser Beitrag wurde am 03.06.2003 um 14:34 Uhr von esskar editiert. ]