一,new和delete的实现原理
1 ,内置类型
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()
函数除了拥有前面版本的所有参数外,还新增了subpos
和sublen
两个参数,分别表示新字符串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