字符串,split. c++我需要帮助来计算如何做

v64noz0r  于 2022-12-20  发布在  其他
关注(0)|答案(2)|浏览(117)

我在lua中使用gmatch来拆分条目,并将插槽放入自己的表中,如数据[1]数据[2]数据来自DB,但现在我转到C++,我找不到正确的方法来做到这一点。我已经浪费了5天的时间来尝试计算如何做到这一点。
这是我在lua上用来分割所需数据的

`
local function stringsplit(inStr,sep)
    local rettab = {}
    for str in string.gmatch(inStr, "[^;]+") do
        table.insert(rettab, str)
    end
    return rettab
end

here is an example of the the data i need to split correctly

1:200000;2:200001;3:200002

1 , 2 , 3 are the ITEM SLOT : the separator and 200000-200002 the items

so i want to store them separated, "SLOTS" in DATA[1] and ITEMS in DATA[2]  (only has example)

the desired result should be something like

DATA[1] // SLOT
1
2
3

DATA[2] //ITEMS
200000
200001
200002

我很抱歉我的英语不好,我试过了,我会很高兴,如果有人知道解决方案或方法来做它正确谢谢你:/

carvr3hs

carvr3hs1#

下面的代码使用strtok()函数和我自己的函数mySplit(),考虑创建自己的实现或调整mySplit()以将单词保存在数组中。

#include <iostream>  
#include <cstring>  
using namespace std;  

#define MAX_STR 128

char* mySplit(char* strg, char delimiter=' ')
{
    if (NULL == strg) return "\n"; // no string to split passed
    
    //cout << " len: " << strlen(strg);
    int len = strlen(strg);
    for(int i=0; i<len; i++) {
       if (strg[i] == delimiter)
           strg[i] = '\n';
    }
    return strg;
}

int main()  
{  
    char str[MAX_STR] = "This is a string to split";     
    //cout << " Text to Split: " <<endl;  
    //cin.getline(str, 100); // get string to split   
      
    char *ptr; // pointer to iterate over the src string  
    ptr = strtok(str, " "); // use strtok() to split.  
    cout << " \n Split withstring strtok() : \n" << endl;  
    int cnt = 0;    // make sure loops terminate.
    // use while loop to check ptr is not null  
    while (ptr != NULL && cnt < MAX_STR)  
    {  
        cout << ptr  << endl; // print the string token  
        ptr = strtok (NULL, " "); 
        ++cnt;
    }  
    
    strcpy(str, "Second test string to split using mySplit()");
    ptr = mySplit(str, ' ');
    cout << endl << ptr << endl;
    return 0;  
}

输出:使用字符串strtok()拆分:

This
is
a
string
to
split

Second
test
string
to
split
using
mySplit()
ngynwnxp

ngynwnxp2#

我通常会这样做,返回string_views的集合,这非常节省内存(但它要求原始字符串保持在作用域中)。

#include <iostream>
#include <string>
#include <string_view>
#include <vector>

std::vector<std::string_view> split_string(const std::string_view input, char delimiter)
{
    std::vector<std::string_view> splits;

    const char* p{ input.data() };
    const char* word_begin{ p };
    std:size_t len{ 0u };

    for( auto p = input.data(); *p != 0; ++p)
    {
        if (*p == delimiter)
        {
            if (len > 0ul) splits.emplace_back(word_begin, len);
            word_begin = p+1;
            len = 0ul;
        }
        else
        {
            ++len;
        }
    }

    if (len > 0ul) splits.emplace_back(word_begin, len);
    
    return splits;
}

int main()
{
    auto splits = split_string("This is a string to split", ' ');
    for (const auto split : splits)
    {
        std::cout << split << "\n";
    }

    return 0;
}

相关问题