いつもお世話になります。
オリジナルクラスのvectorをソートしたいです。
float
型の変数を基準にしてvectorをソート(昇順)したいのですが、意図した通りに並べ替えられません。
次のサイトを参考に以下の形で実装しています。
Sorting a vector of custom objects
Hoge
クラス (ソート対象の自作クラス)
c++
1class Hoge 2{ 3public: 4 Hoge():float_key_(0.f), int_value_(0){} 5 6 Hoge(const float key, const int value):float_key_(key), int_value_(value){} 7 8 ~Hoge() = default; 9 10 bool operator < (const Hoge& hoge) const 11 { 12 return (float_key_ < hoge.float_key_); 13 } 14 15 float GetKey() const 16 { 17 return float_key_; 18 } 19 20private: 21 float float_key_; 22 int32_t int_value_; 23};
並べ替えおよびテストコード
C++
1void TestVectorSort() 2{ 3 std::vector<std::unique_ptr<Hoge>> hoges; 4 5 hoges.push_back(std::make_unique<Hoge>(1.1, 10)); 6 hoges.push_back(std::make_unique<Hoge>(3.3, 10)); 7 hoges.push_back(std::make_unique<Hoge>(2.2, 10)); 8 hoges.push_back(std::make_unique<Hoge>(2.2, 10)); 9 hoges.push_back(std::make_unique<Hoge>(0.1, 10)); 10 hoges.push_back(std::make_unique<Hoge>(2.9, 10)); 11 hoges.push_back(std::make_unique<Hoge>(0.5, 10)); 12 13 auto i = 0; 14 15 // Original output 16 std::cout << "[Original]" << std::endl; 17 for (i = 0; i < hoges.size(); i++) { 18 if (0 < i) std::cout << ", "; 19 std::cout << std::to_string(hoges[i]->GetKey()); 20 } 21 std::cout << std::endl; 22 23 // Sort 24 std::sort(hoges.begin(), hoges.end()); 25 26 // Sorted output 27 std::cout << "[Sorted]" << std::endl; 28 for (i = 0; i < hoges.size(); i++) { 29 if (0 < i) std::cout << ", "; 30 std::cout << std::to_string(hoges[i]->GetKey()); 31 } 32 std::cout << std::endl; 33 34}
実行結果
[Original] 1.100000, 3.300000, 2.200000, 2.200000, 0.100000, 2.900000, 0.500000 [Sorted] 0.100000, 0.500000, 1.100000, 3.300000, 2.200000, 2.200000, 2.900000
希望する実行結果としては以下の並びを期待しています。
[Sorted] 0.100000, 0.500000, 1.100000, 2.200000, 2.200000, 2.900000, 3.300000
解決手法や、私の認識不足をご教示いただきたく、よろしくお願いします。
情報の不足などございましたら、お手数ですがご指摘ください。
ソース全文
c++
1// CppTestProject.cpp : アプリケーションのエントリ ポイントを定義します。 2// 3 4#include "stdafx.h" 5#include <vector> 6#include <iostream> 7#include <string> 8#include <memory> 9#include <algorithm> 10 11class Hoge 12{ 13public: 14 Hoge():float_key_(0.f), int_value_(0){} 15 16 Hoge(const float key, const int value):float_key_(key), int_value_(value){} 17 18 ~Hoge() = default; 19 20 bool operator < (const Hoge& hoge) const 21 { 22 return (float_key_ < hoge.float_key_); 23 } 24 25 float GetKey() const 26 { 27 return float_key_; 28 } 29 30private: 31 float float_key_; 32 int32_t int_value_; 33}; 34 35void TestVectorSort() 36{ 37 std::vector<std::unique_ptr<Hoge>> hoges; 38 39 hoges.push_back(std::make_unique<Hoge>(1.1, 10)); 40 hoges.push_back(std::make_unique<Hoge>(3.3, 10)); 41 hoges.push_back(std::make_unique<Hoge>(2.2, 10)); 42 hoges.push_back(std::make_unique<Hoge>(2.2, 10)); 43 hoges.push_back(std::make_unique<Hoge>(0.1, 10)); 44 hoges.push_back(std::make_unique<Hoge>(2.9, 10)); 45 hoges.push_back(std::make_unique<Hoge>(0.5, 10)); 46 47 auto i = 0; 48 49 // Original output 50 std::cout << "[Original]" << std::endl; 51 for (i = 0; i < hoges.size(); i++) { 52 if (0 < i) std::cout << ", "; 53 std::cout << std::to_string(hoges[i]->GetKey()); 54 } 55 std::cout << std::endl; 56 57 // Sort 58 std::sort(hoges.begin(), hoges.end()); 59 60 // Sorted output 61 std::cout << "[Sorted]" << std::endl; 62 for (i = 0; i < hoges.size(); i++) { 63 if (0 < i) std::cout << ", "; 64 std::cout << std::to_string(hoges[i]->GetKey()); 65 } 66 std::cout << std::endl; 67 68} 69 70int main() 71{ 72 TestVectorSort(); 73 74 int _; 75 std::cin >> _; 76 return 0; 77}
補足情報(FW/ツールのバージョンなど)
言語: C++ (VC++ 2017)
開発環境: Windows 10 Pro 64bit
IDE: Visual Studio 2017 v15.7.5
以上、よろしくお願いします。
回答1件
あなたの回答
tips
プレビュー
バッドをするには、ログインかつ
こちらの条件を満たす必要があります。