| 
              #include <iostream>#include <string>
 #include <vector>
 
 using namespace std;
 
 vector<string> explode(string delimiter, string str)
 {
 vector<string> arr;
 if (!str.empty()) {
 string::size_type pos, oldpos = 0;
 while (pos = str.find(delimiter, oldpos))
 {
 if (pos == string::npos)
 {
 arr.push_back(str.substr(oldpos));
 break;
 }
 else
 {
 arr.push_back(str.substr(oldpos, pos - oldpos));
 oldpos = pos + delimiter.length();
 }
 }
 }
 return arr;
 }
 
 string join(string delimiter, vector<string> &str)
 {
 string result;
 vector<string>::iterator iter;
 for (iter = str.begin(); iter < str.end(); iter++)
 {
 result += *iter;
 if (iter + 1 < str.end())
 result += delimiter;
 }
 return result;
 }
 
 int main(void)
 {
 vector<string> list = explode(", ", "this, that, these, those, find a pen, the pen");
 cout << "Size: " << list.size() << endl;
 
 vector<string>::iterator iter;
 for (iter = list.begin(); iter < list.end(); iter++)
 cout << *iter << endl;
 
 cout << join(";", list) << endl;
 
 return 0;
 }
 |