Vectors in C++ , bucket sort : Segmentation fault

﹥>﹥吖頭↗ 提交于 2021-02-08 12:07:28

问题


In my code, I am trying to implement bucket sort and in my implementation I have tried using vectors, but unfortunately I end up with errors with respect to vector functions.

Code :

#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]);
}

Errors :

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]);

Edited code :

#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]);

}

Edit : I have introduced the correction with respect to the push_back() but now upon running my code I hit a segmentation fault. Any suggestions?


回答1:


push_back is method of vector, not element. To append element (with increasing vector size by 1) use array.push_back(123). To assign something to element use array[i] = 123.

If you want to fill vector using assignment, you have to resize vector first: array.resize(count).

To sort, wirte sort(array.begin() , array.end()).

bucket[i][j] is totally incorrect: bucket is one dimensional vector. You probably want bucket to be vector<vector<float>>.


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

You have only count elements in bucket array but ask for count * array[i] element.




回答2:


Your code have many problems.

Main problem is following:

You can not do this:

    array[i].push_back(element);

You must do this instead:

    array.push_back(element);

Later you do the same:

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

But this time, probably you need just:

    bucket[bucket_index] = array[i];

Or if you want just to obtain a copy from vector called "array", you can just do:

    bucket = array;

If you post comment what bucket_sort should do, I can provide future explanations.

Finally, I also would suggest you to add:

    using MyVector = vector<float>;

Will safe you lots of typing.

Also you might define the function to use the vector by reference, because else it is copied and you probably do not want this:

void bucket_sort(MyVector &array, int count);



回答3:


Instead of array[i].push_back(element), use array.push_back(element). Element will go at the position automatically as a for loop index.

So, at the end of loop, array will have count number of elements as desired by you.

Use this logic everywhere in the code.



来源:https://stackoverflow.com/questions/37131591/vectors-in-c-bucket-sort-segmentation-fault

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!