2016-03-25 21 views
1

Aşağıdaki program için, artan sipariş ve azalan sırayı senkronize edemiyorum. Çıkışta gördüğünüz gibi artan ve azalan düzen aynı değildir.int içerisinden daha az işleç tanımlamak için int

Programı:

#include <iostream> 
#include <vector> 
#include <algorithm> 
#include<stdio.h> 

using namespace std; 

struct box{ 
    vector<int> dim; 
    int index; 
    box(vector<int> temp, int ind) 
    { 
     dim = temp; 
     index = ind; 
    } 
    bool operator<(const box &rhs) const 
    { 
     for(int i = 0; i < dim.size(); i++) 
     { 
      if(dim[i] >= rhs.dim[i]) 
      { 
       return false; 
      } 
     } 
     return true; 
    } 

    void print() 
    { 
     for(int i = 0 ; i < dim.size(); i++) 
     { 
      cout<<dim[i]<<"\t"; 
     } 
     cout<<endl; 
    } 
}; 

int main() 
{ 
    int n,k; 

    while(scanf("%d %d", &k, &n) == 2){ 
     vector<box> arr; 
     vector<box> newarr; 
     for(int i = 0; i < k ; i++) 
     { 
      vector<int> temp; 
      for(int j = 0; j < n ; j++) 
      { 
       int a; 
       cin>>a; 
       temp.push_back(a); 
       std::sort(temp.begin(), temp.end()); 
      } 
      arr.push_back(box(temp,i+1)); 
      newarr.push_back(box(temp,i+1)); 
     } 

     std::sort(arr.begin(), arr.end()); 
     cout<<"Increasing Order"<<endl; 
     for(int i =0 ; i < k ; i++) 
     { 
      arr[i].print(); 
     } 
     std::sort(newarr.rbegin(), newarr.rend()); 
     cout<<"Decreasing Order"<<endl; 
     for(int i =0 ; i < k ; i++) 
     { 
      newarr[i].print(); 
     } 
    } 

    return 0; 
} 

Girdi:

27 2 
39 26 
63 17 
64 46 
75 13 
26 25 
21 45 
15 22 
41 41 
98 92 
27 81 
37 65 
39 25 
53 50 
72 55 
12 42 
66 65 
10 96 
90 90 
93 77 
24 70 
64 49 
87 79 
33 99 
59 11 
49 43 
43 31 
76 85 

My Çıktı:

Increasing Order 
12 42 
24 70 
25 39 
11 59 
15 22 
25 26 
21 45 
41 41 
31 43 
43 49 
37 65 
46 64 
50 53 
17 63 
26 39 
33 99 
49 64 
90 90 
77 93 
10 96 
65 66 
55 72 
13 75 
27 81 
76 85 
79 87 
92 98 
Decreasing Order 
76 85 
33 99 
92 98 
79 87 
77 93 
90 90 
10 96 
65 66 
55 72 
27 81 
37 65 
50 53 
46 64 
49 64 
43 49 
41 41 
31 43 
26 39 
24 70 
17 63 
13 75 
11 59 
25 26 
21 45 
12 42 
25 39 
15 22 
+0

Döngüyü aşağıdaki gibi yapmak tehlikelidir (int i = 0; ben CppChris

cevap

2

Sadece yazmak

bool operator<(const box &rhs) const 
{ 
    return dim < rhs.dim; 
} 
2
std::vector<int> a = {0,1,2}; 
std::vector<int> b = {1,2,3}; 
std::cout << (a < b); 

çıkışları 1.

Here vektör aynı zamanda diğer operatörleri uygular bulabilirsiniz:!doğrudan std::vector::operator< (o sözlük sırasında benzerlikler nelerdir olacaktır), örnek kullanabilirsiniz =, =, <, < =, >,> =