2012-04-19 35 views
1

我如何通過一個char向量爲char *?我知道這個問題很容易用一個帶有SIZE常量的預定義的char []數組來解決,但我想要一個矢量的靈活性,因爲沒有預定義的大小。傳遞載體<char>的指針的char *

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++; 
     } 
    } 
} 

回答

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



} 



/* 
+0

-1:錯誤的代碼格式,實際上不是一個答案原來的問題。 – Griddo 2014-07-31 08:28:03

3

最明顯的是通過&your_vector[0]。請務必首先將NUL添加到矢量的末尾。

或者,使用std::string代替std::vector<char>,在這種情況下,你可以得到一個NULL結尾的字符串的c_str成員函數。

編輯:我不得不懷疑,爲什麼getnmberofwords將被寫入接受char *,除非它是一些你不能遠離使用的舊C代碼。

鑑於「字」計數的一些話在一個字符串開始時的典型定義可以做這樣的事情:

std::istringstream buffer(your_string); 

size_t num_words = std::distance(std::istream_iterator<std::string>(buffer), 
           std::istream_iterator<std::string>()); 
+0

我試過的std :: string – user1066524 2012-04-19 05:12:04

+0

錯誤C2664: 'getnumberofwords':無法從 '的std :: string' 轉換參數1到 '字符*' – user1066524 2012-04-19 05:12:10

+0

@ user1066524:是的,正如我所說,你需要使用'c_str()',如:getnumberofwords(your_string.c_str()); ' – 2012-04-19 05:13:37

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++; 
     } 
    } 
} 

沒有方法將類型從向量轉換爲指針。

+0

這只是使用指針的地址,而不是數值。 – user1066524 2012-04-19 05:16:53

+0

有一種獲取[指向底層數組的指針]的方法(http://en.cppreference.com/w/cpp/container/vector/data)。 – 2012-04-19 05:40:09

3

您可以使用data()成員得到的指針爲基礎數組:

getnumberofwords(input.data()); 
+0

我會試試這個。我現在有點擔心,因爲我嘗試了簡單的方法,而不使用vector,並使用了預定義的char數組SIZE。但不幸的是,這也完全無法工作。所以我不能因爲某種原因讓程序工作。直到我們接觸到指針時,我纔對C++有任何困難。現在我瘋了。我的輸出不斷給我一個0.它就像for循環根本沒有做任何事情。我不知道這個問題是不是很嚴重。我嘗試過調試,它似乎很好,因爲它確實將字符串傳遞給char *,但之後它似乎不起作用。 – user1066524 2012-04-19 20:08:12

+0

@ user1066524「我沒有任何困難與C++,直到我們得到指針」就像說「我沒有任何生活的困難,直到我出生」 – Griddo 2014-07-31 08:29:35