c++;std::字符串数组作为函数参数 我有点喜欢C++。我想为C++中的STD::string函数做分割,就像在string类中的java分割函数(我不想使用Boost库)。所以我做了一个自定义的分割函数,它是 using namespace std; void ofApp::split(string ori, string tokens[], string deli){ // calculate the number of tokens int length = 0; int curPos = 0; do{ curPos = ori.find(deli, curPos) + 1; length++; }while(ori.find(deli, curPos) != string::npos); length++; // to save tokens, initialize tokens array tokens = new string[length]; // this is the line I'm suspicious about.. int startPos = 0; int strLength = 0; int curIndex = 0; do{ strLength = ori.find(deli, startPos) - startPos; tokens[curIndex++] = ori.substr(startPos, strLength); startPos = ori.find(deli, startPos) + 1; }while(ori.find(deli, startPos) != string::npos); tokens[curIndex] = ori.substr(startPos, ori.length() - startPos); }

c++;std::字符串数组作为函数参数 我有点喜欢C++。我想为C++中的STD::string函数做分割,就像在string类中的java分割函数(我不想使用Boost库)。所以我做了一个自定义的分割函数,它是 using namespace std; void ofApp::split(string ori, string tokens[], string deli){ // calculate the number of tokens int length = 0; int curPos = 0; do{ curPos = ori.find(deli, curPos) + 1; length++; }while(ori.find(deli, curPos) != string::npos); length++; // to save tokens, initialize tokens array tokens = new string[length]; // this is the line I'm suspicious about.. int startPos = 0; int strLength = 0; int curIndex = 0; do{ strLength = ori.find(deli, startPos) - startPos; tokens[curIndex++] = ori.substr(startPos, strLength); startPos = ori.find(deli, startPos) + 1; }while(ori.find(deli, startPos) != string::npos); tokens[curIndex] = ori.substr(startPos, ori.length() - startPos); },c++,string,reference,tokenize,C++,String,Reference,Tokenize,首先,我认为将参数作为字符串tokens[]传递是通过引用调用的方式,因此当函数完成时,tokens[]数组将充满由deli string分隔的令牌。但是当我调用这个函数时 string str = "abc,def,10.2,dadd,adsf"; string* tokens; split(str, tokens, ","); 在此之后,令牌数组将完全为空。我猜,这是因为这条线 tokens = new string[length]; 我认为令牌数组作为局部变量的内存空间已经分配,当分割

首先,我认为将参数作为字符串tokens[]传递是通过引用调用的方式,因此当函数完成时,tokens[]数组将充满由deli string分隔的令牌。但是当我调用这个函数时

string str = "abc,def,10.2,dadd,adsf";
string* tokens;
split(str, tokens, ",");
在此之后,令牌数组将完全为空。我猜,这是因为这条线

tokens = new string[length];
我认为令牌数组作为局部变量的内存空间已经分配,当分割函数完成时,这个内存空间将随着块的完成而释放

当我尝试调试时,split函数本身工作得很好,因为至少在split函数块中,tokens数组中充满了token。我想我的猜测是对的,但是我怎样才能解决这个问题呢?有解决办法吗?我认为这不仅仅是std::string数组的问题,这是“通过引用调用”的作业

要求

  • 将std::string[]类型传递给函数参数(返回标记[]也可以。但我认为这会有相同的问题)
  • 函数完成后,数组中必须充满令牌
  • 令牌数组长度必须在split函数中计算(若用户必须计算令牌长度,那个么这是一个愚蠢的函数)。因此,在split函数调用之前,无法分配令牌数组的内存

  • 提前感谢您的精彩回答

    正如@chris所建议的,下面的代码应该可以工作

    示例代码
    除非参数是引用(带&),否则不会通过引用传递任何内容。我明白了。。。。那我该怎么办?事实上,我试着使用它,但它给出了很多错误,我放弃了。和基元类型数组不同,字符串数组的单元格大小不像4字节那个样固定。我想这是个问题。我觉得我真的是C++的NoOB。我使用了很多种语言,比如C++,java,AS3,Lua,python等,但是我真的不能用C++。它太复杂了,我可能只返回一个
    std::vector
    ,而不是强制一个out参数而不返回值。这可能吗?如果我返回std::vector,我必须为split函数块中的std::vector变量分配内存,当我返回它时,这个变量不是免费的吗?因为这个变量内存空间是在本地块中分配的..???创建本地变量
    std::vector令牌并在其上使用
    push_back
    resize
    函数,然后按值从函数返回。它会起作用的。无
    new
    表示没有问题!在C++中,<代码>新< /COD>很糟糕。非常感谢。C++让我疯狂…但编码世界,当我走得越来越深,我认为最终C++是必不可少的。
    #include <iostream>
    #include <string>
    #include <vector>
    
    std::vector<std::string> split(const std::string& delimiter, const std::string& str)
    {
        std::vector<std::string> result;
    
        std::size_t prevPos = 0;
        while (prevPos != std::string::npos)
        {
            std::size_t currPos = str.find(delimiter, prevPos);
            result.push_back(str.substr(prevPos, currPos - prevPos));
    
            prevPos = currPos;
    
            if (prevPos != std::string::npos)
            {
                // Skip the delimiter
                prevPos += delimiter.size();
            }
        }
    
        return result;
    }
    
    int main()
    {
        std::string str("this,is,a,test");
        std::vector<std::string> splitResult = split(",", str);
        for (const auto &s : splitResult)
        {
            std::cout << "'" << s << "'\n";
        }
    
        std::cout << "\n";
    
        str = "this - is - a - test";
        splitResult = split(" - ", str);
        for (const auto &s : splitResult)
        {
            std::cout << "'" << s << "'\n";
        }
    
        return 0;
    }
    
    'this'
    'is'
    'a'
    'test'
    
    'this'
    'is'
    'a'
    'test'