我想让istream
仅将制表符视为空格。所以,鉴于" {json:5} \ tblah",我想将json加载到obj1
和" blah"使用以下代码进入obj2
:
is << obj1 << obj2
有没有办法在不将对象加载到字符串的情况下执行此操作?
答案 0 :(得分:1)
在本地设置中,选项卡是唯一具有空间属性的字符。
困难部分:创建一个继承自ctype
的构面。然后确保将所有字符设置为不是空格(tab除外)。
#include <locale>
#include <fstream>
#include <iostream>
#include <string>
#include <sstream>
// This is my facet:
// It is designed to treat only <tab> as whitespace
class TabSepFacet: public std::ctype<char>
{
public:
typedef std::ctype<char> base;
typedef base::char_type char_type;
TabSepFacet(std::locale const& l) : base(table)
{
// Get the ctype facet of the current locale
std::ctype<char> const& defaultCType = std::use_facet<std::ctype<char> >(l);
// Copy the default flags for each character from the current facet
static char data[256];
for(int loop = 0; loop < 256; ++loop) {data[loop] = loop;}
defaultCType.is(data, data+256, table);
// Remove the other spaces
for(int loop = 0; loop < 256; ++loop)
{
// If the space flag is set then XOR it out.
if (table[loop] & base::space)
{ table[loop] ^= base::space;
}
}
// Only a tab is a space
table['\t'] |= base::space;
}
private:
base::mask table[256];
};
简单的部分:创建一个使用facet的locale对象,并用它来填充流:
int main()
{
// Create a stream (Create the locale) then imbue the stream.
std::stringstream data("This is a\tTab");
const std::locale tabSepLocale(data.getloc(), new TabSepFacet(data.getloc()));
data.imbue(tabSepLocale);
// Note: If it is a file stream then imbue the stream BEFORE opening a file,
// otherwise the imbue is silently ignored on some systems.
// Now you can use the stream like normal; your locale defines what
// is whitespace, so the operator `>>` will split on tab.
std::string word;
while(data >> word)
{
std::cout << "Word(" << word << ")\n";
}
}
结果:
> g++ tab.cpp
> ./a.out
Word(This is a)
Word(Tab)
注意:甚至换行也不是上面的空白字符。因此,运算符>>
将在行尾读取并忽略它。
答案 1 :(得分:0)
std::getline
怎么样?
getline(getline(std::cin, obj1, '\t'), obj2);