将vector <char>传递给指针char * </char>

时间:2012-04-19 04:50:46

标签: c++ pointers vector

如何将char矢量传递给char *?我知道这个问题可以通过带有SIZE const的预定义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++;
        }
    }
}

4 个答案:

答案 0 :(得分:4)

您可以使用data()成员获取指向底层数组的指针:

getnumberofwords(input.data());

答案 1 :(得分:3)

最明显的是通过&your_vector[0]。请务必先将NUL添加到矢量的末尾。

或者,使用std::string代替std::vector<char>,在这种情况下,您可以使用c_str成员函数获取以NUL结尾的字符串。

编辑:但是,我不得不怀疑为什么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>());

答案 2 :(得分: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++;
         }
   }
}

没有将类型从向量转换为指针的方法。

答案 3 :(得分:-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;



}



/*