Figure 3: Third iteration
1: //
2: // Third iteration: goodbye, dynamic allocation;
3: // hello, strings.
4: //
5:
6: template<typename Container>
7: Container make_s(const char *intext,
8: bool stripLeadingSpaces = true)
9: {
10: using std::string;
11: string incopy(intext); // now a std::string; no len needed
12: Container c;
13: string::iterator nextComma; // string iterators, not char *s
14:
15: string::iterator curPos =
16: incopy.begin();
17: while (true)
18: {
19: nextComma = std::find(curPos, incopy.end(), ',');
20:
21: if (stripLeadingSpaces)
22: while (curPos != nextcomma && isspace(*curPos))
23: ++curPos;
24:
25: c.push_back(string(curPos, nextComma));
26:
27: if (nextComma == incopy.end())
28: break;
29:
30: curPos = ++nextComma;
31: }
32: // nothing to delete
33: return c;
34: }