【问题标题】:bubbleSort vector template classbubbleSort 向量模板类
【发布时间】:2014-05-15 02:31:21
【问题描述】:

我正在处理一项任务,我需要使用模板对向量进行排序,以便我可以将不同的数据类型传递给我的类,并且不使用 std::sort 对它们进行排序。所以到目前为止我很困, 我真的不知道如何使用模板来接受来自 main() 的输入。 PS。这是为了作业,这就是为什么我不使用数组和排序函数。

这是我目前的代码。

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:(.text+0x11): 未定义引用SortableVector<int>::SortableVector()' Hw8_3.cpp:(.text+0x138): undefined reference toSortableVector::~SortableVector()' Hw8_3.cpp:(.text+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] << " ";
  }

}

还有其他想法吗?我真的迷路了……

【问题讨论】:

    标签: c++ vector bubble-sort


    【解决方案1】:

    如果您使用的是模板,您的swap 变量必须属于该模板类型。即:

    T swap = a[j];
    

    编辑:仔细看,我认为您使用的设计不正确。你的SortableVector 应该有一个vector&lt;T&gt; 作为成员,IE:

    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类的原因,您可以将函数作为模板函数放在全局空间中。

    【讨论】:

    • 也许这个想法是从向量类继承?这不是一个好主意,但这可能是一些混乱所在
    • 我明白了,非常感谢,这是我第一次使用矢量,所以我不确定我要去哪里。我的方向是;编写一个类模板 SortableVector。该类应该有一个成员函数,可以按升序对向量元素进行排序……而且我们不应该使用 std::sort。所以我考虑实现冒泡排序......现在我发现我的逻辑可能是错误的。
    【解决方案2】:

    您需要为构造函数和析构函数提供定义。

    如果您不声明它们,编译器会自动为您创建它们。构造函数将调用每个元素的默认构造函数(如果有基类,则调用基类),析构函数将调用每个类元素的析构函数。

    但是,既然你已经声明了它们,你还需要提供定义。

    【讨论】:

      【解决方案3】:

      错误很明显:

      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&lt;int&gt;::bubble_sort(a);为什么不只是L.buble_sort(a);
      另外,这里:

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

      您知道您传递的是a 的副本而不是引用吗?这意味着您对该向量的任何修改都不会在您的主函数或您从中调用它的任何函数中的vector&lt;int&gt; a; 中生效:

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

      注意&amp;

      这个:

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

      你是说a吗?

      这个类成员没用:

      vector&lt;T&gt; vec;

      我建议提供附加到该成员的函数,并从类中的 bubble_sort 函数中删除 vector&lt;T&gt;&amp; a 参数,然后不要在你的 main 或 w/e 中创建另一个向量,而是在你的SortableVector 类实例。

      【讨论】:

        【解决方案4】:
        #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;
            }
        }
        

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 1970-01-01
          • 2013-04-19
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 2019-10-22
          • 1970-01-01
          相关资源
          最近更新 更多