[lambda] Making my expression less complicated

Hello, In my test program below, I have a container of columns and want to output the column headers into a string separated by a tab. The lambda expression is looking fairly complicated so I wonder if it could be made simpler or if there's any other algorithm I should've used to put a separator between the elements? Regards, Pete #include <algorithm> #include <iostream> #include <string> #include <vector> #include <boost/lambda/bind.hpp> #include <boost/lambda/lambda.hpp> #include <boost/lambda/if.hpp> class Column { public: explicit Column(const std::string& str) : str_(str) {} const std::string& getColumnText() const {return str_;} private: std::string str_; }; int main() { using namespace boost::lambda; using boost::lambda::_1; std::vector<Column> cols; cols.push_back(Column("Fred")); cols.push_back(Column("Jim")); cols.push_back(Column("Harry")); const std::size_t numCols = static_cast<size_t>(cols.size()); const char fieldSeparator = '\t'; std::string outputText; std::size_t i = 1; var_type<std::size_t>::type iV(var(i)); std::for_each(cols.begin(),cols.end(), ( var(outputText) += bind(&Column::getColumnText,_1), if_then(iV < constant(numCols),var(outputText) += constant(fieldSeparator)), ++iV ) ); std::cout << '[' << outputText << ']'; }

AMDG Peter Barker wrote:
Hello,
In my test program below, I have a container of columns and want to output the column headers into a string separated by a tab. The lambda expression is looking fairly complicated so I wonder if it could be made simpler or if there's any other algorithm I should've used to put a separator between the elements?
How about, std::size_t i = 0; //... ( if_(var(i)++) [ var(outputText) += constant(fieldSeparator) ], var(outputText) += bind(&Column::getColumnText,_1) ) In Christ, Steven Watanabe

--- On Fri, 5/9/08, Steven Watanabe <watanabesj@gmail.com> wrote:
From: Steven Watanabe <watanabesj@gmail.com> Subject: Re: [Boost-users] [lambda] Making my expression less complicated To: boost-users@lists.boost.org Date: Friday, 5 September, 2008, 5:44 PM AMDG
Peter Barker wrote:
Hello,
In my test program below, I have a container of
columns and want to output the column headers into a string separated by a tab. The lambda expression is looking fairly complicated so I wonder if it could be made simpler or if there's any other algorithm I should've used to put a separator between the elements? >
How about,
std::size_t i = 0;
//... ( if_(var(i)++) [ var(outputText) += constant(fieldSeparator) ], var(outputText) += bind(&Column::getColumnText,_1) )
That's much nicer and works very well, thanks :) By the way, do you know if there's something in Boost that works like std::ostream_iterator but does what I'm doing - putting the delimiter between the elements, not after each element? Some Googling tells me that there was an "Output Formatter" Boost library submitted with a range_streamer class to do it but the looks like that library was rejected. See http://lists.boost.org/Archives/boost/2004/09/71875.php Regards, Pete
participants (2)
-
Peter Barker
-
Steven Watanabe