Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

stringstream set delimeter

27 views
Skip to first unread message

Christopher Pisz

unread,
Jul 30, 2015, 7:33:26 PM7/30/15
to
Is there a way to set the delimeter for an istringstream when using the
'>> operator'?

I prefer to not use getline if possible.


--
I have chosen to troll filter/ignore all subthreads containing the
words: "Rick C. Hodgins", "Flibble", and "Islam"
So, I won't be able to see or respond to any such messages
---

R. Schubert

unread,
Jul 31, 2015, 5:03:54 AM7/31/15
to
You could do it similarly to the example here
http://en.cppreference.com/w/cpp/locale/ctype

Öö Tiib

unread,
Jul 31, 2015, 7:59:11 AM7/31/15
to
On Friday, 31 July 2015 02:33:26 UTC+3, Christopher Pisz wrote:
> Is there a way to set the delimeter for an istringstream when using the
> '>> operator'?
>
> I prefer to not use getline if possible.

Copy-paste of accepted answer from

http://stackoverflow.com/questions/10376199/how-can-i-use-non-default-delimiters-when-reading-a-text-file-with-stdfstream

It looks fine.

#include <locale>
#include <iostream>
#include <algorithm>
#include <iterator>
#include <vector>
#include <sstream>

class my_ctype
: public std::ctype<char>
{
mask my_table[table_size];
public:
my_ctype(size_t refs = 0)
: std::ctype<char>(&my_table[0], false, refs)
{
std::copy_n(classic_table(), table_size, my_table);
my_table['-'] = (mask)space;
my_table['\''] = (mask)space;
}
};

// test
int main()
{
std::istringstream input("This is some input from McDonald's and Burger-King.");
std::locale x(std::locale::classic(), new my_ctype);
input.imbue(x);

std::copy(std::istream_iterator<std::string>(input),
std::istream_iterator<std::string>(),
std::ostream_iterator<std::string>(std::cout, "\n"));

return 0;
}

Output:

This
is
some
input
from
McDonald
s
and
Burger
King.
0 new messages