2016-05-10 26 views
-1

私のコードでは、バケットソートを実装しようとしていますが、私の実装ではベクトルを使用しようとしましたが、残念ながらベクトル関数に関してエラーが発生します。C++のベクトル、バケットソート:セグメンテーションフォールト

コード:

エラー:

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

} 

編集:私は一back(に対する補正を導入しました)が、今に応じてきた 私のコードを実行すると、セグメンテーション違反に遭いました。助言がありますか?

+0

ベクトル自体ではなく、ベクトルの要素に対して 'push_back'を呼び出そうとしています。 –

+0

あなたは 'array [i] .push_back..'のようにすることはできません。あなたはベクトルを押すのではなく、 'float'であるベクトルの要素に渡してエラーを出します。 –

答えて

2

push_backは、vectorのメソッドであり、要素ではありません。要素を追加するには(ベクトルサイズを1増加させる)、array.push_back(123)を使用します。要素に何かを割り当てるには、array[i] = 123を使用します。

割り当てを使用してベクトルを塗りつぶしたい場合は、ベクトルのサイズを最初に変更する必要があります。array.resize(count)

ソートするには、sort(array.begin() , array.end())を入力します。

bucket[i][j]は完全に間違っています。bucketは1次元ベクトルです。 bucketvector<vector<float>>になります。


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

あなたはbucket配列にのみcountの要素を持っていますが、count * array[i]要素をお願いいたします。

+0

ありがとう、非常に!それは正常にコンパイルされます。 –

1

コードには多くの問題があります。

主な問題は以下の通りです:

あなたがこれを行うことはできません。

array.push_back(element); 

後で、同じ操作を行います。:

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

array[i].push_back(element); 

あなたは代わりに、これを行う必要があります

しかし今回は、あなただけの必要Y:

bucket[bucket_index] = array[i]; 

それとも、単に「アレイ」と呼ばれるベクターからコピーを入手したい場合は、あなただけで行うことができます。

bucket = array; 

あなたはbucket_sortは何をすべきかコメントを投稿する場合は、Iを将来の説明を提供することができます。

最後に、私はまた、あなたが追加することをお勧め:タイピングの

using MyVector = vector<float>; 

ウィル安全ますたくさん。

void bucket_sort(MyVector &array, int count); 
0

代わりのarray[i].push_back(element)array.push_back(element)を使用します。

また、あなたはそれがコピーされ、あなたはおそらくこれたくない他ので、参照することにより、ベクターを使用する関数を定義することができます。要素は自動的にforループのインデックスとしてその位置に移動します。

ループの最後では、配列はあなたの望むように要素数をカウントします。

このロジックはコードのどこでも使用できます。

関連する問題