简体   繁体   English

C++ 将字符串调用到函数中?

[英]C++ Call string into function?

Not sure how to exactly explain this, sorry.不知道如何准确解释这一点,抱歉。 I'm creating a function to find the first instance of a char in an array built by a given string.我正在创建一个函数来查找由给定字符串构建的数组中的第一个字符实例。 I have the function to create an array from the string and loop through the array, but not sure how to put it the array into the find function.我有从字符串创建数组并遍历数组的函数,但不确定如何将数组放入 find 函数中。

the tester is built like测试仪是这样构建的

stringName("Test test test");

stringName.find("e",0);  //where 0 is the starting position, so it would return 1. 
int SuperString::find(char c, int start) {
// put array grabber thing here
size = *(&data + 1) - data;
    for(int i = start; i < size ; i++){
        if(data[i] == c){
            return i;
            }
    }
   return -1;
   
    } 

This is what I have to make the string into an array.这就是我必须将字符串变成数组的原因。

SuperString::SuperString(std::string str) {
    size = str.size();
    data = new char[size];
    for (int i = 0; i < size; i++) {
        data[i] = str.at(i);
    }

}

This is probably something easy I'm missing, but any help is appreciated.这可能是我缺少的一些简单的东西,但任何帮助表示赞赏。

You are passing a string literal, specifically a const char[2] , where a single char is expected.您正在传递一个字符串文字,特别是一个const char[2] ,其中需要一个char Use 'e' instead of "e" :使用'e'而不是"e"

stringName.find('e', 0);

More importantly, size = *(&data + 1) - data;更重要的是, size = *(&data + 1) - data; will only work when data is a (reference to a) fixed array (see How does *(&arr + 1) - arr give the length in elements of array arr? ).仅当data是(引用 a)固定数组时才有效(请参阅*(&arr + 1) - arr 如何给出数组 arr 元素的长度? )。 It will not work when data is a pointer to an array, as it is in your case since you are allocating the array with new char[] .data是指向数组的指针时,它将不起作用,就像您的情况一样,因为您使用new char[]分配数组。 You will have to keep track of the array's size separately, which you appear to be doing, except that you are not actually using the size you obtained in the SuperString constructor.您将不得不单独跟踪数组的size ,这似乎是在做,但实际上并没有使用在SuperString构造函数中获得的size Just get rid of the line in find() that is trying to re-calculate size , use the value you already have:只需去掉find()中试图重新计算size ,使用您已有的值:

int SuperString::find(char c, int start) {
    // size = *(&data + 1) - data; // <-- GET RID OF THIS
    for(int i = start; i < size; ++i){
        if (data[i] == c){
            return i;
        }
    }
    return -1;
} 

That being said, Your SuperString class can be greatly simplified if you just make its data member be a std::string instead of char* , eg:话虽如此,如果您只是将其data成员设为std::string而不是char* ,则可以大大简化您的SuperString类,例如:

#include <string>

class SuperString {
private:
    std::string data;
    ...
public:
    SuperString(const std::string &str);
    int find(char c, int start = 0);
    ...
};

SuperString::SuperString(const std::string &str) : data(str) {
}

int SuperString::find(char c, int start) {
    return (int) data.find(c, start);
} 

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM