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”这样的 ...
随机推荐
- 【PHP设计模式 01_DuoTai.php】多态的说明
<?php /** * [多态] * 定义一个抽象类:Tiger,有两个子类:XTiger 和 MTiger */ header("Content-type: text/html; c ...
- move语义和右值引用
C++11支持move语义,用以避免非必要拷贝和临时对象. 具体内容见收藏中的“C++右值引用” .
- mha的搭建步骤(一主一从架构)
所需脚本文件到这里下载:http://note.youdao.com/share/web/file.html?id=ae8b11a61f7a8aa7b52aac3fcf0c4b83&type= ...
- python实现删除文件与目录的方法
os.remove(path) 删除文件 path. 如果path是一个目录, 抛出 OSError错误.如果要删除目录,请使用rmdir().os.rmdir()只能删除空目录 remove() 同 ...
- ZOJ 3860: - Find the Spy
3860 - Find the Spy Time Limit:2000MS Memory Limit:65536KB 64bit IO Format:%lld & %llu S ...
- jquery 实现页面局部刷新ajax做法
这个方法就多了去了,常见的有以下几种:下面介绍全页面刷新方法:有时候可能会用到 window.location.reload()刷新当前页面. parent.location.reload()刷新父亲 ...
- Unity-Animator深入系列---deltaPosition&deltaRotation
回到 Animator深入系列总目录 官方文档给出的信息非常含糊 Gets the avatar delta position for the last evaluated frame. 测试了一下, ...
- linux里用cmake安装的软件要怎么卸载?
找到make install之后产生的这个文件install_manifest.txt 这里在build文件里面有一个 install_manifest.txt,在里面有安装的所有东西的路径,删除它们 ...
- GROUP BY和ORDER BY共用
SELECT BatchNumber,MAX(Id) FROM dbo.SceneryOrder AND BatchNumber<>'' GROUP BY BatchNumber DESC
- 蚂蚁运输(ant)
蚂蚁运输(ant)Time Limit:5000ms Memory Limit:64MB[题目描述] LYK 在观察一些蚂蚁.蚂蚁想要积攒一些货物来过冬.积攒货物的方法是这样的.对于第i只蚂蚁, 它要 ...