Figure 5: Rewriting using a straight call to some appropriate STL algorithm
1: //
2: // Extra credit? remove the inner loop
3: //
4:
5: template<typename Container>
6: Container make_s(const std::string &intext,
7: bool stripLeadingSpaces = true)
8: {
9: using std::string;
10: Container c;
11: string::const_iterator nextComma;
12:
13: string::const_iterator curPos = intext.begin();
14:
15: while (true)
16: {
17: nextComma = std::find(curPos, intext.end(), ',');
18:
19: if (stripLeadingSpaces) // call to find_if replaces loop
20: curPos = std::find_if(curPos, nextComma,
21: std::not1(std::ptr_fun(isspace)));
22:
23: c.push_back(string(curPos, nextComma));
24:
25: if (nextComma == intext.end())
26: break;
27:
28: curPos = ++nextComma;
29: }
30:
31: return c;
32: }