gpt4 book ai didi

c++ - 多个分隔符

转载 作者:行者123 更新时间:2023-11-28 03:28:06 24 4
gpt4 key购买 nike

例如,我有一个来自具有多个定界符的文件的输入

years,(7),(9)
years,(8),(3)

我可以用什么方法将它们分开

years
7
9
years
8
3

我尝试使用 strok,但我没有显示以下内容。

getline (myfile,line, ',' );
line = strtok (pch," (),");

我从 http://www.cplusplus.com/reference/clibrary/cstring/strtok/ 得到这个例子

最佳答案

这看起来像是 std::locale 的工作和他可信赖的伙伴 imbue :

#include <locale>
#include <iostream>


struct punct_ctype : std::ctype<char> {
punct_ctype() : std::ctype<char>(get_table()) {}
static mask const* get_table()
{
static mask rc[table_size];
rc[' '] = std::ctype_base::space;
rc['\n'] = std::ctype_base::space;
rc['('] = std::ctype_base::space;
rc[')'] = std::ctype_base::space;
rc[','] = std::ctype_base::space;
return &rc[0];
}
};

int main() {
using std::string;
using std::cin;
using std::locale;

cin.imbue(locale(cin.getloc(), new punct_ctype));

string word;
while(cin >> word) {
std::cout << word << "\n";
}
}

关于c++ - 多个分隔符,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13380843/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com