这是使用 iostreams 的暗内部的解决方案:
struct dataset_ctype : std::ctype<char>
{
mask table[table_size];
public:
dataset_ctype(size_t refs = 0) : std::ctype<char>(table, false, refs) {
std::fill(table, table+table_size, (mask)alpha);
table[','] = (mask)space;
}
};
int main()
{
std::string inp =
"e,x,y,w,t,a,f,c,b,k,e,c,s,s,w,w,p,w,o,p,n,s,g\n"
"e,f,y,y,t,l,f,c,b,w,e,r,s,y,w,w,p,w,o,p,n,y,p\n"
"e,b,s,w,t,a,f,c,b,w,e,c,s,s,w,w,p,w,o,p,n,s,g\n"
"e,b,s,w,t,a,f,c,b,w,e,c,s,s,w,w,p,w,o,p,k,s,m\n"
"e,x,y,n,t,l,f,c,b,w,e,r,s,y,w,w,p,w,o,p,k,y,g\n"
"e,b,s,w,t,a,f,c,b,k,e,c,s,s,w,w,p,w,o,p,k,s,g\n"
"e,x,f,g,f,n,f,c,n,g,e,e,s,s,w,w,p,w,o,p,n,y,u\n"
"e,b,s,y,t,l,f,c,b,k,e,c,s,s,w,w,p,w,o,p,n,s,g\n";
std::istringstream is(inp);
is.imbue(std::locale(is.getloc(), new dataset_ctype));
const int column = 2;
while (is) {
std::istream_iterator<std::string> ii(is);
std::advance(ii, column);
std::cout << *ii << '\n';
is.ignore(std::numeric_limits<std::streamsize>::max(), '\n');
}
}