C ++中的向量,桶排序:分段错误

时间:2016-05-10 07:10:54

标签: c++ vector stl

在我的代码中,我正在尝试实现桶排序,在我的实现中,我尝试过使用向量,但不幸的是,我最终得到了有关向量函数的错误。

代码:

#include <iostream>
#include <algorithm>
#include <vector>

using namespace std;

void bucket_sort(vector<float> & , int); //vector is passed by reference


int main(){

vector<float> array;
float element;
int count;


cout << "\nEnter the size of the vector : ";
cin >> count;

cout << "\nEnter the elements into the vector : ";

for(vector<float>::size_type i = 0 ; i < count ; i++){
    cin >> element;
    array[i].push_back(element);
}

bucket_sort(array , count);
}

void bucket_sort(vector<float> array, int count){

    vector<float> bucket;

    for(int i = 0 ; i < count ; i++){
        int bucket_index = count * array[i];
        bucket[bucket_index].push_back(array[i]);
    }

    for(int i = 0 ; i < count ; i++)
        sort(bucket[i].begin() , bucket[i].end());

    int index = 0;

    for(int i = 0 ; i < count ; i++)
        for(int j = 0 ; j < bucket[i].size() ; j++)
            array[index++].push_back(bucket[i][j]);
}

错误:

bucket_sort.cpp: In function ‘int main()’:
bucket_sort.cpp:24:12: error: request for member ‘push_back’ in ‘array.std::vector<_Tp, _Alloc>::operator[]<float, std::allocator<float> >(i)’, which is of non-class type ‘float’
   array[i].push_back(element);
            ^
bucket_sort.cpp: In function ‘void bucket_sort(std::vector<float>, int)’:
bucket_sort.cpp:36:24: error: request for member ‘push_back’ in ‘bucket.std::vector<_Tp, _Alloc>::operator[]<float, std::allocator<float> >(((std::vector<float>::size_type)bucket_index))’, which is of non-class type ‘float’
   bucket[bucket_index].push_back(array[i]);
                        ^
bucket_sort.cpp:40:18: error: request for member ‘begin’ in ‘bucket.std::vector<_Tp, _Alloc>::operator[]<float, std::allocator<float> >(((std::vector<float>::size_type)i))’, which is of non-class type ‘float’
   sort(bucket[i].begin() , bucket[i].end());
                  ^
bucket_sort.cpp:40:38: error: request for member ‘end’ in ‘bucket.std::vector<_Tp, _Alloc>::operator[]<float, std::allocator<float> >(((std::vector<float>::size_type)i))’, which is of non-class type ‘float’
   sort(bucket[i].begin() , bucket[i].end());
                                      ^
bucket_sort.cpp:45:33: error: request for member ‘size’ in ‘bucket.std::vector<_Tp, _Alloc>::operator[]<float, std::allocator<float> >(((std::vector<float>::size_type)i))’, which is of non-class type ‘float’
   for(int j = 0 ; j < bucket[i].size() ; j++)
                                 ^
bucket_sort.cpp:46:19: error: request for member ‘push_back’ in ‘array.std::vector<_Tp, _Alloc>::operator[]<float, std::allocator<float> >(((std::vector<float>::size_type)(index ++)))’, which is of non-class type ‘float’
    array[index++].push_back(bucket[i][j]);
                   ^
bucket_sort.cpp:46:40: error: invalid types ‘float[int]’ for array subscript
    array[index++].push_back(bucket[i][j]);

编辑代码:

#include <iostream>
#include <algorithm>
#include <vector>

using namespace std;

void bucket_sort(vector<float> & , int); //vector is passed by reference


int main(){

vector<float> array;
float element;
int count;


cout << "\nEnter the size of the vector : ";
cin >> count;

cout << "\nEnter the elements into the vector : ";

for(int i = 0 ; i < count ; i++){
    cin >> element;
    array.push_back(element);
}

bucket_sort(array , count);

cout << "\nSorted vector : ";

for(int i = 0 ; i < count ; i++)
    cout << array[i] << " ";
 }

void bucket_sort(vector<float>& array, int count){

vector<float> bucket[count];

for(int i = 0 ; i < count ; i++){
    int bucket_index = count * array[i];
    bucket[bucket_index].push_back(array[i]);
}

for(int i = 0 ; i < count ; i++)
    sort(bucket[i].begin() , bucket[i].end());

int index = 0;

for(int i = 0 ; i < count ; i++)
    for(int j = 0 ; j < bucket[i].size() ; j++)
        array.push_back(bucket[i][j]);

}

编辑: 我已经介绍了关于push_back()的更正,但现在运行我的代码时遇到了分段错误。有什么建议吗?

3 个答案:

答案 0 :(得分:2)

push_backvector的方法,而不是元素。要追加元素(向量大小增加1),请使用array.push_back(123)。要为元素指定内容使用array[i] = 123

如果要使用赋值填充矢量,则必须先调整矢量大小:array.resize(count)

要排序,请sort(array.begin() , array.end())

bucket[i][j]完全错误:bucket是一维向量。您可能希望bucketvector<vector<float>>

for(int i = 0 ; i < count ; i++){
    int bucket_index = count * array[i];
    bucket[bucket_index].push_back(array[i]);
}

count数组中只有bucket个元素,但要求count * array[i]元素。

答案 1 :(得分:1)

您的代码有很多问题。

主要问题如下:

你不能这样做:

    array[i].push_back(element);

你必须这样做:

    array.push_back(element);

稍后你会这样做:

    bucket[bucket_index].push_back(array[i]);

但这一次,你可能只需要:

    bucket[bucket_index] = array[i];

或者,如果您只想从名为&#34; array&#34;的矢量中获取副本,您可以这样做:

    bucket = array;

如果您发表评论bucket_sort应该做什么,我可以提供以后的解释。

最后,我还建议你添加:

    using MyVector = vector<float>;

可以安全地打字。

此外,您可以定义函数以通过引用使用向量,因为除非它被复制,您可能不希望这样:

void bucket_sort(MyVector &array, int count);

答案 2 :(得分:0)

而不是array[i].push_back(element),请使用array.push_back(element)。元素将自动作为for循环索引进入该位置。

因此,在循环结束时,数组将根据您的需要计算元素的数量。

在代码中的任何地方使用此逻辑。