new和delete原理以及string的函数

本文详细解析了C++中new、delete以及数组版new[]和delete[]的内存管理原理,包括内置类型和自定义类型的使用,以及string类的insert、erase、replace和find方法。
摘要由CSDN通过智能技术生成

一,newdelete的实现原理

1 ,内置类型

        如果申请的是内置类型的空间,new malloc delete free 基本类似,不同的地方是:new/delete申请和释放的是单个元素的空间, new[] delete[] 申请的是连续空间,而且 new 在申请空间失败时会抛异常,malloc 会返回 NULL。

2,自定义类型

  • new的原理

        1. 调用operator new函数申请空间

        2. 在申请的空间上执行构造函数,完成对象的构造

  • delete的原理

        1. 在空间上执行析构函数,完成对象中资源的清理工作

        2. 调用operator delete函数释放对象的空间

  • new T[N]的原理

        1. 调用operator new[]函数,在operator new[]中实际调用operator new函数完成N个对

     象空间的申请

        2. 在申请的空间上执行N次构造函数

  • delete[]的原理

        1. 在释放的对象空间上执行N次析构函数,完成N个对象中资源的清理

        2. 调用operator delete[]释放空间,实际在operator delete[]中调用operator delete来释

放空间

二,new的用处

new的好处有以下优点:

1,用法上变简洁了

2,可以控制初始化

Int *p3 =new int(10)  //new一个int对象,初始化为10
Int* p4 =new int[10]{1,2,3,4,5}  //new10个对象

3,自定义类型,开空间+构造函数

Struct  ListNode
{
   ListNode* _next;
   ListNode *_prev;
   int _val;

构造函数:

ListNode(int val)
:_next(nullptr)
,_prev(nullptr)
,_val(val)
{}

};

ListNode *node=new ListNode(1);

4,new失败以后抛异常,不需要手动检查

三,string的函数

1,insert

1.1,在原串下标为pos的字符前插入字符串str

basic_string& insert (size_type pos, const basic_string& str);

1.2,str从下标为pos1开始数的n个字符插在原串下标为pos的字符前

basic_string& insert (size_type pos, const basic_string& str, size_type pos1, size_type n);

1.3,在原串下标为pos的字符前插入n个字符c

basic_string& insert (size_type pos, size_type n, char c);

1.4,举例

// inserting into a string
#include <iostream>
#include <string>

int main ()
{
  std::string str="to be question";
  std::string str2="the ";
  std::string str3="or not to be";
  std::string::iterator it;

  // used in the same order as described above:
  str.insert(6,str2);                 // to be (the )question
  str.insert(6,str3,3,4);             // to be (not )the question
  str.insert(10,"that is cool",8);    // to be not (that is )the question
  str.insert(10,"to be ");            // to be not (to be )that is the question
  str.insert(15,1,':');               // to be not to be(:) that is the question
  it = str.insert(str.begin()+5,','); // to be(,) not to be: that is the question
  str.insert (str.end(),3,'.');       // to be, not to be: that is the question(...)
  str.insert (it+2,str3.begin(),str3.begin()+3); // (or )

  std::cout << str << '\n';
  return 0;
}

2,erase

2.1,删除指定位置的字符

iterator erase (iterator p);

2.2,删除指定长度的字符串

string& erase(size_t pos=0, size_t len = npos);

2.3,删除指定范围的字符串

iterator erase (iterator first, iterator last);

2.4,举例

// string::erase
#include <iostream>
#include <string>

int main ()
{
  std::string str ("This is an example sentence.");
  std::cout << str << '\n';
                                           // "This is an example sentence."
  str.erase (10,8);                        //            ^^^^^^^^
  std::cout << str << '\n';
                                           // "This is an sentence."
  str.erase (str.begin()+9);               //           ^
  std::cout << str << '\n';
                                           // "This is a sentence."
  str.erase (str.begin()+5, str.end()-9);  //       ^^^^^
  std::cout << str << '\n';
                                           // "This sentence."
  return 0;
}

 

3,replace

3.1,替换从pos位置开始,长度为len的子串为新的str字符串。

string& replace (size_t pos, size_t len, const string& str);

3.2,replace()函数除了拥有前面版本的所有参数外,还新增了subpossublen两个参数,分别表示新字符串str的子串的起始位置和长度。

string& replace (size_t pos, size_t len, const string& str, size_t subpos, size_t sublen);

3. 3,replace()函数接受一个C风格的字符串作为新的字符串。

string& replace (size_t pos, size_t len, const char* s);

3.4,举例

// replacing in a string
#include <iostream>
#include <string>

int main ()
{
  std::string base="this is a test string.";
  std::string str2="n example";
  std::string str3="sample phrase";
  std::string str4="useful.";

  // replace signatures used in the same order as described above:

  // Using positions:                 0123456789*123456789*12345
  std::string str=base;           // "this is a test string."
  str.replace(9,5,str2);          // "this is an example string." (1)
  str.replace(19,6,str3,7,6);     // "this is an example phrase." (2)
  str.replace(8,10,"just a");     // "this is just a phrase."     (3)
  str.replace(8,6,"a shorty",7);  // "this is a short phrase."    (4)
  str.replace(22,1,3,'!');        // "this is a short phrase!!!"  (5)

  // Using iterators:                                               0123456789*123456789*
  str.replace(str.begin(),str.end()-3,str3);                    // "sample phrase!!!"      (1)
  str.replace(str.begin(),str.begin()+6,"replace");             // "replace phrase!!!"     (3)
  str.replace(str.begin()+8,str.begin()+14,"is coolness",7);    // "replace is cool!!!"    (4)
  str.replace(str.begin()+12,str.end()-4,4,'o');                // "replace is cooool!!!"  (5)
  str.replace(str.begin()+11,str.end(),str4.begin(),str4.end());// "replace is useful."    (6)
  std::cout << str << '\n';
  return 0;
}

 

4,find

4.1,返回字符串s1在s中的位置,如果没有找到,则返回-1

#include <iostream>
#include <string.h>
using namespace std;
 
int main()
{
   string s="what are you dong";
   string s1="are";
   int position;
   position=s.find(s1);
   if(position==-1)
    cout<<"not find"<<endl;
   else
    cout<<"position= "<<position<<endl;
   return 0;
}

//position=5

4.2,返回任意字符s1在s中第一次出现的位置,s1为字符,不能为字符串

#include <iostream>
#include <string.h>
using namespace std;
 
int main()
{
   string s="hahahaha";
   string s1="a";
   int position;
   position=s.find_first_of(s1);
   cout<<position<<endl;
   return 0;
}

//1

4. 3,从字符串s下标为a开始查找字符串s1,返回起始位置  s.find(s1,a); 查找不到返回-1

#include <iostream>
#include <string.h>
using namespace std;
 
int main()
{
   string s="have a good time";
   string s1="good";
   int position;
   position=s.find(s1,3);
   cout<<position<<endl;
   position=s.find(s1,12);
   cout<<position<<endl;
   return 0;
}

//7
//-1
  • 20
    点赞
  • 17
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值