c++ string的实现。
第三次做了。只是做个复习。偶然发现之前的版本有内存泄露。基本功还是不过关。这次应该没有内存泄漏了。虽然是个简单版本。
1)了解堆,栈,值copy。
2)几个常用的c的字符函数和c中的char 如何表示串。和c++的string不同。
3)string。自动有‘\0’, 。 "hi.",这样一个常字符串,编译器也是会给'\0'的。char [3]={xxx,'\0'} 必须自己加。
main.cpp
#include <iostream>
#include "Mystring.h"
using namespace std; void main_mystring();
int main()
{
main_mystring(); return ;
} //mystring g_hi("hia");
//void main_mystring()
//{
// mystring a;
// mystring b("hi");
// mystring c=mystring("linson");
// mystring d=c;
// d=b;
// d[2]='a';
//} void main_mystring()
{
Mystring hi("hi");
Mystring hi2="h2";
Mystring c=hi;
c=c;
cout<<c<<endl; c[]='x'; cout<<c<<endl; Mystring emptystr;
cout<<emptystr<<endl;
emptystr=hi;
cout<<emptystr<<endl; Mystring add=hi+hi2;
cout<<add<<endl; }
Mystring.h
#ifndef MYSTRING_H_INCLUDED
#define MYSTRING_H_INCLUDED #include <stdio.h>
#include <iostream>
using namespace std; class Mystring
{
public:
Mystring(char* const);
Mystring();
Mystring(const Mystring&);
Mystring& operator=(const Mystring&);
char& operator[](unsigned int);
Mystring operator+(const Mystring& right);
~Mystring();
private:
Mystring(char * const,unsigned int);//专给+操作符使用.之前的版本应该内存泄漏了.
unsigned int CharSize;
char* pChar;
friend ostream& operator<<(ostream& os,const Mystring& mys);
}; ostream& operator<<(ostream& os,const Mystring& mys); #endif // MYSTRING_H_INCLUDED
Mystring.cpp
#include "Mystring.h"
#include "malloc.h"
#include "string.h"
#include <stdexcept> using namespace std; Mystring::Mystring(char* const _pchar)
{
int Length=;
char* pflag=_pchar;
if(_pchar==0)
{
throw runtime_error("null pointer");
}
while(*pflag!=0x0 && Length<*)////strlen还是感觉不安全.如果本来形参就是一个没有\0结尾的符号呢.长度怕出错.随便假设最大为1m长度的字符串吧.
{
++Length;
++pflag;
}
pChar=(char *)malloc(Length+);
//cout<<"new"<<(void *)pChar<<endl;
if(pChar!=)
{
strncpy(pChar,_pchar,Length);
}
else
{
throw runtime_error("alloc error.");
}
CharSize=Length;
pChar[CharSize]='\0'; } Mystring::Mystring(const Mystring& source)
{
pChar=(char *)malloc(source.CharSize+);
if(pChar==)
{
throw runtime_error("alloc error.");
}
//cout<<"new"<<(void *)pChar<<endl;
strncpy(pChar,source.pChar,source.CharSize);
CharSize=source.CharSize;
pChar[CharSize]='\0';
} Mystring::Mystring()
{
pChar=(char *)malloc();
//cout<<"new"<<(void *)pChar<<endl;
if(pChar==)
{
throw runtime_error("alloc error.");
}
CharSize=;
pChar[CharSize]='\0'; } Mystring& Mystring::operator=(const Mystring& source)
{
string temp=string(source);
std::swap(pChar,temp.pChar); return *this;
return *this;} char& Mystring::operator[](unsigned int index)
{
if(index>=&& index<CharSize)
{
return pChar[index];
}
else
{
throw runtime_error("over range!");
}
} Mystring Mystring::operator+(const Mystring& right)
{ char * temppChar=(char *)malloc(this->CharSize+right.CharSize+);
//cout<<"new"<<(void *)temppChar<<endl;
if(pChar==)
{
throw runtime_error("alloc error.");
}
strncpy(temppChar,this->pChar,this->CharSize);
strncpy(temppChar+this->CharSize,right.pChar,right.CharSize);
int tempCharSize=this->CharSize+right.CharSize;
temppChar[tempCharSize]='\0';
return Mystring(temppChar,tempCharSize);
//Mystring()
} Mystring::Mystring(char * const _p,unsigned int _size)
{
pChar=_p;
CharSize=_size;
} Mystring::~Mystring()
{
//cout<<"del"<<(void *)pChar<<endl;
free(pChar);
} ostream& operator<<(ostream& os,const Mystring& mys)
{
return os<<mys.pChar;
}
c++ string的实现。的更多相关文章
- 透过WinDBG的视角看String
摘要 : 最近在博客园里面看到有人在讨论 C# String的一些特性. 大部分情况下是从CODING的角度来讨论String. 本人觉得非常好奇, 在运行时态, String是如何与这些特性联系上的 ...
- JavaScript String对象
本编主要介绍String 字符串对象. 目录 1. 介绍:阐述 String 对象的说明以及定义方式. 2. 实例属性:介绍 String 对象的实例属性: length. 3. 实例方法:介绍 St ...
- ElasticSearch 5学习(9)——映射和分析(string类型废弃)
在ElasticSearch中,存入文档的内容类似于传统数据每个字段一样,都会有一个指定的属性,为了能够把日期字段处理成日期,把数字字段处理成数字,把字符串字段处理成字符串值,Elasticsearc ...
- [C#] string 与 String,大 S 与小 S 之间没有什么不可言说的秘密
string 与 String,大 S 与小 S 之间没有什么不可言说的秘密 目录 小写 string 与大写 String 声明与初始化 string string 的不可变性 正则 string ...
- js报错: Uncaught RangeError: Invalid string length
在ajax请求后得到的json数据,遍历的时候chrome控制台报这个错误:Uncaught RangeError: Invalid string length,在stackoverflow查找答案时 ...
- c# 字符串连接使用“+”和string.format格式化两种方式
参考文章:http://www.liangshunet.com/ca/201303/218815742.htm 字符串之间的连接常用的两种是:“+”连接.string.format格式化连接.Stri ...
- 【手记】注意BinaryWriter写string的小坑——会在string前加上长度前缀length-prefixed
之前以为BinaryWriter写string会严格按构造时指定的编码(不指定则是无BOM的UTF8)写入string的二进制,如下面的代码: //将字符串"a"写入流,再拿到流的 ...
- JavaScript中String对象的方法介绍
1.字符方法 1.1 charAt() 方法,返回字符串中指定位置的字符. var question = "Do you like JavaScript?"; alert(ques ...
- 在多线程编程中lock(string){...}隐藏的机关
常见误用场景:在订单支付环节中,为了防止用户不小心多次点击支付按钮而导致的订单重复支付问题,我们用 lock(订单号) 来保证对该订单的操作同时只允许一个线程执行. 这样的想法很好,至少比 lock( ...
- BCL中String.Join的实现
在开发中,有时候会遇到需要把一个List对象中的某个字段用一个分隔符拼成一个字符串的情况.比如在SQL语句的in条件中,我们通常需要把List<int>这样的对象转换为“1,2,3”这样的 ...
随机推荐
- 通过Oracle数据库访问控制功能的方法(Database access control)
修改sqlnet.ora文件中的IP列表后都需要重启监听才能生效.(原文是: Any changes to the values requires the TNS listener to be sto ...
- HDU 3487:Play with Chain(Splay)
http://acm.hdu.edu.cn/showproblem.php?pid=3487 题意:有两种操作:1.Flip l r ,把 l 到 r 这段区间 reverse.2.Cut a b c ...
- ecshop添加商品选择品牌时如何按拼音排序
ECSHOP后台添加新商品时,有一个选择品牌的下拉框,如果品牌太多,在下拉框里查找起来很不方便. 我想给“下拉框里的品牌列表”按品牌名的拼音排序,比如有“中国水利出版社” “中国人民出版社” 这两个品 ...
- redis 几种数据类型往数据库存数据和取数据的帮助类
package com.fndsoft.bcis.utils; import org.springframework.beans.factory.annotation.Autowired; impor ...
- Poj(2679),SPFA,邻接表(主流写法)
题目链接:http://poj.org/problem?id=3268 题意: 有编号为1-N的牛,它们之间存在一些单向的路径.给定一头牛的编号,其他牛要去拜访它并且拜访完之后要返回自己原来的位置,求 ...
- CyclicBarrier原理
转载:http://www.cnblogs.com/skywang12345/p/3533995.html CyclicBarrier是一个同步辅助类,允许一组线程互相等待,直到到达某个公共屏障点 ( ...
- 遍历对象的list删除时报错问题。
我们对一个对象的list或者map进行删除操作时,可能会这么写 for(Distributor distributor:distributorList){ String distributorShor ...
- webform数据导出
把数据放到一个泛型集合里,再把泛型集合里面的数据放到一个table中,设置好文件路径,然后进行文件读取,最后供用户下载. 数据导出放在一个按钮中就可以了 using System; using Sys ...
- IOSUIcontrol事件
UIKit提供了一组控件:UISwitch开关.UIButton按钮.UISegmentedControl分段控件.UISlider滑块.UITextField文本字段控件.UIPageControl ...
- IOSView翻转扭矩位移
CoreGraphics.h CGAffineTransform rotation = CGAffineTransformMakeRotation(M_PI_2);[xxx setTransform: ...