【问题标题】:passing vector<char> to a pointer char*将 vector<char> 传递给指针 char*
【发布时间】:2012-04-19 04:50:46
【问题描述】:

如何将 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++;
        }
    }
}

【问题讨论】:

    标签: c++ pointers vector


    【解决方案1】:

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

    getnumberofwords(input.data());
    

    【讨论】:

    • 我会试试这个。我现在有点担心,因为我尝试在没有向量的情况下使用简单的方法并使用预定义的 char 数组 SIZE。但不幸的是,这也根本不起作用。所以由于某种原因,我根本无法让程序运行。在我们得到指针之前,我对 c++ 没有任何困难。现在我快疯了。我的输出一直给我一个 0。就像 for 循环根本没有做任何事情一样。我想知道问题是否是strlen。我尝试调试,它似乎很好,因为它确实将字符串传递给 char*,但之后它似乎没有工作。
    • @user1066524 “在我们得到指针之前,我在 C++ 方面没有遇到任何困难”就像在说“在我出生之前我的生活没有任何困难”
    【解决方案2】:

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

    或者,使用std::string 而不是std::vector&lt;char&gt;,在这种情况下,您可以使用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>());
    

    【讨论】:

    • 错误 C2664: 'getnumberofwords' : 无法将参数 1 从 'std::string' 转换为 'char *'
    • @user1066524:是的,正如我所说,您需要使用c_str(),例如:getnumberofwords(your_string.c_str());`
    • 使用您的第一个选项 &your_vector[0],我认为您所展示的内容就像我在 MSDN 网站上看到的 vector::pointer 示例。 msdn.microsoft.com/en-us/library/7e4tx21z(v=vs.80).aspx 但这不起作用,因为它只是通过并创建一个地址,然后添加并索引到该地址,然后分配一个新值。
    • @user1066524:恐怕我不能完全理解你为什么认为&amp;some_vector[0] 行不通的解释。
    • 你问我为什么必须将它传递给 char* ----我在 C++ 中的分配要求我们将 cstring 作为函数中的参数传递给指针。我们被要求创建两个函数。主要我们需要向用户询问一个字符串:例如,“四分七年前”,然后将该输入作为参数并将其传递给获取字符串中有多少单词并打印出来的函数.然后传递字符串中有多少个单词和指向一个函数的指针,该函数可以找到字符串中的平均字母数。然后打印出平均值。
    【解决方案3】:

    您应该将向量的引用传递给函数 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++;
             }
       }
    }
    

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

    【讨论】:

    【解决方案4】:

    这就是我最终做的事情:

    #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:错误的代码格式,实际上不是原始问题的答案。
    猜你喜欢
    • 2021-09-07
    • 2018-08-11
    • 1970-01-01
    • 2016-03-27
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多