diff --git a/gtsam/discrete/DiscreteValues.cpp b/gtsam/discrete/DiscreteValues.cpp index 416dfb888..3c3ed4468 100644 --- a/gtsam/discrete/DiscreteValues.cpp +++ b/gtsam/discrete/DiscreteValues.cpp @@ -26,12 +26,24 @@ using std::stringstream; namespace gtsam { +/* ************************************************************************ */ +static void stream(std::ostream& os, const DiscreteValues& x, + const KeyFormatter& keyFormatter) { + for (const auto& kv : x) + os << "(" << keyFormatter(kv.first) << ", " << kv.second << ")"; +} + +/* ************************************************************************ */ +std::ostream& operator<<(std::ostream& os, const DiscreteValues& x) { + stream(os, x, DefaultKeyFormatter); + return os; +} + /* ************************************************************************ */ void DiscreteValues::print(const string& s, const KeyFormatter& keyFormatter) const { cout << s << ": "; - for (auto&& kv : *this) - cout << "(" << keyFormatter(kv.first) << ", " << kv.second << ")"; + stream(cout, *this, keyFormatter); cout << endl; } diff --git a/gtsam/discrete/DiscreteValues.h b/gtsam/discrete/DiscreteValues.h index fa8a8a846..df4ecdbff 100644 --- a/gtsam/discrete/DiscreteValues.h +++ b/gtsam/discrete/DiscreteValues.h @@ -64,6 +64,9 @@ class GTSAM_EXPORT DiscreteValues : public Assignment { /// @name Standard Interface /// @{ + /// ostream operator: + friend std::ostream& operator<<(std::ostream& os, const DiscreteValues& x); + // insert in base class; std::pair insert( const value_type& value ){ return Base::insert(value);