- string类的构造函数:
#include
#include
int main()
{
std::string str1 = std::string("123");
std::string str2 = std::string(5, '4');
std::string str3 = std::string(str2);
std::string str4 = std::string();
std::string str5 = std::string("234234", 2);
std::string str6 = std::string("23", 3);
std::string str7 = std::string(str1.begin() + 1, str1.end());
std::string str8 = std::string(str2, 2);
std::string str9 = std::string(str2, 2, 2);
char ch[5] = "1234";
std::string str10 = std::string(ch);
std::cout << str1 << std::endl;
std::cout << str2 << std::endl;
std::cout << str3 << std::endl;
std::cout << str4 << std::endl;
std::cout << str5 << std::endl;
std::cout << str6 << std::endl;
std::cout << str7 << std::endl;
std::cout << str8 << std::endl;
std::cout << str9 << std::endl;
std::cout << str10 << std::endl;
}
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
- 15
- 16
- 17
- 18
- 19
- 20
- 21
- 22
- 23
- 24
- 25
- 26
- 27
- 28
123
44444
44444
23
23
23
444
44
1234
- string的find函数方法:
#include
#include
int main()
{
std::string str = "2342342342356";
std::string temp = "2342";
std::cout << str.find("56", 2) << std::endl;
std::cout << str.find("56") << std::endl;
std::cout << str.find("89") << std::endl;
std::cout << str.find(temp, 2) << std::endl;
std::cout << str.find(temp) << std::endl;
std::cout << str.find(temp + '0') << std::endl;
std::cout << str.find("345", 3, 2) << std::endl;
std::cout <<"npos = " << std::string::npos << std::endl;
std::cout << str.find("345", 3) << std::endl;
std::cout << str.find("2", 2) << std::endl;
unsigned long long ans = std::string::npos;
int cnt = 0;
while (ans)
{
ans /= 2;
++cnt;
}
std::cout << cnt << std::endl;
}
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
- 15
- 16
- 17
- 18
- 19
- 20
- 21
- 22
- 23
- 24
- 25
11
11
18446744073709551615
3
0
18446744073709551615
4
npos = 18446744073709551615
18446744073709551615
3
64
- 智能指针与常规指针的区别是一旦程序运行离开了代码块,智能指针将自动的释放指向的内存,因此new的时候不需要delete语句,注:智能化指针初始化不能直接被赋值数据的地址,只能被赋值为在堆内存的地址,因为智能指针在释放内存时会调用delete运算符。
#include
#include
#include
class Report
{
private:
std::string str;
public:
Report(const std::string s) :str(s)
{
std::cout << "Object created!\n";
}
~Report()
{
std::cout << "Object deleted!\n";
}
void comment() const
{
std::cout << str << std::endl;
}
};
int main()
{
std::string str;
{
std::auto_ptr<Report> ps(new Report("using auto_ptr"));
ps->comment();
}
{
std::shared_ptr<Report> ps(new Report("using shared_ptr"));
ps->comment();
}
{
std::unique_ptr<Report> ps(new Report("using unique_ptr"));
ps->comment();
}
{
}
return 0;
}
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
- 15
- 16
- 17
- 18
- 19
- 20
- 21
- 22
- 23
- 24
- 25
- 26
- 27
- 28
- 29
- 30
- 31
- 32
- 33
- 34
- 35
- 36
- 37
- 38
- 39
- 40
- 41
Object created!
using auto_ptr
Object deleted!
Object created!
using shared_ptr
Object deleted!
Object created!
using unique_ptr
Object deleted!
- 三种智能指针的区别:
- auto_ptr:如果前面的智能指针的指向和现有的智能指针指向了同一个对象,则针对这个对象的操作所有权传递给了现有的指针,而原有的智能指针无法引用该对象,auto_ptr使用需要使用new分配内存,注,unique_ptr和shared_ptr指针不能转换成auto_ptr,但是auto_ptr指针能转换成unique_ptr和shared_ptr指针。
- unique_ptr: 如果前面的智能指针和现有的智能指针指向了同一个对象,则编译器会报错,unique_ptr使用需要使用new或者new[]分配内存。
- shared_ptr如果前面的智能指针和现有的智能指针指向了同一个对象,则二者共享针对这个对象操作的所有权,shared_ptr使用需要new分配内存,注shared_ptr指针不能与unique_ptr共享同一个对象所有权。
#include
#include
#include
int main()
{
std::unique_ptr<std::string> film1[5] =
{
std::unique_ptr<std::string>(new std::string("u111")),
std::unique_ptr<std::string>(new std::string("u222")),
std::unique_ptr<std::string>(new std::string("u333")),
std::unique_ptr<std::string>(new std::string("u444")),
std::unique_ptr<std::string>(new std::string("u555"))
};
std::auto_ptr<std::string> film2[5] =
{
std::auto_ptr<std::string>(new std::string("a111")),
std::auto_ptr<std::string>(new std::string("a222")),
std::auto_ptr<std::string>(new std::string("a333")),
std::auto_ptr<std::string>(new std::string("a444")),
std::auto_ptr<std::string>(new std::string("a555"))
};
std::shared_ptr<std::string> film3[5] =
{
std::shared_ptr<std::string>(new std::string("u111")),
std::shared_ptr<std::string>(new std::string("u222")),
std::shared_ptr<std::string>(new std::string("u333")),
std::shared_ptr<std::string>(new std::string("u444")),
std::shared_ptr<std::string>(new std::string("u555"))
};
std::shared_ptr<std::string> ptr = film3[2];
(*ptr) += "123";
std::cout << (*ptr) << " " << (*film3[2]) << std::endl;
std::unique_ptr<double[]> unptr1(new double(5));
std::unique_ptr<std::string[]> unprt2(new std::string[4]);
return 0;
}

- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
- 15
- 16
- 17
- 18
- 19
- 20
- 21
- 22
- 23
- 24
- 25
- 26
- 27
- 28
- 29
- 30
- 31
- 32
- 33
- 34
- 35
- 36
- 37
- 38
- 39
- 40
- 41
- 42
- 43
- 44
- 45
- 46
u333123 u333123