简体   繁体   English

如何在C ++中分割字符串向量数组

[英]How to split the string vector array in C++

How to split the string vector in C++? 如何在C ++中分割字符串向量?

The input string values are read from the file, and it's format is like below. 输入的字符串值是从文件中读取的,其格式如下。

P00 ARRIVAL:3   CPU:1   I/O:3   CPU:7   I/O:1   CPU:3   
P01 ARRIVAL:2   CPU:9   
P02 ARRIVAL:0   CPU:6   
P03 ARRIVAL:4   CPU:1   
P04 ARRIVAL:0   CPU:5   

However, I need just the value like, 但是,我只需要这样的值,

p00 3   1   3   7   1   3
p01 2   9
p02 0   6
...

And this is my part of code. 这是我代码的一部分。 The file is read by a line by line, and each line is saved on string vector array. 逐行读取文件,并将每一行保存在字符串向量数组中。

vector<string> procList; // file


void readProc(char *filename) {

    std::ifstream file(filename);
    std::string str;
    while (std::getline(file, str))
    {
        procList.push_back(str);
    }

    file.close();

    for (int i = 0, size = procList.size(); i < size; ++i) {
        _tprintf(TEXT("[%s] \n"), procList[i]);
    }
}

Thanks. 谢谢。

Using regexps for parsing: 使用正则表达式进行解析:

live 生活

#include <regex>

std::vector<std::string> example = {
        "P00 ARRIVAL:3   CPU:1   I/O:3   CPU:7   I/O:1   CPU:3",
        "P01 ARRIVAL:2   CPU:9",
        "P02 ARRIVAL:0   CPU:6",
        "P03 ARRIVAL:4   CPU:1",
        "P04 ARRIVAL:0   CPU:5"
};
std::regex re{"[\\w\\d]+:(\\d+)"};
for (auto s : example) {
    std::cout << "\n";
    std::regex_token_iterator<std::string::iterator> it{s.begin(), s.end(), re, 1};
    decltype(it) end{};
    while (it != end) std::cout << *it++ << ":";
}

will output: 将输出:

3:1:3:7:1:3:
2:9:
0:6:
4:1:
0:5:

With regex_token_iterator you could also specify to iterate over name and value in your data. 使用regex_token_iterator,您还可以指定对数据中的名称和值进行迭代。 You specify in regexp two groups (using parentheses), and then specify in constructor to regex_token_iterator which groups (sub matches) to include during iteration using: {1, 2}. 您可以在正则表达式中指定两个组(使用括号),然后在构造函数中指定regex_token_iterator使用{1,2}在迭代过程中包括哪些组(子匹配项)。

live 生活

std::regex re{R"(([\w\d/]+):(\d+))"};
for (auto s : example) {
    std::cout << "\n";
    using reg_itr = std::regex_token_iterator<std::string::iterator>;
    for (reg_itr it{s.begin(), s.end(), re, {1,2}}, end{}; it != end;) {
        std::cout << *it++ << ":"; std::cout << std::stoi(*it++) << " ";
    }
}

will output: 将输出:

ARRIVAL:3 CPU:1 I/O:3 CPU:7 I/O:1 CPU:3 
ARRIVAL:2 CPU:9 
ARRIVAL:0 CPU:6 
ARRIVAL:4 CPU:1 
ARRIVAL:0 CPU:5 

Assuming the format is id {key:value} . 假设格式为id {key:value} The following code utilizes stringstream and getline . 以下代码利用stringstreamgetline Might have problems, I haven't tested. 可能有问题,我尚未测试。

vector<string> procList; // file


void readProc(char *filename) {

    std::ifstream file(filename);
    std::string str;
    while (std::getline(file, str))
    {
        std::stringstream in(str);
        std::stringstream out;
        std::string temp;

        in>>temp;
        out<<temp;

        while(getline(in, temp, ':')) {
            in>>temp;
            out<<"\t"<<temp;
        }

        procList.push_back(out.str());
    }

    file.close();

    for (int i = 0, size = procList.size(); i < size; ++i) {
        _tprintf(TEXT("[%s] \n"), procList[i]);
    }
}

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

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