Figure 2: Second iteration
1: //
2: // Second iteration: starting to think in terms
3: // of iterators.
4: //
5:
6: template<typename Container>
7: Container make_s(const char *intext,
8: bool stripLeadingSpaces = true)
9: {
10: using std::string;
11: int len;
12: char *incopy = new char[(len = strlen(intext)) + 1];
13: strcpy(incopy, intext);
14: Container c;
15: char *end = incopy + len;
16: char *nextComma;
17:
18: char *curPos = incopy;
19: while (true)
20: {
21: // no need now to stuff a '\0' at *nextComma:
22: nextComma = std::find(curPos, end, ',');
23:
24: if (stripLeadingSpaces)
25: while (isspace(*curPos))
26: ++curPos;
27:
28: c.push_back(string(curPos, nextComma));
29:
30: if (nextComma == end)
31: break;
32:
33: curPos = ++nextComma;
34: }
35: delete[] incopy;
36: return c;
37: }