2012-04-23 22 views
0

I am referring you to a previous link that compares the performance of qsort vs stdsortqsortとstd :: sortの比較

大文字のstd::mapが入力され、配列を並べ替えるCプログラムを作成しました。現在はqsortを使用しています。

typedef std::map<uint16_t, uint32_t> TSrcMap; 
TPSrcMap sp; 
TSrcMap::iterator its; 
/*Code to populate the array_start.*/ 

/*Code to populate the array_end.*/ 

typedef struct port_count 
{ 
     uint32_t port_number; 
     uint32_t port_count; 
}port_count_t; 

port_count_t pcount[10]; 
memset(pcount,0,sizeof(pcount)); 
size_t structs_len = sizeof(pcount)/sizeof(port_count_t); 
for(its = stcp.begin(); its != stcp.end();its++) 
{ 
     if(pcount[smallest_index].port_count < (*its).second) 
     { 
      pcount[smallest_index].port_count = (*its).second; 
      pcount[smallest_index].port_number = (*its).first; 
      /*qsort(pcount, structs_len, sizeof(port_count_t), struct_cmp_by_port_count);*/ 
      std::sort(pcount,sizeof(port_count_t)); 
     } 
} 

qsort関数は、配列を正しくソートしています。私はqsortstd::sortのパフォーマンスを比較したいと思いますstd::sortはなく、コールstd::sort呼び出しが‘sort(port_count_t [10], long unsigned int)’

にコンパイルエラー

に電話に該当する機能を付与されていないとqsortのパフォーマンスを比較したいですアルゴリズム。それ、どうやったら出来るの?

+0

ご質問にお答えいただいた場合は、対応する回答に同意してください。 –

答えて

6

の署名がある:

template <class RandomAccessIterator, class StrictWeakOrdering> 
void sort(RandomAccessIterator first, RandomAccessIterator last, 
      StrictWeakOrdering comp); 

だから、2つのイテレータコンパレータなく、ポインタ及び長さを必要とします。あなたのコードを修正するには、

std::sort(pcount, pcount+structs_len, cmp_port_count); 

cmp_port_countは、参照することにより2つのport_count_tオブジェクトを取り、最初の引数はfalseそうでない場合、第二引数の前に注文する場合trueを返すあなたのコンパレータ機能であると仮定して呼び出します。

+2

+1。最後の部分は重要です。 'std :: sort'はコンパレータのために別の規約を使います。 – MSalters

0

呼び出してみてください。引数は、Aの開始と終了イテレータとして

std::sort(pcount,pcount + 10); 

のstd ::ソートがかかります。したがって、配列をソートするには、配列の先頭にポインタを渡し、配列の最後の後にある要素(常にarray_pointer + array_size)へのポインタを渡します。

std::sort()
関連する問題