C++ 如何跳过空格而不是换行符
我想跳过C++ 如何跳过空格而不是换行符,c++,iostream,C++,Iostream,我想跳过std::istream中的所有SPs和HTs,然后从中读取一个字符串,直到第一个SP或HT,然后跳过所有SPs和HTs,然后将所有内容读到新行\n 我试过了 stream >> str >> std::ws; std::getline(stream, rest); 但这并不能很好地工作,因为std::ws吃换行符、空格和水平表格。我想我能行 stream >> str; std::getline(stream, rest); trim_leadin
std::istream
中的所有SP
s和HT
s,然后从中读取一个字符串,直到第一个SP
或HT
,然后跳过所有SP
s和HT
s,然后将所有内容读到新行\n
我试过了
stream >> str >> std::ws;
std::getline(stream, rest);
但这并不能很好地工作,因为std::ws
吃换行符、空格和水平表格。我想我能行
stream >> str;
std::getline(stream, rest);
trim_leading_whitespace(rest);
甚至std::getline(流、温度);do_regex_match(“/*一些复杂的regex*/”,temp,str,rest)代码>
是否有可能仅使用与iostream相关的iostream
修复第一次尝试?就个人而言,它看起来最简洁,当然,任何关于如何简洁明了地编写它的建议都是受欢迎的。在阅读行声明一个额外的流:
std::string data; // <<-- trimmed data
std::string line;
std::getline(stream, line);
std::istringstream line_buffer(line);
line_buffer >> std::ws >> data;
std::字符串数据;//std::ws>>数据;
您可以创建自己的方面,将用户提供的空白字符注册到基础表中。创建用于启用/禁用此功能的操纵器是一项练习,我将留给您:
#include <iostream>
#include <sstream>
#include <string>
#include <vector>
class ctype : public std::ctype<char>
{
template<class... Args>
static mask* make_table(Args... args)
{
static std::vector<mask> table(classic_table(),
classic_table() + table_size);
typedef int pack[];
(void)pack{ ((table[args] |= space), void(), 0) ... };
table['\n'] &= ~space;
return &v[0];
}
public:
template<class... Args>
ctype(Args... args)
: std::ctype<char>(make_table(args...)) { }
};
int main()
{
std::istringstream iss("!@#abc");
iss.imbue(std::locale(iss.getloc(), new ctype('!', '@', '#')));
std::string str;
std::getline(iss >> std::ws, str);
std::cout << str; // "abc"
}
#包括
#包括
#包括
#包括
类ctype:public std::ctype
{
模板
静态掩码*生成表(Args…Args)
{
静态std::向量表(经典_表(),
经典表格()+表格大小);
typedef int-pack[];
(void)pack{((表[args]|=空格),void(),0).};
表['\n']&=~空间;
返回&v[0];
}
公众:
模板
ctype(Args…Args)
:std::ctype(生成表(args…){}
};
int main()
{
标准:istringstream iss(“!@#abc”);
iss.imbue(std::locale(iss.getloc(),新的ctype(“!”、“@”、“#”);
std::字符串str;
std::getline(iss>>std::ws,str);
std::cout这有帮助吗?@AdriC.S.ignore
只忽略一个特定字符。当然,我可以设计类似于while(stream.ignore(1,'\x20')| | | stream.ignore(1,'\t');
…实际上,让我检查一下。但是,如果不是count=1
你有std::数值限制::max()
,难道它不应该丢弃所有的空白吗?这个工作流不应该在第一个delim
字符之前跳过所有内容吗。