第三次做了。只是做个复习。偶然发现之前的版本有内存泄露。基本功还是不过关。这次应该没有内存泄漏了。虽然是个简单版本。

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的实现。的更多相关文章

  1. 透过WinDBG的视角看String

    摘要 : 最近在博客园里面看到有人在讨论 C# String的一些特性. 大部分情况下是从CODING的角度来讨论String. 本人觉得非常好奇, 在运行时态, String是如何与这些特性联系上的 ...

  2. JavaScript String对象

    本编主要介绍String 字符串对象. 目录 1. 介绍:阐述 String 对象的说明以及定义方式. 2. 实例属性:介绍 String 对象的实例属性: length. 3. 实例方法:介绍 St ...

  3. ElasticSearch 5学习(9)——映射和分析(string类型废弃)

    在ElasticSearch中,存入文档的内容类似于传统数据每个字段一样,都会有一个指定的属性,为了能够把日期字段处理成日期,把数字字段处理成数字,把字符串字段处理成字符串值,Elasticsearc ...

  4. [C#] string 与 String,大 S 与小 S 之间没有什么不可言说的秘密

    string 与 String,大 S 与小 S 之间没有什么不可言说的秘密 目录 小写 string 与大写 String 声明与初始化 string string 的不可变性 正则 string ...

  5. js报错: Uncaught RangeError: Invalid string length

    在ajax请求后得到的json数据,遍历的时候chrome控制台报这个错误:Uncaught RangeError: Invalid string length,在stackoverflow查找答案时 ...

  6. c# 字符串连接使用“+”和string.format格式化两种方式

    参考文章:http://www.liangshunet.com/ca/201303/218815742.htm 字符串之间的连接常用的两种是:“+”连接.string.format格式化连接.Stri ...

  7. 【手记】注意BinaryWriter写string的小坑——会在string前加上长度前缀length-prefixed

    之前以为BinaryWriter写string会严格按构造时指定的编码(不指定则是无BOM的UTF8)写入string的二进制,如下面的代码: //将字符串"a"写入流,再拿到流的 ...

  8. JavaScript中String对象的方法介绍

    1.字符方法 1.1 charAt() 方法,返回字符串中指定位置的字符. var question = "Do you like JavaScript?"; alert(ques ...

  9. 在多线程编程中lock(string){...}隐藏的机关

    常见误用场景:在订单支付环节中,为了防止用户不小心多次点击支付按钮而导致的订单重复支付问题,我们用 lock(订单号) 来保证对该订单的操作同时只允许一个线程执行. 这样的想法很好,至少比 lock( ...

  10. BCL中String.Join的实现

    在开发中,有时候会遇到需要把一个List对象中的某个字段用一个分隔符拼成一个字符串的情况.比如在SQL语句的in条件中,我们通常需要把List<int>这样的对象转换为“1,2,3”这样的 ...

随机推荐

  1. ORA-29339错误解决办法

    create tablespace TBS_JACK_16k blocksize 16k datafile '/u01/app/oracle/oradata/orcl/TBS_JACK_32K_01. ...

  2. IE6兼容透明背景图

    JS代码: <!--[if IE 6]><script src="~/Scripts/UI/DD_belatedPNG.js"></script> ...

  3. python中列表和字典常用方法和函数

    Python列表函数&方法 Python包含以下函数: 序号 函数 1 cmp(list1, list2)比较两个列表的元素 2 len(list)列表元素个数 3 max(list)返回列表 ...

  4. 关于hibernate对应关系之后取值的问题

    hibernate对应关系之后取值,比如一对一关系,取不到值,需要检查PO类中是否生成了getter及setter方法.

  5. FIleReader无法解决编码问题

    可把FileReader 用InputStreamReader(new FileInputStream(String fileptah),"utf-8")替代

  6. c#之线程池

    using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.I ...

  7. A simple problem 分类: 哈希 HDU 2015-08-06 08:06 1人阅读 评论(0) 收藏

    A simple problem Time Limit: 4000/2000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others) To ...

  8. 一些常用的SQL查询语句

    学习网站:http://www.w3cschool.cc/sql/sql-tutorial.html 一:查询所有表的属性 SELECT 'ALTER TABLE '+ CASE WHEN O.sch ...

  9. DLL数据共享在不同处定义效果不同..

    DLL头文件: #ifndef _DLL_SAMPLE_H #define _DLL_SAMPLE_H #pragma data_seg("ShareReadAndWrite") ...

  10. grails的layouts模板页面使用

    使用方式1: layouts文件夹下新建文件,名称和Controller名称相同,例如UserController,layouts下面创建user.gsp,此时,user站点下所有的页面都将套用 us ...