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

3

如何将char向量传递给char*?我知道这个问题可以通过预定义的char[]数组和一个SIZE常量轻松解决,但我想要向量的灵活性,因为没有预定义的大小。

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个回答

7
您可以使用data()成员获得指向底层数组的指针:
getnumberofwords(input.data());

我会试一下这个。但现在我有点担心,因为我尝试了不使用向量并使用预定义的char数组SIZE来简化操作,但不幸的是那也完全没用。所以出于某种原因,我无法让程序正常工作。在指针方面,我之前没有遇到过任何困难,但现在我快疯了。我的输出一直给我0,就好像for循环根本没有起作用。我想知道问题是否出在strlen上。我尝试进行调试,它似乎在将字符串传递给char*时没问题,但之后似乎就不起作用了。 - user1066524
@user1066524说:“在指针之前,我对C++没有遇到任何困难”,就像说“在出生之前,我对生活没有遇到任何困难”。 - Gerrit-K

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

@user1066524:是的,正如我所说,你需要使用c_str(),例如:getnumberofwords(your_string.c_str()); - Jerry Coffin
通过使用“your first option”和“your_vector[0]”,我认为你展示的是类似于我在MSDN网站上看到的vector::pointer示例。http://msdn.microsoft.com/en-us/library/7e4tx21z(v=vs.80).aspx 但这并不起作用,因为它只是通过创建一个地址,然后添加索引到该地址,最后分配一个新值。 - user1066524
1
@user1066524:恐怕我无法理解您为什么认为&some_vector[0]不起作用的解释。 - Jerry Coffin
你问为什么我必须将它传递给char*——我在C++中的任务要求我们将cstring作为函数参数传递给指针。我们被要求创建两个函数。在主函数中,我们需要向用户询问一个字符串:例如,“Four score and seven years ago”,然后将该输入作为参数传递给获取字符串中单词数并打印出来的函数。然后将字符串中单词数和指针传递给查找字符串中字母平均数的函数,并打印出平均值。 - user1066524
@user1066524:你能传递一个指向字符串的指针而不是一个指向字符的指针吗? - Jerry Coffin
显示剩余3条评论

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

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


这里只使用指针的地址,而不是值。 - user1066524
有一种方法可以获取指向底层数组的指针。 - David Brown

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



}



/*

-1:代码格式不良,实际上并没有回答原始问题。 - Gerrit-K

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接