3

char ベクトルを char* に渡すにはどうすればよいですか? この問題は、事前定義された char[] 配列と SIZE const を使用して簡単に解決できることはわかっていますが、事前定義されたサイズがないため、ベクトルの柔軟性が必要です。

using namespace std;

//prototype 
void getnumberofwords(char*);

int main() {
    //declare the input vector
    vector<char> input;

    /*here I collect the input from user into the vector, but I am omitting the code here for sake of brevity...*/

    getnumberofwords(input); 
    //here is where an ERROR shows up: there is no suitable conversion from std::vector to char*                     
    return 0;
}

void getnumberofwords(char *str){
    int numwords=0;
    int lengthofstring = (int)str.size();  
    //this ERROR says the expression must have a case

    //step through characters until null
    for (int index=0; index < lengthofstring; index++){
        if ( *(str+index) == '\0') {
            numwords++;
        }
    }
}
4

4 に答える 4

7

member を使用data()して、基になる配列へのポインターを取得できます。

getnumberofwords(input.data());
于 2012-04-19T05:33:02.000 に答える
3

最も明白なのは pass&your_vector[0]です。ただし、最初にベクトルの最後に NUL を追加してください。

std::stringまたは、代わりに を使用します。この場合、メンバー関数を使用std::vector<char>して NUL で終了する文字列を取得できます。c_str

編集:しかし、使用から逃れることができない古いCコードでない限り、なぜgetnmberofwordsa を受け入れるように書かれるのでしょうか。char *

文字列で始まるいくつかの単語を数える「単語」の典型的な定義を考えると、次のようにすることができます:

std::istringstream buffer(your_string);

size_t num_words = std::distance(std::istream_iterator<std::string>(buffer),
                                 std::istream_iterator<std::string>());
于 2012-04-19T04:54:50.943 に答える
0

ベクトルの参照を関数 getnumberofwords に渡す必要があります。

void getnumberofwords(vector<char>& str){
int numwords=0;
int lengthofstring = str.size(); 
   for (int index=0; index < lengthofstring; index++){
        if ( str[index] == '\0') {
           numwords++;
         }
   }
}

型をベクトルからポインターに変換する方法はありません。

于 2012-04-19T05:02:54.513 に答える
-1

これが私がやったことでうまくいったことです:

#include <iostream>
#include <cstring>
#include <string>
#include <iomanip>



using namespace std;

//prototype

void getnumberofwords(char*);
void getavgnumofletters(char*, int);



int main() {

    const int SIZE=50;
    char str[SIZE];

    cout<<"Enter a string:";
    cin.getline(str, SIZE);




    getnumberofwords(str);


    return 0;

 }


void getnumberofwords(char *str){
    int numwords=0;

    int lengthstring=strlen(str);  







    //step through characters until null
  for (int index=0; index < lengthstring; index++){
        if (str[index] ==' ') {
               numwords++;
         }else{

          continue;
        }


   }
     numwords+=1;
    cout<<"There are "<<numwords<<" in that sentence "<<endl;
    getavgnumofletters(str, numwords);



}



void getavgnumofletters(char *str, int numwords) {
    int numofletters=0;

    double avgnumofletters;
    int lengthstring=strlen(str);


    //step through characters until null
  for (int index=0; index < lengthstring; index++){
        if (str[index] != ' ') {
               numofletters++;
         }else{
             continue;
        }


   }

       avgnumofletters = (double)numofletters/numwords;
       cout<<"The average number of letters per word is "<<setprecision(1)<<fixed<<avgnumofletters<<endl;



}



/*
于 2012-06-19T13:47:24.630 に答える