Using isPunct to separate tokens from an expression

I am looking to separate a polynomial expression. I've looked at http://www.cplusplus.com/reference/cctype/ and it seems that isPunct is a viable option.

Now i'd like to use isPunct to separate an expression when an operator(in this case punctuation is detected). I want to use istringstream, but I'm still baffled on how to use it.

In the end, I'd like a vector the contains the substrings such as
[0] 3x^3
[1] +2x-1
[2] -x

When the user inputs: 3x^3 +2x-1 -x

1
2
3
4
5
6
7
8
9
10
11
void separatePolynomial(string expr) {
	string result;
	vector<string> terms;

	// remove spaces
	remove_copy_if(expr.begin(), expr.end(), back_inserter(result), // store output
	ptr_fun<int, int>(&isspace));

	// separate tokens using operators
	// code here
}
Last edited on
bump.
isspace has the wrong signature (char is required). You may change it like that:
1
2
3
4
5
6
7
8
9
10
11
12
bool IsSeparator(char ch) { return isspace(ch); }

void separatePolynomial(string expr) {
	string result;
	vector<string> terms;

	// remove spaces
	remove_copy_if(expr.begin(), expr.end(), back_inserter(result), IsSeparator);

	// separate tokens using operators
	// code here
}


Apart from that, what is your question?
Topic archived. No new replies allowed.