Gavin Doughtie on 15 Aug 2003 20:52:34 -0000


[Date Prev] [Date Next] [Thread Prev] [Thread Next] [Date Index] [Thread Index]

[ALACPP] compact, insane example code


// OK, this compiles and runs. boost::lambda is INSANE!
// Can anybody make it even more STL/boost exotic? Perhaps initializing the string vector??

#include <algorithm>
#include <numeric>
#include <string>
#include <iostream>
#include <vector>

#include <boost/lambda/lambda.hpp>
#include <boost/lambda/bind.hpp>

using namespace std;
using namespace boost::lambda;

string uppercase(const string& input)
{
   string result(input);
   transform(result.begin(), result.end(), result.begin(), ::toupper);
   return result;
}

// Non boost::lambda version for comparison
// ostream& operator<<(ostream& str, const vector<string>& rhs)
// {
//         copy(rhs.begin(), rhs.end(),
//                 ostream_iterator<string>(rhs, "\n"));
//         return str;
// }

// Very groovy boost::lambda version
ostream& operator << (ostream& str, const vector<string> & rhs)
{
    for_each(rhs.begin(), rhs.end(), str << _1 << "\n");
    return str;
}

int main(int, char**)
{
    char* badNameArray[] = {"one", "two", "three"};
    vector<string> badNames(&badNameArray[0], &badNameArray[3]);

    vector<string> goodNames;
    transform(badNames.begin(), badNames.end(), back_inserter(goodNames), ::uppercase);
    // transformFunc could also be written with lambda somehow, but yechh
    
    int theCount(accumulate(goodNames.begin(), goodNames.end(), 0, _1 + bind(&string::size, _2)));
    string theAccumulatedString(accumulate(goodNames.begin(), goodNames.end(), string(""), _1 += _2));
    
    cout << theCount << " total characters in all strings, accumulated from:" << endl << theAccumulatedString << endl;
    
    cout << "badNames: " << badNames << endl;
    cout << "goodNames: " << goodNames << endl;
    return 0;
}

_______________________________________________
alacpp mailing list
alacpp@xxxxxxxxxxx
http://lists.ellipsis.cx/mailman/listinfo/alacpp