質問をすることでしか得られない、回答やアドバイスがある。

15分調べてもわからないことは、質問しよう!

新規登録して質問してみよう
ただいま回答率
85.50%
Visual Studio

Microsoft Visual StudioはMicrosoftによる統合開発環境(IDE)です。多種多様なプログラミング言語に対応しています。

C++

C++はC言語をもとにしてつくられた最もよく使われるマルチパラダイムプログラミング言語の1つです。オブジェクト指向、ジェネリック、命令型など広く対応しており、多目的に使用されています。

Q&A

解決済

1回答

2044閲覧

カスタムクラスのvectorをソートしたい

g_uo

総合スコア212

Visual Studio

Microsoft Visual StudioはMicrosoftによる統合開発環境(IDE)です。多種多様なプログラミング言語に対応しています。

C++

C++はC言語をもとにしてつくられた最もよく使われるマルチパラダイムプログラミング言語の1つです。オブジェクト指向、ジェネリック、命令型など広く対応しており、多目的に使用されています。

0グッド

0クリップ

投稿2018/08/08 01:11

いつもお世話になります。
オリジナルクラスの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

以上、よろしくお願いします。

気になる質問をクリップする

クリップした質問は、後からいつでもMYページで確認できます。

またクリップした質問に回答があった際、通知やメールを受け取ることができます。

バッドをするには、ログインかつ

こちらの条件を満たす必要があります。

guest

回答1

0

自己解決

[自己解決]
以下の方法で解決することが出来ました。

c++

1 // Sort 2 std::sort(hoges.begin(), hoges.end(), [ ](const std::unique_ptr<Hoge>& lhoge, const std::unique_ptr<Hoge>& rhoge) 3 { 4 return lhoge->GetKey() < rhoge->GetKey(); 5 });

修正後の実行結果

[Original] 1.100000, 3.300000, 2.200000, 2.200000, 0.100000, 2.900000, 0.500000 [Sorted] 0.100000, 0.500000, 1.100000, 2.200000, 2.200000, 2.900000, 3.300000

次のサイトを参考にしました。
Sorting a vector of unique_ptr's

ラムダを用いた記述方法に変更し、引数をstd::unique_ptr<Hoge>&に変更しました。

お騒がせして申し訳ありません。また、ご回答を記述中だった方がおりましたら、重ねてお詫び申し上げます。

修正後ソース全文

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 float GetKey() const 21 { 22 return float_key_; 23 } 24 25private: 26 float float_key_; 27 int32_t int_value_; 28}; 29 30void TestVectorSort() 31{ 32 std::vector<std::unique_ptr<Hoge>> hoges; 33 34 hoges.push_back(std::make_unique<Hoge>(1.1, 10)); 35 hoges.push_back(std::make_unique<Hoge>(3.3, 10)); 36 hoges.push_back(std::make_unique<Hoge>(2.2, 10)); 37 hoges.push_back(std::make_unique<Hoge>(2.2, 10)); 38 hoges.push_back(std::make_unique<Hoge>(0.1, 10)); 39 hoges.push_back(std::make_unique<Hoge>(2.9, 10)); 40 hoges.push_back(std::make_unique<Hoge>(0.5, 10)); 41 42 auto i = 0; 43 44 // Original output 45 std::cout << "[Original]" << std::endl; 46 for (i = 0; i < hoges.size(); i++) { 47 if (0 < i) std::cout << ", "; 48 std::cout << std::to_string(hoges[i]->GetKey()); 49 } 50 std::cout << std::endl; 51 52 // Sort 53 std::sort(hoges.begin(), hoges.end(), [ ](const std::unique_ptr<Hoge>& lhoge, const std::unique_ptr<Hoge>& rhoge) 54 { 55 return lhoge->GetKey() < rhoge->GetKey(); 56 }); 57 58 // Sorted output 59 std::cout << "[Sorted]" << std::endl; 60 for (i = 0; i < hoges.size(); i++) { 61 if (0 < i) std::cout << ", "; 62 std::cout << std::to_string(hoges[i]->GetKey()); 63 } 64 std::cout << std::endl; 65 66} 67 68int main() 69{ 70 TestVectorSort(); 71 72 int _; 73 std::cin >> _; 74 return 0; 75}

また問題や不明なことがありましたら、お力添えいただければ幸いです。
ありがとうございました。

投稿2018/08/08 01:54

g_uo

総合スコア212

バッドをするには、ログインかつ

こちらの条件を満たす必要があります。

あなたの回答

tips

太字

斜体

打ち消し線

見出し

引用テキストの挿入

コードの挿入

リンクの挿入

リストの挿入

番号リストの挿入

表の挿入

水平線の挿入

プレビュー

15分調べてもわからないことは
teratailで質問しよう!

ただいまの回答率
85.50%

質問をまとめることで
思考を整理して素早く解決

テンプレート機能で
簡単に質問をまとめる

質問する

関連した質問