网页资讯视频图片知道文库贴吧地图采购
进入贴吧全吧搜索

 
 
 
日一二三四五六
       
       
       
       
       
       

签到排名:今日本吧第个签到,

本吧因你更精彩,明天继续来努力!

本吧签到人数:0

一键签到
成为超级会员,使用一键签到
一键签到
本月漏签0次!
0
成为超级会员,赠送8张补签卡
如何使用?
点击日历上漏签日期,即可进行补签。
连续签到:天  累计签到:天
0
超级会员单次开通12个月以上,赠送连续签到卡3张
使用连续签到卡
06月24日漏签0天
图形编程forc吧 关注:14贴子:296
  • 看贴

  • 图片

  • 吧主推荐

  • 游戏

  • 8回复贴,共1页
<<返回图形编程forc吧
>0< 加载中...

发下代码试试,500行左右的string

  • 只看楼主
  • 收藏

  • 回复
  • hellovfp
  • 排序查找
    6
该楼层疑似违规已被系统折叠 隐藏此楼查看此楼
无聊,写了一下unicode的string实现。
//删除,插入,查找,替换,取子串, Trim, 迭代器
// 还差 替换和 char转换unicode, char或许是utf-8, int 和保留功能有点重合
class String
{
typedef struct _format_args
{
int prec;
int width;
wchar_t fill;
}_fargs;
public:
String()
:_size(0), _capacity(_size+1), _data(new wchar_t[_capacity])
{
*_data = L'\0';
}
// 容器需要
String(const String& rhs)
:_size(rhs._size), _capacity(_size+1), _data(new wchar_t[_capacity])
{
_copy(_data, rhs._data, _size);
}
String(const wchar_t *str)
{
for(_size = 0; str[_size]; ++_size);
_capacity = _size + 1;
_data = new wchar_t[_capacity];
_copy(_data, str, _size);
}
String(const wchar_t ch)
:_size(1), _capacity(_size+1), _data(new wchar_t[_capacity])
{
*_data = ch;
*(_data+1) = L'\0';
}
//这里有必要么?。
String(int val)
:_size(0), _capacity(12), _data(new wchar_t[_capacity])
{
bool sign = false;
if(val < 0){
sign = true;
val = -val;
}
wchar_t *p = _uitoa(_data, val, 10, sign);
_size = _data - p;
}
String(const char *str)
{
unsigned int code_page = 936;
_size = MultiByteToWideChar(code_page, 0, str, -1, NULL, 0);
_capacity = _size;
_data = new wchar_t[_size];
MultiByteToWideChar(code_page, 0, str, -1, _data, _size--);
}
~String()
{
delete[] _data;
}
//////////////////////////////////////////////////////
String& operator=(String rhs) // pass by value
{
swap(rhs);
_size = rhs._size;
_capacity = _size + 1;
return *this;
}
String operator+(const String &rhs)
{
String tmp(_data); //没有进行测同,因为无意义
tmp += rhs;
return tmp;
}
String& operator+=(const String& rhs)
{
_data = _renew(_data, _size, _size + rhs._size);
_copy(_data+_size, rhs._data, rhs._size);
_size += rhs._size;
_capacity = _size + 1;
return *this;
}
wchar_t& operator[](size_t index) const
{
return _data[index];
}
bool operator==(const String& rhs)
{
return _cmp(_data, rhs._data) == 0;
}
bool operator >(const String& rhs)
{
return _cmp(_data, rhs._data) > 0;
}
bool operator < (const String& rhs)
{
return _cmp(_data, rhs._data) < 0;
}


  • hellovfp
  • 排序查找
    6
该楼层疑似违规已被系统折叠 隐藏此楼查看此楼
///////////////////////////////////////////////
//简单的查找
int find(const String& rhs) const
{
if(_size >= rhs._size)
{
for(size_t i = 0; i < (_size +rhs._size + 1); ++i){
size_t j;
for(j = 0; (j < rhs._size) &&
(_data[i+j] == rhs._data[j]); ++j);
if(j == rhs._size) return i;
}
}
return -1;
}
// 超出范围不会删除字符。
String& erase(size_t start_idx = 0, size_t num = -1)
{
wchar_t *p1 = _data + start_idx, *p2 = p1 + num;
if(num == -1 && start_idx < _size){
*p1 = L'\0';
_size = start_idx;
}
else if((start_idx + num ) <= _size){
while(*p1){*p1++ = *p2++;}
_size -= num;
}
return *this;
}
String& insert(size_t idx, const String& s)
{
if(idx <= _size)
{
wchar_t *p = new wchar_t[_size + s._size + 1];
_copy(p, _data, idx);
_copy(p+idx, s._data, s._size);
_copy(p+idx+s._size, _data + idx, _size - idx);
delete[] _data;
_data = p;
_size += s._size;
_capacity = _size + 1;
}
return *this;
}
// 超出范围不会返回子串
String substr(size_t idx, size_t num = -1)
{
if(idx <= _size)
{
if (num == -1) num = _size - idx;
wchar_t * p = new wchar_t[num+1];
_copy(p, _data+idx, num);
String s(p);
delete[] p;
return s;
}
return *this;
}
String& push_back(wchar_t wch)
{
if(_size == _capacity - 1){
_capacity *= 2;
_data = _renew(_data, _size, _capacity);
}
if(wch)
_data[_size++] = wch;
else
_data[_size] = wch;
return *this;
}


2025-06-24 12:40:10
广告
  • hellovfp
  • 排序查找
    6
该楼层疑似违规已被系统折叠 隐藏此楼查看此楼
// 简单的格式化字符串, 仅支持少量的格式。差一个8进制和2进制
void format(const wchar_t* fmt, ...)
{
wchar_t s[64] = {0}, *p, *end;
bool sign;
int b;
_size = 0;
va_list ap;
va_start(ap, fmt);
while(*fmt)
{
if(*fmt != L'%')
{
this->push_back(*fmt++);
continue;
}
//处理宽度和精度
_fargs fa = {0};
int idx;
if(*++fmt == L'0') fa.fill = *fmt++;
if(is_digit(*fmt)){
idx = 0;
fa.width = _atoi(fmt, idx);
fmt += idx;
}
if(*fmt == L'.'){
idx = 0;
fa.prec = _atoi(++fmt, idx);
fmt += idx;
}
switch(*fmt)
{
case L'c':
this->push_back(va_arg(ap, wchar_t));
break;
case L'd':
{
int a = va_arg(ap, int);
sign = false;
if(a < 0){
sign = true;
a = -a;
}
end = _uitoa(s, a, 10, sign);
b = fa.width - (end-s);
while(0 < b--) this->push_back(fa.fill);
for(int i = 0; s[i]; ++i) this->push_back(s[i]);
}
break;
case L'u':
{
unsigned int a = va_arg(ap, unsigned int);
end = _uitoa(s, a, 10, false);
b = fa.width - (end-s);
while(0 < b--) this->push_back(fa.fill);
for(int i = 0; s[i]; ++i) this->push_back(s[i]);
}
break;
case L'f':
{
double a = va_arg(ap, double);
ftoa_4(s, a, fa.prec);
for(int i = 0; s[i]; ++i) this->push_back(s[i]);
}
break;
case L's':
p = va_arg(ap, wchar_t*);
while(*p) this->push_back(*p++);
break;
case L'x':
{
unsigned int a = va_arg(ap, unsigned int);
_fast_itoa(s, (unsigned int)a, 16, false);
for(int i = 0; s[i]; ++i) this->push_back(s[i]);
}
break;
case L'p':
{
void *ip = va_arg(ap, void*);
end = _fast_itoa(s, (unsigned int)ip, 16, false);
b = 8 - (end-s);
while(0 < b--) this->push_back(L'0');
for(int i = 0; s[i]; ++i) this->push_back(s[i]);
}
break;
case L'%':
this->push_back(L'%');
break;
}
++fmt;
}// end while
va_end(ap);
_data[_size] = 0;
}
////////////////////////////////////////
String& trim_left()
{
size_t i = 0;
while(_data[i] == L' ') ++i;
if(i){
_copy(_data, _data+i, _size -= i);
}
return *this;
}
String& trim_right()
{
for(size_t i = _size - 1; _data[i] == L' ' && i >= 0; --i, _size--);
_data[_size] = L'\0';
return *this;
}
String& all_trim()
{
trim_left(); trim_right();
return *this;
}
///////////////////////////////////////////
const wchar_t* c_str() const
{
return _data;
}
size_t length() const
{
return _size;
}
size_t capacity() const
{
return _capacity;
}
void swap(String& rhs)
{
std::swap(_data, rhs._data);
}
// 预留一定的空间
void reserve(size_t size)
{
assert(size > 0);
delete[] _data;
_size = 0;
_capacity = size;
_data = new wchar_t[_capacity];
*_data = L'\0';
}


  • hellovfp
  • 排序查找
    6
该楼层疑似违规已被系统折叠 隐藏此楼查看此楼
private:
// 简单的拷贝函数
void _copy(wchar_t *dst, const wchar_t *src, size_t len)
{
for(size_t i = 0; i < len; ++i){
*dst++ = src[i];
}
*dst = L'\0';
}
//简单的比较函数
int _cmp(const wchar_t *a, const wchar_t *b)
{
for(; *a == *b; a++, b++)
if(*a == L'\0') return 0;
return (*a < *b) ? -1 : +1;
}
//重新分配内存,保持原数据不变
wchar_t* _renew(wchar_t *src, size_t src_size, size_t new_size)
{
size_t copy_size = (new_size > src_size) ? src_size : new_size;
wchar_t *p = new wchar_t[new_size + 1];
_copy(p, src, copy_size);
delete[] src;
return p;
}
// 判断是否是数字字符
bool is_digit(wchar_t ch)
{
return (ch >= L'0' && ch <= L'9') ? true : false;
}
// 提取字符串中的数字
int _atoi(const wchar_t *s, int &i)
{
int n;
for(n = 0;is_digit(*s); s++, i++){
n = (n << 3) +n +n + *s - L'0';
}
return n;
}
//////////////////////////////////////////////////////////
wchar_t* _uitoa(wchar_t *buf, unsigned int val, int base, bool sign)
{
wchar_t tmp[12] = {0}, *p = tmp + 11;
do{
*--p = val % base + L'0';
}while(val/=base);
if( sign ) *--p = L'-';
while(*p) *buf++ = *p++;
*buf = 0;
return buf;
}
wchar_t* _fast_itoa(wchar_t *buf, unsigned int val, int base, bool sign)
{
if((base & 1) == 0) //是2的n次方
{
int c = 0, b = base;
wchar_t tmp[12] = {0}, *p = tmp + 11, base_char;
while(b>>=1) c++;
do{
base_char = (val &(base-1)) > 9 ? L'a' - 10 : L'0';
*--p = (val & (base -1)) + base_char;
}while(val>>=c);
if( sign ) *--p = L'-';
while(*p) *buf++ = *p++;
*buf = 0;
}
return buf;
}
int ftoa_4(wchar_t *buf, double val, int prec)
{
//double tround[] = {
//0.5,
//0.05,
//0.005,
//0.0005,
//0.00005,
//0.000005,
//0.0000005,
//0.00000005,
//0.000000005,
//0.0000000005,
//0.00000000005,
//0.000000000005,
//0.0000000000005,
//0.00000000000005,
//0.000000000000005
//};
int tmp;
int sign = 0;
wchar_t *p = buf;
wchar_t *end;
if(buf == NULL) return -1;
/*Is less than 0*/
if(val < 0)
{
sign = 1;
val = -val;
}
//val += tround[prec];
tmp = (int)val;//tmp is the part of int
val -= tmp;//sumF is the part of float
/*Int ===> String*/
do
{
*(buf++) = tmp % 10 + L'0';
}while(tmp /= 10);
if(sign)
{
*buf++ = L'-';
}
end = buf;
end--;
for(;p<end; p++, end--)
{
*p = *p + *end;
*end = *p - *end;
*p = *p -*end;
}
*buf++ = L'.';
while(prec--)
{
*buf++ = (int)(val*10) + L'0';
val = val*10 - (int)(val*10);
}
*buf = 0;
return 0;
}
private:
size_t _size;
size_t _capacity;
wchar_t *_data;
};


  • hellovfp
  • 排序查找
    6
该楼层疑似违规已被系统折叠 隐藏此楼查看此楼
研究了一下printf的实现,搞了一个精简版的格式化format函数,实际证明,使用不同的优化部分,可以比微软的sprintf实现还要快上一点点,下面上10万次运行时间结果:
C++的stringstream要达到相同的的结果,效率实在令人无语。
还可以参考CPP format进一步实现type safe,不过可能需要C++11的新功能参数模版。


  • hellovfp
  • 排序查找
    6
该楼层疑似违规已被系统折叠 隐藏此楼查看此楼
测试代码:
int _tmain(int argc, _TCHAR* argv[])
{
setlocale(LC_CTYPE, "");
String s1;
s1.reserve(10);
s1.push_back(L'H');
s1.push_back(L'H');
s1.push_back(0);
PR(s1);
s1.push_back(L'H');
s1.push_back(0);
PR(s1);
s1.push_back(L'H');
s1.push_back(0);
PR(s1);
s1 = L"Power";
PR(s1);
s1.push_back(L' ');
s1.push_back(L'O');
s1.push_back(L'n');
s1.push_back(L'e');
s1.push_back(0);
PR(s1);
short sval = 119;
wchar_t str[] = L"copy str here";
s1.reserve(128);
RunTimer rtimer;
for(int i=0; i < 100000; i++)
s1.format(L"Test\' %03d-%d%c %.3f %d%% %s! %x %d:%p", 67, 78, L'Y', 31.1415, sval, str, sval, sval, &sval);
printf("format use %.3f\n", rtimer.elapse()); //0.360, 0.311
PR(s1);
printf("%.3f\n%p\n", 3.1415, &sval);
char buf[256];
rtimer.restart();
for(int i = 0; i < 100000; i++)
sprintf_s(buf, 256,"Test\' %03d-%d%c %.3f %d%% %s! %x %d:%p", 67, 78, L'Y', 31.1415, sval, str, sval, sval, &sval);
printf("sprintf_s use %.3f\n", rtimer.elapse()); //0.344
s1 = L"Power of system is just now, go to work and more";
rtimer.restart();
for(int i = 0; i < 100000; i++)
s1.find(L"more");
printf("find use %.3f\n", rtimer.elapse()); //0.016
PR(s1);
rtimer.restart();
std::stringstream ss;
for(int i = 0; i < 100000; i++)
{
ss.precision(5);
ss.setf(std::ios_base::fixed, std::ios_base::floatfield);
ss << 3.14159;
ss.width(3);
ss.fill('#');
ss << 28;
ss << str;
}
s1 = ss.str().c_str();
printf("stringstream use %.3f\n", rtimer.elapse()); //0.016
//PR(s1);
return 0;
}


  • hellovfp
  • 排序查找
    6
该楼层疑似违规已被系统折叠 隐藏此楼查看此楼
未优化前比sprintf慢,大概0.360秒


  • 贴吧用户_7WAV3t5
  • 并查集
    11
该楼层疑似违规已被系统折叠 隐藏此楼查看此楼
,这吧没人,去主吧发发?有没兴趣继续研究下TFS输入法(几乎没有人公开研究)?


登录百度账号

扫二维码下载贴吧客户端

下载贴吧APP
看高清直播、视频!
  • 贴吧页面意见反馈
  • 违规贴吧举报反馈通道
  • 贴吧违规信息处理公示
  • 8回复贴,共1页
<<返回图形编程forc吧
分享到:
©2025 Baidu贴吧协议|隐私政策|吧主制度|意见反馈|网络谣言警示