|
| 1 | +/* |
| 2 | + * File: main.cpp |
| 3 | + * Author: Lukas Elmer |
| 4 | + * |
| 5 | + * Created on 30. Juli 2010, 12:12 |
| 6 | + */ |
| 7 | + |
| 8 | +#include <vector> |
| 9 | +#include <iostream> |
| 10 | +#include <numeric> |
| 11 | +#include <functional> |
| 12 | +#include <iomanip> |
| 13 | +#include <iterator> |
| 14 | +#include <string> |
| 15 | + |
| 16 | +#include <boost/bind.hpp> |
| 17 | +using namespace std; |
| 18 | + |
| 19 | +void printDoubles(std::ostream &os, double const &d) { |
| 20 | + using namespace std; |
| 21 | + os << setw(10) << setprecision(2) << fixed << d << '\n'; |
| 22 | +} |
| 23 | + |
| 24 | +int main(int argc, char** argv) { |
| 25 | + using namespace std; |
| 26 | + using namespace boost; |
| 27 | + vector<int> v; |
| 28 | + v.push_back(10); |
| 29 | + v.push_back(8); |
| 30 | + v.push_back(12); |
| 31 | + v.push_back(1); |
| 32 | + v.push_back(3); |
| 33 | + v.push_back(5); |
| 34 | + |
| 35 | + // summiert alle zahlen |
| 36 | + cout << accumulate(v.begin(), v.end(), 0, plus<int>()) << endl; |
| 37 | + // sum + (i*(i%2)), Summiert alle ungerade Zahlen |
| 38 | + cout << accumulate(v.begin(), v.end(), 0, bind(plus<int>(), _1, bind(multiplies<int>(), bind(modulus<int>(), _2, 2), _2))) << endl; |
| 39 | + // Zählt die Anzahl der ungeraden Zahlen |
| 40 | + cout << accumulate(v.begin(), v.end(), 0, bind(plus<int>(), _1, bind(modulus<int>(), _2, 2))) << endl; |
| 41 | + cout << count_if(v.begin(), v.end(), bind(modulus<int>(), _1, 2)) << endl; |
| 42 | + // Zählt die Anzahl der Zahlen mit Wert 42 |
| 43 | + cout << count_if(v.begin(), v.end(), bind(equal_to<int>(), _1, 42)) << endl; |
| 44 | + |
| 45 | + vector<double> v2; |
| 46 | + // Kopiert alle Werte von v in v2 |
| 47 | + copy(v.begin(), v.end(), back_inserter(v2)); |
| 48 | + // Ruft die Funktion printDoubles für alle doubles auf. Achtung: ref für den cout! |
| 49 | + for_each(v2.begin(), v2.end(), bind(printDoubles, ref(cout), _1)); |
| 50 | + |
| 51 | + // vergleichen Sie zwei strings lexikographisch, ohne gross/kleinschreibung zu unterscheiden. |
| 52 | + string s1 = "bla", s2 = "blu"; |
| 53 | + cout << s1 << " < " << s2 << "? " << lexicographical_compare(s1.begin(), s1.end(), s2.begin(), s2.end()) << endl; |
| 54 | + s1 = "bla", s2 = "Blu"; |
| 55 | + cout << s1 << " < " << s2 << "? (mit gross/kleinschreibung) " << |
| 56 | + lexicographical_compare(s1.begin(), s1.end(), s2.begin(), s2.end()) << endl; |
| 57 | + string tmp1 = s1, tmp2 = s2; |
| 58 | + transform(tmp1.begin(), tmp1.end(), tmp1.begin(), ::tolower); |
| 59 | + transform(tmp2.begin(), tmp2.end(), tmp2.begin(), ::tolower); |
| 60 | + cout << s1 << " < " << s2 << "? (ohne gross/kleinschreibung) " << |
| 61 | + lexicographical_compare(tmp1.begin(), tmp1.end(), tmp2.begin(), tmp2.end()) << endl; |
| 62 | + |
| 63 | + //suchen Sie in einem std::string nach aufeinanderfolgenden gleichen Zeichen |
| 64 | + // * Nutzen Sie einen passenden Algorithmus! |
| 65 | + // * geben Sie alle solche Positionen des std::string aus (braucht eine Schleife) |
| 66 | + // output: auee |
| 67 | + string s3("blaabluueeez"); |
| 68 | + string::iterator it = adjacent_find(s3.begin(), s3.end()); |
| 69 | + while (*it != *s3.end()) { |
| 70 | + cout << *it; |
| 71 | + it++; |
| 72 | + it = adjacent_find(it, s3.end()); |
| 73 | + } |
| 74 | + cout << endl; |
| 75 | + |
| 76 | + //entfernden Sie aus einem vector<int> alle durch 7 teilbaren Zahlen. endgültig. |
| 77 | + //vector<int> v; |
| 78 | + vector<int> v3; |
| 79 | + v.clear(); |
| 80 | + v.push_back(7); |
| 81 | + v.push_back(77); |
| 82 | + v.push_back(14); |
| 83 | + v.push_back(10); |
| 84 | + v.push_back(1); |
| 85 | + v.push_back(3); |
| 86 | + v.push_back(14); |
| 87 | + v.push_back(5); |
| 88 | + v.push_back(27); |
| 89 | + |
| 90 | + copy(v.begin(), v.end(), ostream_iterator<int>(cout, ", ")); |
| 91 | + cout << endl; |
| 92 | + remove_copy_if(v.begin(), v.end(), back_inserter(v3), bind(equal_to<int>(), bind(modulus<int>(), _1, 7), 0)); |
| 93 | + copy(v3.begin(), v3.end(), ostream_iterator<int>(cout, ", ")); |
| 94 | + cout << endl; |
| 95 | + |
| 96 | + return 0; |
| 97 | +} |
0 commit comments