string这个词很容易让我们联想到str,也就是字符串,实际上string和字符串的关联性还是很强的。
很多字符串的题目都是string类的形式出现的,日常工作中为了方便使用都是用的string类,
标准string类
使用string类时,必须加上#include头文件和using namespace std
这里介绍一个关键字auto,这个中C/C++中,使用auto修饰的变量,具有自动存储器的局部变量,C++11中,auto不再是一个存储类型的指示符,而是作为一个新的类型指示符来指示编译器,auto声明的变量必须由编译器在编译时期推导得到
auto和auto*每区别,使用auto声明引用类型时必须加上&
auto不可做为函数参数,但是可以做返回值,并且不能直接声明数组
#include<iostream>
using namespace std;
int func1()
{
return 10;
}
void func2(auto a)
{}// 可以做返回值,但是建议谨慎使用
auto func3()
{
return 3;
}
int main()
{
int a = 10;
auto b = a;
auto c = 'a';
auto d = func1();
auto e;
cout << typeid(b).name() << endl;
cout << typeid(c).name() << endl;
cout << typeid(d).name() << endl;
int x = 10;
auto y = &x;
auto* z = &x;
auto& m = x;
cout << typeid(x).name() << endl;
cout << typeid(y).name() << endl;
cout << typeid(z).name() << endl;
auto aa = 1, bb = 2;
auto cc = 3, dd = 4.0;
auto array[] = { 4, 5, 6 };
return 0;
}
for
for循环后的括号和冒号:分为两部分,第一部分是范围内用于迭代的变量。第二部表示被迭代的范围。,自动迭代,自动取数据,自动判断结束。
#define _CRT_SECURE_NO_WARNINGS
#include<iostream>
#include<string>
#include<map>
using namespace std;
int main()
{
int array[]={ 1,2,3,4,5 };
for (int i = 0; i < sizeof(array) / sizeof(array[0]); ++i)
{
array[i] *= 2;
}
for (int i = 0; i < sizeof(array) / sizeof(array[0]); ++i)
{
cout << array[i] << endl;
}
for (auto& e : array)
{
e *= 2;
}
for (auto e : array)
{
cout << e << " " << endl;
}
string str("hello world");
for (auto ch : str)
{
cout << ch << " ";
}
cout << endl;
return 0;
}
string-----构造空的string类对象,即空字符串
string(const char *s)--------用C-string来构造string类对象
string(size_t n char c)---------string 类对象中包含n个字符
string(const string &s) -------拷贝构造函数
void Teststring()
{
string s1; // 构造空的string类对象s1
string s2("hello bit"); // 用C格式字符串构造string类对象s2
string s3(s2); // 拷贝构造s3
}
| size | 返回字符串有效字符长度 |
|---|---|
| length | 返回字符串有效字符长度 |
| capacity | 空间总大小 |
| empty | 检测字符串释放为空,返回true/false |
| clear | 清空有效字符 |
| reverse | 为字符串预留空间 |
| resize | 将有效字符的个数改成n个,多处的空间用c填充 |
| operator | 返回Pos 位置的字符,const string类对象调用 |
|---|---|
| begin+end | begin获取一个字符的迭代器+end获取最后一个字符下一位置的迭代器 |
| rbegin+rend | begin获取一个字符的迭代器+end获取最后有一个字符位置的迭代器 |
| 范围for | c++11支持更简洁的范围for的新遍历方式 |
函数名称 功能说明
push_back 在字符串后尾插字符c
append 在字符串后追加一个字符串
operator+= 在字符串后追加字符串str
c_str返回C格式字符串
find + npos 从字符串pos位置开始往后找字符c,返回该字符在字符串中的
位置
rfind 从字符串pos位置开始往前找字符c,返回该字符在字符串中的
位置
substr 在str中从pos位置开始,截取n个字符,然后将其返回

2803

被折叠的 条评论
为什么被折叠?



