テンプレートの特殊化の中でconst char *配列にstrcmp()を使用する際に問題があります。テンプレート特殊化内でstrcmp()の 'char'から 'const char *'への変換が無効です。
私のスクリプトでは、大きな値/長さから小さいものにいくつかの配列を並べ替える必要があります。それは整数と浮動小数点型の配列で動作しますが、const char *配列では機能しません。 3つのテンプレート関数を定義して宣言しました。 const char *配列では、配列をソートするためにstrcmp(const char *、const char *)関数を使用する特殊なテンプレート関数を使用します。
(最初のテンプレート宣言、第二主スクリプト、第三テンプレート関数の定義を)次のように私のスクリプトは次のとおりです。
#include <iostream>
#include <string.h>
using namespace std ;
// Template function declarations
template <class T>
void order(T& a, T& b) ;
template <class T>
void sort(T* c, int d) ;
template <class T>
void display(T* e, int f) ;
// Main
int main() {
int random[10] = {10,23,5,37,56,0,20,88,95,32} ; // Random Array of integers
float random_fl[10] = {9.5,66.2,5.8,41.1,89.4,0.6,23.4,66.5,90.9,57.7} ; // Random Array of floats
const char* random_char[] = {"blah", "blahblah", "string", "character", "literal", "one", "randomize", "unsigned", "red", "wolf"} ;
int length = sizeof(random)/sizeof(int) ; // Calculating the lenght of the array
int length_fl = sizeof(random_fl)/sizeof(float) ;
int length_char = sizeof(random_char)/sizeof(const char*) ;
cout << "Initial integer Array: "; // Terminal message giving the initial array
for (int i = 0; i < length; ++i) {
cout << random[i] << " ";
}
cout << endl;
cout << "Initial float Array: ";
for (int i = 0; i < length_fl; ++i) {
cout << random_fl[i] << " ";
}
cout << endl;
cout << "Initial character Array: ";
for (int i = 0; i < length_char; ++i) {
cout << random_char[i] << " ";
}
cout << endl;
sort(random, length) ; // Call sort() function to sort array
sort(random_fl, length_fl) ;
sort(random_char, length_char) ;
display(random, length) ; // Call display() function to print sorted array in terminal
display(random_fl, length_fl) ;
//display(random_char, length_char) ;
return 0 ;
}
// Template function definitions
template <class T>
void order(T& a, T& b) { // order function using references
T Temp = a ;
a = b ;
b = Temp ;
}
template <class T>
void sort(T* c, int d) { // Sorting function
for (int i=0; i<d-1; i++) {
for (int j=0; j<d-1-i; j++) {
if(c[j+1] > c[j]) {
order(c[j] , c[j+1]) ;
}
}
}
}
template<>
void sort(const char* a, int b) { // Template specialization sort function for character string
for (int i=0; i<b-1; i++) {
for (int j=0; j<b-1-i; j++) {
if(strcmp(a[j+1], a[j])>0) {
order(a[j], a[j+1]) ;
}
}
}
}
template <class T>
void display(T* e, int f) { // Display function
cout << "Sorted Array: ";
for (int i=0; i<f; i++) {
cout << e[i] << " ";
}
cout << endl ;
}
スクリプトをコンパイルすると、私は無効な変換が発生しているというエラーが出ます私の特殊なテンプレート関数の中のstrcmp(const char *、const char *)関数の 'char'から 'const char *'に変換します。なぜ私はconst char *配列を10個の文字列リテラルで定義しているのでしょうか?配列の要素a [j + 1]とa [j]は、const char * aの定義を持つ特殊なソート関数の開始時に期待されるので、const char *要素でなければなりません。
私はかなり新しくC++であり、ポインタを理解することが特に難しく、この問題の根本にあるポインタ/参照で配列要素を参照する方法が特にあります。
私の英語を気にしないでください。事前にありがとうございます。
編集:
シュタイナーによって指摘されているように意図したとおりにソートされた文字列を取得するには、私の場合は()文はまだ正しくないにもかかわらず、strcmpの()は、今取り組んでいます。
ご協力いただきありがとうございます。
です。あなたは何を期待しましたか?ああ、 '> 0'比較のあいだに括弧がない。 – Arkadiy
c-stringsを削除して 'std :: string'sを使うもう一つの理由があります。次に 'std :: sort'を使うこともできます。 – NathanOliver
@Arkadiy、はい、申し訳ありません。私は括弧を誤って消してしまったのを見る。 –