diff --git a/gtsam/linear/VectorValues.cpp b/gtsam/linear/VectorValues.cpp index 2da1a12f1..bb0dbf625 100644 --- a/gtsam/linear/VectorValues.cpp +++ b/gtsam/linear/VectorValues.cpp @@ -129,30 +129,22 @@ namespace gtsam { } /* ************************************************************************* */ - bool compare(std::pair& lhs, std::pair& rhs) { - return lhs.first < rhs.first; - } - - ostream& operator<<(ostream& ss, const VectorValues& v) { - ss << "VectorValues: " + ostream& operator<<(ostream& os, const VectorValues& v) { + os << "VectorValues" << ": " << v.size() << " elements\n"; // Change print depending on whether we are using TBB #ifdef GTSAM_USE_TBB - std::vector> vec; - vec.reserve(v.size()); + map sorted; for (const auto& key_value : v) { - vec.push_back(std::make_pair(key_value.first, key_value.second)); + sorted.insert(std::make_pair(key_value.first, key_value.second)); } - sort(vec.begin(), vec.end(), compare); - for (const auto& key_value : vec) - ss << " " << key_value.first << ": " << key_value.second.transpose() - << "\n"; + for (const auto& key_value : sorted) #else for (const auto& key_value : v) - ss << " " << key_value.first << ": " << key_value.second.transpose() - << "\n"; #endif - return ss; + os << " " << key_value.first << ": " << key_value.second.transpose() + << "\n"; + return os; } /* ************************************************************************* */ diff --git a/gtsam/linear/VectorValues.h b/gtsam/linear/VectorValues.h index 32a311848..fe6b5fcb2 100644 --- a/gtsam/linear/VectorValues.h +++ b/gtsam/linear/VectorValues.h @@ -29,7 +29,7 @@ #include #include -#include +#include namespace gtsam { @@ -229,13 +229,11 @@ namespace gtsam { */ const_iterator find(Key j) const { return values_.find(j); } - /** - * overload operator << to print to stringstream - */ - friend std::ostream& operator<<(std::ostream& ss, const VectorValues& v); + /// overload operator << to print to stringstream + friend std::ostream& operator<<(std::ostream&, const VectorValues&); /** print required by Testable for unit testing */ - void print(const std::string& str = "VectorValues: ", + void print(const std::string& str = "VectorValues", const KeyFormatter& formatter = DefaultKeyFormatter) const; /** equals required by Testable for unit testing */ diff --git a/gtsam/linear/tests/testVectorValues.cpp b/gtsam/linear/tests/testVectorValues.cpp index 28600ecad..2f7e0a35c 100644 --- a/gtsam/linear/tests/testVectorValues.cpp +++ b/gtsam/linear/tests/testVectorValues.cpp @@ -241,10 +241,9 @@ TEST(VectorValues, print) vv.insert(7, Vector2(8, 9)); string expected = - "VectorValues: : 5 elements\n 0: 1\n 1: 2 3\n 2: 4 5\n 5: 6 7\n 7: 8 9\n"; + "VectorValues: 5 elements\n 0: 1\n 1: 2 3\n 2: 4 5\n 5: 6 7\n 7: 8 9\n"; stringstream actual; actual << vv; - EXPECT(expected == actual.str()); }