以下是引用lz1091914999在2012-5-14 10:30:57的发言:
#include
#include
#include
#include
using namespace std;
class SplitResult {
public:
SplitResult(char** a = 0, int c = 0) : array(a), count(c) {}
~SplitResult();
char** getArray() { return array; }
int getCount() { return count; }
private:
char** array;
int count;
friend SplitResult* split(const char*, SplitResult*);
};
SplitResult::~SplitResult()
{
for (int i = 0; i < count; ++i)
delete array;
delete array;
}
SplitResult* split(const char*, SplitResult*);
int main()
{
const char* str = "zhongyuan da jia ting";
SplitResult sr;
int i;
split(str, &sr);
for (i = 0; i < sr.getCount(); ++i)
cout << sr.getArray() << " ";
}
SplitResult* split(const char* str, SplitResult* sr)
{
const char* p = str;
const char* start;
char** array;
char* tmp;
int length;
int i;
vector v;
while (*p) {
if (isalpha(*p)) {
start = p;
length = 0;
while (isalpha(*p))
++p, ++length;
tmp = new char[length + 1];
tmp[length] = '\0';
v.push_back(strncpy(tmp, start, length));
}
++p;
}
array = new char*[v.size()];
for (i = 0; i < v.size(); ++i)
array = v;
sr->array = array;
sr->count = v.size();
return sr;
}
c c++ 混合编程啊