2016-08-12 1 views
4

私はSTLからのベクトルのリストを持っています。それらは長さが異なり、それぞれのベクトルの最初の要素で並べ替える必要があります。例えば、Iはベクトルを有する:4 5 6 10、1 、2 3,3 7~9を最初の数は、各ベクトルの大きさであり、それは、ベクターのが続いています要素。私は最初の要素でソートされたファイルにベクトルを表示したい。この例では、私のベクトルがこの順に表示されるはず:3 7 9、2 3、4 5 6 10及び1 。私はSTLベクトルのリストを持っており、各ベクトルの最初の要素でそれらをソートしたい

ofstream fout ("retele.out"); 
fout << T << '\n'; 
for (i=1; i<=T; i++) 
{ 
    fout << sol[i].size() << ' '; 
    sort(sol[i].begin(),sol[i].end()); 
    for (j=0; j<sol[i].size(); j++) 
     fout << sol[i][j] << ' '; 
    fout << '\n'; 
} 
fout.close(); 

"T" はベクトルの数は次のとおりです。

は、ここに私のコードです。 "並べ替え"機能は各ベクトルをソートするためのもので、今説明したようにベクトルをソートするために別のソートが必要です。

bool compare_vec (const std::vector<int>& first, const std::vector<int>& second) 
{ 
    return first[1]<second[1]; 
} 

およびコール:あなたはこのようなメソッドを比較作成することができます

:このlinkに基づいて

+1

あなたは、この例に変数 'sol'の宣言を追加することはできますか? – JVApen

+2

'sort(sol.begin()、sol.end())'でしょうか? – enedil

+0

ここに宣言があります: 符号なし整数i、j; ベクトル sol [50001]; 符号なし整数T; –

答えて

6

他の回答のための小さな明確化:std::vector<T>がすでに説明したように、全く同じ動作をするoperator<cplusplus description)を持つの一つは、これを書き換える必要があります。

template < class T, class Alloc > 
bool operator < (const vector<T, Alloc>& lhs, const vector<T, Alloc>& rhs); 

説明:

The less-than comparison (operator<) behaves as if using algorithm lexicographical_compare, which compares the elements sequentially using operator< in a reciprocal manner (i.e., checking both a < b and b < a) and stopping at the first occurrence.

だから、あなただけUP

sort(sol.begin(), sol.end()); 

書くことができます:それはあなたがstd::vector、ないstd::list<std::vector>の配列を持っているコメントで書かれています。配列にはbegin()end()のメソッドがないため、このコードは正しく動作しません。ソリューションはSIZEをソートしたい配列の一部の大きさである

sort(sol, sol + SIZE); 

です。私はこれがあなたのベクトル並べ替えることができます願ってい

+0

問題はそれが動作しないということです。これはエラーです: "クラス 'ではない' 'std :: vector [50001]' 'である' sol 'のメンバー' begin 'のリクエスト。 –

+0

@TudorGălăţan* UP *の後に回答の部分をお読みください。 – alexeykuzmin0

+1

さらに良い: 'sort(std :: begin(sol)、std :: end(sol))'。 – Quentin

0

回答

sol.sort(compare_vec); 
+0

エラーが発生しました:「ソールド」のメンバ「ソート」の要求。非クラス型の「std :: vector [50001] '」です。 –

0

thisポストをチェックしてください。

bool myfunction (vector<int> i,vector<int> j) { return (i[0]<j[0]);} 
struct myclass { 
    bool operator() (vector<int> i,vector<int> j) { return (i[0]<j[0]);} 
} myobject; 


... 
ofstream fout ("retele.out"); 
fout << T << '\n'; 
for (i=1; i<=T; i++) 
{ 
    fout << sol[i].size() << ' '; 
    sort(sol[i].begin(),sol[i].end(), myfunction); 
    for (j=0; j<sol[i].size(); j++) 
     fout << sol[i][j] << ' '; 
    fout << '\n'; 
} 
fout.close(); 

あなたはvectorsのリストをソートするために、後で使用される独自のコンパレータを実装する必要があります。

+0

これは高価な比較/ソートになるでしょう、私はあなたの 'my​​function'のベクトルにconst-refsを受け入れることをお勧めします – JVApen

0

あなたはこのアルゴリズムに従うことができます:

// sort algorithm example 
#include <iostream>  // std::cout 
#include <algorithm> // std::sort 
#include <vector>  // std::vector 

bool myfunction (int i,int j) { return (i<j); } 

struct myclass { 
bool operator() (int i,int j) { return (i<j);} 
} myobject; 

int main() { 
int myints[] = {32,71,12,45,26,80,53,33}; 
    std::vector<int> myvector (myints, myints+8);    // 32 71 12 45 26 80 53 33 

    // using default comparison (operator <): 
    std::sort (myvector.begin(), myvector.begin()+4);   //(12 32 45 71)26 80 53 33 

     // using function as comp 
    std::sort (myvector.begin()+4, myvector.end(), myfunction); // 12 32 45 71(26 33 53 80) 

    // using object as comp 
     std::sort (myvector.begin(), myvector.end(), myobject);  //(12 26 32 33 45 53 71 80) 

    // print out content: 
    std::cout << "myvector contains:"; 
     for (std::vector<int>::iterator it=myvector.begin(); it!=myvector.end(); ++it) 
     std::cout << ' ' << *it; 
    std::cout << '\n'; 

    return 0; 
    } 
0

STL機能std::sortは、オプションの述語が含まれています。リストが空のベクトルが含まれている場合、このコードがクラッシュすること

std::sort(sol.begin(), sol.end(), [](const std::vector<int> &lhs, const std::vector<int> &rhs) { return lhs.front() < rhs.front(); }); 

注:この場合 は、私はあなたがソートのようなあなたを書きたいと思います。

solがstd::listではない場合は、Cスタイルの配列std::vector<int> v[50001]のようにコメントに示されています。

std::sort(sol[0], sol[nofElementsFilledIn], [](const std::vector<int> &lhs, const std::vector<int> &rhs) { return lhs.front() < rhs.front(); }); 
0
std::sort(MyVector.begin(), MyVector.end(), [](auto a, auto b) { return a.front() < b.front(); });