2014-05-15 68 views
0

我正在處理一個任務,我需要使用模板對矢量進行排序,以便我可以將不同的數據類型傳遞給我的類,並使用std :: sort對它們進行排序。所以我很困難, 我真的不知道如何使用該模板接受來自main()的輸入。 PS。這是爲了家庭作業,這就是爲什麼我不使用數組和排序功能。bubbleSort矢量模板類

這是我的代碼到目前爲止。

using namespace std; 

template <class T> 
class SortableVector 
{ 
private: 
T a =0; 
public: 
    SortableVector();     // constructor 
~SortableVector();    // destructor 

void bubble_sort(vector<T>, a) 


{ 
    for (int i = a.size(); i > 0;i--) 
    { 
     for (int j = 0, k = 1; k < i;j++, k++) 
     { 
     if (a[j] > a[k]) 
     { 
      int swap = a[j]; 
      a[j] = a[k]; 
      a[k] = swap; 
     } 
    } 
    } 
} 
}; 

而我主要是尋找這樣的:

int main() 
{ 


    int alen, val; 
    vector<int> a; 
    cout << "Enter the number of elements : "; 
    cin >> alen; 
    for(int i = 0; i < alen; i++) 
    { 
    cin >> val; 
    a.push_back(val); 
    } 
    SortableVector::bubble_sort(a); 
    cout << "List of sorted elements: " << endl; 
    for(int i = 0; i < alen; i++) 
    { 
    cout << a[i] << " "; 
    } 
     cout << endl; 
} 

任何幫助將受到歡迎:)

好...... 所以我做了一些改動感謝Namfuak

現在我有一個完全不同的問題

命令行輸出; 。

Hw8_3.cpp :(文本+ 0×11):未定義參照SortableVector<int>::SortableVector()' Hw8_3.cpp:(.text+0x138): undefined reference to SortableVector ::〜SortableVector()」 Hw8_3.cpp :(文本+量0x170):未定義參考`SortableVector ::〜SortableVector() ' collect2:錯誤:ld返回1退出狀態

我真的沒有得到這個。這是我迄今爲止的代碼;

template <class T> 
class SortableVector 
{ 
private: 
vector<T> vec; 
public: 
    SortableVector();     // constructor 
~SortableVector();    // destructor 
void push_back(T push) {vec.push_back(push);} 
T bubble_sort(vector<T> a); 
}; 

template <class T> 
T SortableVector<T>::bubble_sort(vector<T> a) 
{ 


for (int i = a.size(); i > 0;i--) 
{ 
    for (int j = 0, k = 1; k < i;j++, k++) 
    { 
    if (a[j] > a[k]) 
    { 
     T swap = vec[j]; 
     vec[j] = vec[k]; 
     vec[k] = swap; 
    } 
} 
    }return 0; 
} 

而我的main();

{ 
SortableVector<int> L; 

    int alen, val; 
    vector<int> a; 
    cout << "Enter the number of elements : "; 
    cin >> alen; 
    for(int i = 0; i < alen; i++) 
    { 
    cin >> val; 
    L.push_back(val); 
    } 
L.SortableVector<int>::bubble_sort(a); 
    cout << "List of sorted elements: " << endl; 
    for(int i = 0; i < alen; i++) 
    { 
    cout << a[i] << " "; 
    } 

} 

還有其他想法嗎?我真的迷失在這裏...

回答

0

如果您使用模板,您的swap變量需要是該模板類型。 IE:

T swap = a[j]; 

編輯:看透了,我不認爲你使用正確的設計。你可能SortableVector應該有一個vector<T>成爲會員,即:

template <class T> 
class SortableVector 
{ 
private: 
    std::vector<T> vec; 
public: 
    SortableVector(); 
    ~SortableVector(); 
    void push_back(T push) {vec.push_back(push);} 
    void bubble_sort() //You could also return a sorted version of vec 
} 

否則,沒有理由爲SortableVector類,你可以只取功能,並把它在全球空間作爲模板函數。

+0

也許這個想法是從矢量類繼承?這不是一個好主意,但這可能是一些混淆的地方 – Henry

+0

我看,非常感謝,這是我第一次使用矢量,所以我不確定我要去哪裏。我的方向是;編寫一個類模板SortableVector。該類應該有一個按照升序排列向量元素的成員函數...我們不應該使用std :: sort。所以我想到了實施冒泡排序......現在我發現我的邏輯可能是錯誤的。 – Mrodri13

0

您需要提供構造函數和析構函數的定義。

如果你不聲明它們,編譯器會自動爲你創建它們。構造函數將調用每個元素(和基類,如果有的話)的默認構造函數,析構函數將調用每個類元素的析構函數。

但是,既然您已經聲明瞭它們,您還需要提供定義。

0

錯誤是相當清楚的:

Hw8_3.cpp:(.text+0x11): undefined reference to SortableVector<int>::SortableVector()' 
Hw8_3.cpp:(.text+0x138): undefined reference toSortableVector::~SortableVector()' Hw8_3.cpp:(.text+0x170): undefined reference to `SortableVector::~SortableVector()' collect2: error: ld returned 1 exit status 

你沒有定義構造函數和析構函數...

SortableVector() { } 
~SortableVector() { } 

或者,如果你想:

template<class T> 
SortableVector::SortableVector() 
{ 
... 
} 

template<class T> 
SortableVector::~SortableVector() 
{ 
... 
} 

另外,該線路L.SortableVector<int>::bubble_sort(a);爲什麼不L.buble_sort(a);
此外,在這裏:

template <class T> 
T SortableVector<T>::bubble_sort(vector<T> a) 

你知道你傳遞一個副本a而不是參考?這意味着你到矢量任何修改不會住在vector<int> a;在主功能或任何功能你從調用它:

template <class T> 
T SortableVector<T>::bubble_sort(vector<T>& a) 

通知的&

此:

vec[j] = vec[k]; 
    vec[k] = swap; 

您的意思是a

此類部件是無用:

vector<T> vec;

我建議用於附加到該構件代替並刪除vector<T>& a參數從bubble_sort函數類中的,那麼,而不是使另一矢量提供功能在你的主或w/e中,使用SortableVector類實例推送。

0
#include <vector> 
#include <algorithm> //This library give us some handy function swap 
#include <iostream> 
#include <random> 

// This is a function to create random number between 1 and 10 
static int random_int(){ 
    static std::random_device rd; 
    static std::mt19937 prng{ rd() }; 
    static std::uniform_int_distribution<> d10{1, 10}; 
    return d10(prng); 
} 


template<class T> 
class SortableVector{ 
    //By default element define in classes are private so no need to specify 
    std::vector<T> v_; 
public: 
    //You dont need to specify constructor 
    //Also, you dont need to specify destructor. 
    //This is because the default constructor and destructor are ok. 
    //The default constructor use item to item copy. In this case it's gonna copy the vector val_ wich is ok. 
    //For the destructor, we don't need to do anything 

    //Usually you use const T& to avoid copy 
    void push_back(const T& val){ 
     v_.push_back(val); 
    } 

    //Here i don't really know what u want to do. 
    //Do you want to sort your inner vector and return it ? 
    //Or do you want to make a copy and return it ? 

    //Let's make a static method so it helps us covering both cases. 
    //This method is static so it can be used outside of an instance. 
    //It's like a function that is define inside the scope of this class. 
    static void bubble_sort(std::vector<T>& v){ 
     //Using the correct type is always better. 
     //In this case it's the type that define the size of this particular vector is : 
     size_t size = v.size();//Or with c++11 : auto size = v.size() 
     bool change; 
     do{ 
      change = false; 
      for(int i = 0; i < size - 1; ++i){ 
       if(v[i-1] > v[i]){ 
        std::swap(v[i-1],v[i]); 
        change = true; 
       } 
      } 
     } 
     while(change); 
    } 

    //This is the method version using the static one. 
    std::vector<T>& bubble_sort(){ 
     bubble_sort(v_); 
     return v_; 
    } 
}; 
int main() { 
    SortableVector<int> ss; 
    for(int k = 0; k < 10; ++k){ 
     ss.push_back(random_int()); 
    } 
    for(auto& elem : ss.bubble_sort()){//C++ 11 foreach 
     std::cout << elem << std::endl; 
    } 
}