标题:STL里vector问题
只看楼主
parazhouyu
Rank: 1
等 级:新手上路
帖 子:1
专家分:0
注 册:2007-6-23
 问题点数:0 回复次数:1 
STL里vector问题

//----------------------MyString.h---------------
#ifndef MYSTRING_H_
#define MYSTRING_H_
#include<vector>
class myString
{
public:
myString(const char*);
~myString();
typedef std::vector<myString> myStringCollection;
const myStringCollection Split(const char separators[] );
private:
char* msg;
unsigned length;

};
#endif
//--------------------MyString.cpp---------------------------------

#include<string.h>
#include "MyString.h"
typedef std::vector<myString> StringCollection;
myString::myString(const char *value)
{
length = (unsigned)strlen(value);
msg = new char[length+1];
if(value!=NULL)
strcpy(msg,value);

}


myString::~myString()
{

length=0;
delete [] msg;
msg=NULL;
CharPointerCollection.clear();


}
const StringCollection myString::Split(const char separators[])
{
StringCollection Strings;
unsigned size = sizeof(separators);
if(size > 0)
{
//char* temp = msg;
char* temp = new char[length+1];
strcpy(temp,msg);
//char* current = msg;
char* current = temp;

//unsigned current = length;

for(unsigned cur=0;cur<length;cur++)
{
for( unsigned count = 0;count < size; count++)
{
if(*(temp + cur) != separators[count])
continue;
else
{
*(temp+cur) = '\0';
myString ms(current);
Strings.push_back(ms);//此处执行之后,ms.msg已经变了,很怪的字符。
current += cur;

}

}
}
delete [] temp;
temp = NULL;
current = NULL;
delete current;

return Strings;
}
else
{
Strings.push_back(*this);
return Strings;
}

}

const char* const myString::GetData() const
{
return msg;
}

//------------------------------------main.cpp-----------------------------------
#include "MyString.h"
#include<iostream>
using namespace std;
int main()
{
char p[] = {',','|','*','#'};
myString a("af,jfkd|fdjk*dsfg#fasdf"); //目的:把此字符串用分割符号分开。
cout<<a.Split(p)[0].GetData();

return 0;
}



搜索更多相关主题的帖子: vector STL 
2007-06-23 11:22
aipb2007
Rank: 8Rank: 8
来 自:CQU
等 级:贵宾
威 望:40
帖 子:2879
专家分:7
注 册:2007-3-18
得分:0 
What's the problem?

Fight  to win  or  die...
2007-06-23 12:24



参与讨论请移步原网站贴子:https://bbs.bccn.net/thread-149671-1-1.html




关于我们 | 广告合作 | 编程中国 | 清除Cookies | TOP | 手机版

编程中国 版权所有,并保留所有权利。
Powered by Discuz, Processed in 0.032149 second(s), 8 queries.
Copyright©2004-2024, BCCN.NET, All Rights Reserved