掲示板システム
ホーム
アクセス解析
カテゴリ
ログアウト
2次元配列のソートについて (ID:52653)
名前
ホームページ(ブログ、Twitterなど)のURL (省略可)
本文
> 行いたいことは 2次元配列の『列』でのソートです。 > Excelのように列にてソートを行いたく思います。 > まずファイルから vector<string> にて分割を行い、 > index を作成し、ソートを行ってみました。 vector<string>ではなく, struct { std::string index; std::string date; std::string str; } row_type; を使った方がわかりやすいのでは? まぁ,std::vector<std::string>を相手にしても同じようなことができますが。 とりあえず,構造体を利用するとして,まずは比較用の型を用意。 struct row_type_compare : binary_function<row_type, row_type, bool> { bool operator() (const row_type & lhs, const row_type & rhs) const { return lhs.str < rhs.str; // std::stringは直接比較可能 } }; で, > // ソートの実行 > string str; > string::size_type tempval = 0; > > for (i = 0; i < data.size(); i++){ > str = data[index[i]][2]; > int x = i; > > for (j = 0 ; j < data.size() ; j++){ > if (strcmp(data[index[j]][2].c_str(), > str.c_str()) > 0){ > str = data[index[j]][2]; > x = j; > } > } > > // インデックスの交換 > tempval = index[i]; > index[i] = index[x]; > index[x] = tempval; > } は std::sort(data.begin(), data.end(), row_type_compare()); で一発。 インデックスが必要なら,それなりにデータ構造を変える必要がありますが……。
←解決時は質問者本人がここをチェックしてください。
戻る
掲示板システム
Copyright 2020 Takeshi Okamoto All Rights Reserved.