2016-06-14 9 views
-4

私は、次の文字列があります。 1465883175.476876 RX 0x03の0x00000000の文字列を入力してコンテナをソートする方法は?

1465883175.606049 RX 0x00の0x00000000の

1465883175.783562 RX 0x02の0x00000000の

1465883175.906900 RX 0x03の0x00000000の

1465883176.051490 RX 0x00の0x00000000の

1465883176.201903をRX 0x03 0x00000000

ダブルタイプ(C++)の最初のデータに従って並べ替えるコンテナにどのように入力すればよいですか?

私は最も効果的な方法でそれを行う必要があります。

私はstd :: setコンテナを使用することを考えますが、このコンテナでは、コンテナ内の2つの要素には同等のキーがありません。 nyの場合、文字列には同じ番号が含まれている可能性があります。

+1

を。あなたの研究を共有してください。* –

+0

文字列として 'std :: vector'に入れて、[' std :: sort'](http://en.cppreference.com/w/cpp/algorithm/sort)をdoubleを解析して比較する独自の比較関数。または、解析されたフィールドで構造体を使用し、それらをベクトルに入れることができるため、比較ごとにダブルを解析する必要はありません。 –

+0

@ Karsten Koop-プライオリティキューはどうですか? –

答えて

0
#include <string> 
#include <vector> 
#include <algorithm> 
#include <iostream> 
#include <queue> 

int main() { 
    std::vector<std::string> s{ 
    "1465883175.476876 RX 0x03 0x00000000" 
    ,"1465883175.606049 RX 0x00 0x00000000" 
    ,"1465883175.783562 RX 0x02 0x00000000" 
    ,"1465883175.906900 RX 0x03 0x00000000" 
    ,"1465883176.051490 RX 0x00 0x00000000" 
    ,"1465883176.201903 RX 0x03 0x00000000"}; 

    auto cmp = [](const std::string &f, const std::string &s) { 
    double n1 = 0.0; 
    size_t processed1{0}; 
    auto sub_str1 = f.substr(0, f.find(" ")); 
    try { 
     n1 = std::stod(sub_str1, &processed1); 
     if (processed1 != sub_str1.size()) { 
     // TODO 
     } 
    } catch (std::invalid_argument ia) { 
     // TODO 
    } catch (std::out_of_range oor) { 
     // TODO 
    } 

    double n2 = 0.0; 
    size_t processed2{0}; 
    auto sub_str2 = s.substr(0, s.find(" ")); 
    try { 
     n2 = std::stod(sub_str2, &processed2); 
     if (processed2 != sub_str2.size()) { 
     // TODO 
     } 
    } catch (std::invalid_argument ia) { 
     // TODO 
    } catch (std::out_of_range oor) { 
     // TODO 
    } 

    return n1 > n2; 
    }; 

    std::priority_queue<std::string, std::vector<std::string>, decltype(cmp)> q(cmp); 

    for (const auto &c : s) { 
    q.push(c); 
    } 
    while (!q.empty()) { 
    std::cout << q.top() << "\n"; 
    q.pop(); 
    } 
    return 0; 
} 
+0

優先度キューにsoritinhのキーがあってはなりませんか?私はそれを鍵にすることはできないのですが、それを自動的にソートする必要がありますか? –

+0

プライオリティキューに比較機能(コード上のcmp)が必要な場合、priority_queueはランダムアクセスコンテナのヒープ管理と似ていますが、ヒープを誤って無効にできないという利点があります。 http:// en .cppreference.com/w/cpp/container/priority_queue –

+0

これで、特定のキーを取得し、このキー(この場合は番号)に基づいてソートできるコンテナはありますか? –

0

(あなたがこれを改善する必要がある)、C++ 11コンパイラのサポートでこのコードを試してみてください:*詳細を提供します:あなたが質問するときの右側のポップアウトボックスの状態として

#include <string> 
#include <vector> 
#include <algorithm> 
#include <iostream> 

int main() { 
    std::vector<std::string> s{ 
    "1465883175.476876 RX 0x03 0x00000000" 
    ,"1465883175.606049 RX 0x00 0x00000000" 
    ,"1465883175.783562 RX 0x02 0x00000000" 
    ,"1465883175.906900 RX 0x03 0x00000000" 
    ,"1465883176.051490 RX 0x00 0x00000000" 
    ,"1465883176.201903 RX 0x03 0x00000000"}; 

    std::sort(s.begin(), s.end(), [](const std::string &f, const std::string &s){ 
    double n1 = 0.0; 
    size_t processed1{0}; 
    auto sub_str1 = f.substr(0, f.find(" ")); 
    try { 
     n1 = std::stod(sub_str1, &processed1); 
     if (processed1 != sub_str1.size()) { 
     // TODO 
     } 
    } catch (std::invalid_argument ia) { 
     // TODO 
    } catch (std::out_of_range oor) { 
     // TODO 
    } 

    double n2 = 0.0; 
    size_t processed2{0}; 
    auto sub_str2 = s.substr(0, s.find(" ")); 
    try { 
     n2 = std::stod(sub_str2, &processed2); 
     if (processed2 != sub_str2.size()) { 
     // TODO 
     } 
    } catch (std::invalid_argument ia) { 
     // TODO 
    } catch (std::out_of_range oor) { 
     // TODO 
    } 

    return n1 < n2; 
    }); 
    for (const auto &c : s) { 
    std::cout << c << "\n"; 
    } 
    return 0; 
} 
+0

https://ideone.com/usorzl –

+0

目的は、ベクトルの要素をソートして挿入するコンテナに挿入することです。 –

+0

関連はlamdaなので、優先度キューでこれを使用してください。例を見てください。 –

関連する問題