The free function getline
also offers to have a custom delimiter like user4581301 said. However, it will only extract string
s and you also have int
.
A similar solution can be found in this answer and I have modified the code to fit your needs: changing the delimiter for cin (c++)
You can use imbue
to have some custom delimiter. A simple example is below:
#include <locale>
#include <iostream>
template<char Delim>
struct alternativeDelimiter : std::ctype<char> {
alternativeDelimiter() : std::ctype<char>(get_table()) {}
static mask const* get_table()
{
static mask rc[table_size];
rc[Delim] = std::ctype_base::space;
return &rc[0];
}
};
int main() {
using std::string;
using std::cin;
using std::locale;
cin.imbue(locale(cin.getloc(), new alternativeDelimiter<'^'>));
string word;
while(cin >> word) {
std::cout << word << "\n";
}
}
imbue
does take ownership of the ctype
, so no worries about calling delete yourself.
If you input some^text
, the output will be
some
text
You can also use it with your example, of course.
If you extend the table by writing lines similar to line 11 (rc[Delim] = std::ctype_base::space
) only changing Delim
, you can have multiple characters that will be interpreted as space.
I am not sure in how far this solves your original problem of writing a math parser, though. The general terminology involves the concepts "parser" and "lexer" and you might research these concepts to build a reliable math solver. Hope it helps, too.