【发布时间】:2011-07-02 19:16:34
【问题描述】:
我在将 char* 插入向量时遇到问题
当我执行以下操作时:
string str = "Hello b World d"
char *cstr, *p;
vector<char*> redn;
cstr = new char [ (str.size)+1 ];
strcpy(cstr, str.c_str());
//here I tokenize "Hello b World d"
p = strtok(cstr," ");
while(p!=NULL){
redn.push_back(p);
cout << "just pushed back: " << redn.back() << endl;
p = strtok(NULL," ");
}
delete[] cstr;
//now check
for(it= redn.begin(); it < redn.end(); it++)
cout << *it << endl;
我得到了一个输出:
just pushed back: Hello
just pushed back: b
just pushed back: World
just pushed back: d
p0s
World
d
在我看来,*它指向了错误的东西.. 有人能告诉我发生了什么事以及如何解决这个问题吗?
【问题讨论】: