I found myself having to write a bunch of range checking code like this:
if (val < 0.0 || val > 1.0) val = 1.0;
so I wrote this simple function.
Continue reading “C++ test for within”
NYC, Politics, and Tech
I found myself having to write a bunch of range checking code like this:
if (val < 0.0 || val > 1.0) val = 1.0;
so I wrote this simple function.
Continue reading “C++ test for within”
To reset Xcode to the default settings:
$ defaults delete com.apple.Xcode $ rm -rf ~/Library/Application Support/Xcode
First, I dislike having a lot of popup windows so I set the layout to “All-In-One” to keep most things within a single window:
And my usual indentation style:
I like having the files autosaved on build:
There’s talk that Bing has gotten to a 10% market share in search. Apple Insider instead points out that the study didn’t include a big part of Google’s search business (you know, maps, video, local directory). Here’s a chart showing how Google is dominating and pushing nearly 90%:
Sorry for the long delay. I’ve been playing around a bit.
I’d been using the sqlite3 that came installed on Mac OS X but I ran into some portability issues — not every system has it installed. So I decided to bite the bullet and include it with my code.
It was trivial! I downloaded a zip with 3 files (sqlite3.c,sqlite3.h, and sqlite3ext.h). Added sqlite3.c to my list of sources and it just compiled! No configuration, no trickly flags, it just worked!
So last week I wrote about initializing a std::map from two std::vectors. How about doing the reverse? How do I get a list of the keys or a list of the values into a vector?
So I wrote a functor, based on std::unary_function, that returns the pair.first and another that returns the pair.second:
Continue reading “Getting the keys from a std::map”
In a destructor for a class, I had a std::vector of pointers that I wanted to delete. I started to write the usual for(;;) loop and realized I do this often enough I should make it easy to use std::for_each().
Here’s the functor (based on std::unary_function):
template struct deleter: public std::unary_function { bool operator()(Type * ptr) const { delete ptr; return true; } };
And here’s some sample code using it:
std::vector m_symbols; // ... std::for_each (m_symbols.begin(), m_symbols.end(), deleter());
I added a return of true because some other template code wasn’t happy about “return void” — supposedly something fixed in recent compilers.
I was using the std::stream_iterator, for example:
std::vector vals; vals.push_back (12); vals.push_back (22); vals.push_back (5); vals.push_back (30); // The std::ostream_iterator appends the "," std::ostringstream str3; std::copy (vals.begin(), vals.end(), std::ostream_iterator (str3, ",")); CPPUNIT_ASSERT_EQUAL (std::string ("12,22,5,30,"), str3.str());
It has the “,” after the last item.
So here’s an implementation that doesn’t append the seperator after the last one:
#ifndef INCLUDED_OUTITER_H #define INCLUDED_OUTITER_H #include /** * A replacement for std::ostream_iterator that doesn't put the * seperator after the last item. */ template<typename Type, typename CharType = char, typename Traits = std::char_traits > class outiter : public std::iterator { public: typedef CharType char_type; typedef Traits traits_type; typedef std::basic_ostream ostream_type; /// Initialize from a stream outiter (ostream_type &stream) : m_stream (&stream), m_string (0), m_started (false) {} /// Copy constructor outiter (const outiter ©) : m_stream (copy.m_stream), m_string (copy.m_string), m_started (copy.m_started) {} /// Initialize from a stream and the seperator outiter (ostream_type &stream, const CharType *str) : m_stream (&stream), m_string (str), m_started (false) {} /// Assignment actually does the output outiter & operator=(const Type &value) { if (!m_started) { m_started = true; } else if (m_string) { (*m_stream) << m_string; } (*m_stream) << value; return *this; } /// Just return a reference to this outiter & operator*() { return *this; } /// Just return a reference to this outiter & operator++() { return *this; } /// Just return a reference to this outiter & operator++(int) { return *this; } private: /// The stream to write to ostream_type * m_stream; /// The seperator (may be NULL) const char_type * m_string; /// Flag to indicate if we've output anything bool m_started; }; #endif /* INCLUDED_OUTITER_H */
And here’s the corresponding code to use it:
std::ostringstream str; std::copy (vals.begin(), vals.end(), outiter (str, ",")); CPPUNIT_ASSERT_EQUAL (std::string ("12,22,5,30"), str.str());
It’s one step closer to happening! Getting a new car based on a trade in might actually happen. It hasn’t passed the House yet (much less the Senate) but the leadership of both parties agree on doing it.
From House Reaches a Deal on ‘Cash for Clunkers’ Program:
Under the House plan, a car trade-in that improves fuel efficiency by at least 10 miles per gallon would qualify for a $4,500 voucher, as would the trade-in of a small truck that improves efficiency by 5 miles per gallon. The new vehicle must have a minimum fuel efficiency rating of 22 miles per gallon for cars and 18 miles per gallon for small trucks.
I’ve gotten pretty careful about keeping time in UTC and then converting it to localtime for the user to understand. For the first time, I actually had to find the localtime in a non-local timezone. It’s ugly. It seems you have to mess with the TZ environment variable. Here’s what I wrote:
#include
And here’s a code fragment that uses it:
std::string oldzone = changeTimeZone("US/Pacific"); time_t seconds = ::time(0); struct tm tm_time; localtime_r (&seconds, tm_time); changeTimeZone(oldzone);