容器:
顺序容器:string/数组/双端队列/链表/哈希表
关联容器:----
| C++ string类的详细总结(看这两个就可以了): 【1】https://blog.csdn.net/qq_42659468/article/details/90381985 【2】https://www.cnblogs.com/xFreedom/archive/2011/05/16/2048037.html |
20200423 | string类: 【1】专门是字符串操作的一个类,非常强大,字符串Cstring,Qstring。 【2】string是一个类,封装之前的那些字符串操作函数如strlen等,通过string类创建的对象如str1,直接str1.strcpy就可以调用; 【3】不用考虑内存的分配和释放,不用考虑越界,都已经处理了。 【4】string类头文件:#include <string> //注意string.h是C语言的头文件 |
| string类属性: 代码: #include <iostream>
#include <string>
using namespace std;
int main()
{
string str1;
str1 = "hello world";
string str2("hello world");
cout << str1.capacity()<<endl; //返回当前容量(即string中不必增加内存即可存放的元素个数)
cout << str1.max_size() << endl;//返回string对象中可存放的最大字符串的长度
cout << str1.size() << endl; //返回当前字符串的大小
cout << str1.length() << endl; //返回当前字符串的长度
cout << str1.empty() << endl; //当前字符串是否为空
str1.resize(20, '*'); //把字符串当前大小置为20,并用字符*填充不足的部分
cout << str1.c_str() << endl;
//c_str()就是将C++的string转化为C的字符串数组,c_str()生成一个const char *指针,指向字符串的首地址
cout << str2 << endl;//str2是一个类 不是一个字符串
system("pause");
return 0;
} 1】 2】 函数c_str()就是将C++的string转化为C的字符串数组,c_str()生成一个const char *指针,指向字符串的首地址 3】 4】 |
| |
| |
| string类的输出: 代码: #include <iostream>
#include <string>
using namespace std;
//string类输出
void strCout()
{
string str1("abcdefg");
//输出全部字符串
cout << str1 << endl;//<<是重载输出,可以输出类str1
cout << str1.c_str() << endl;//c_str指向字符串"abcdefg"的首地址
//输出单个字符
cout << str1[0] << endl; //通过下标运算符[]输出指定字符
cout << str1.at(0) << endl; //at()函数输出指定字符
//输出单个字符不能越界,使用try处理异常
//cout << str1.at(10) << endl; //at()函数输出越界
try
{
str1.at(10);
}
catch (...)
{
cout << "数组输出越界" << endl;
}
}
int main()
{
strCout();
system("pause");
return 0;
} 1】 |
| string修改: #include <iostream>
#include <string>
using namespace std;
//string类修改
void strChange()
{
string str1("abcdefg");
//---修改指定元素
cout << str1 << endl;
str1[2] = 'm';
cout << str1 << endl;
str1.at(2) = 'c';
cout << str1 << endl;
//---中间插入元素
string str2(3, 'w'); //str2="www"
str1.insert(2, str2);//在str1的第2个字符处插入str2,str1="abwwwcdefg"
cout << str1 << endl;
str1.insert(3, "zzz");//在str1的第3个字符处插入"zzz",str1="abwzzzwwcdefg"
cout << str1 << endl;
string str3 = "abcdefg";
string str4 = "hello";
str3.insert(3, str4, 3, 4);//在str3的第3个字符插入str4的第3~4个字符,str3="abclodefg"
cout << str3 << endl;
str4.insert(2, 3, '*');//在str4的第2字符处插入3个*,str4="he***llo"
cout << str4 << endl;
//---尾巴插入元素
string str5 = "abcdefg";
string str6 = "hello";
str5.insert(str5.length(), 3, '*');//在字符串str5尾巴插入3个*,str5="abcdefg***";
//str5 += "***"; //在字符串str5尾巴插入"***"
cout << str5 << endl;
str5 += str6;//将str6拼接到str5尾巴
//str5.append(str6);//将str6拼接到str5尾巴
cout << str5 << endl;
str5.append("%%%");//在字符串str5尾巴插入"%%%"
//str5.append(3,"%");//在字符串str5尾巴插入3个%
cout << str5 << endl;
//---字符串重新赋值
string str7 = "abcdefg";
string str8 = "hello";
str7 = str8;//将str8赋予str7
//str7 = "hello";//将"hello"赋予str7
//cin >> str7;//利用输入给str7重新赋值
str7.assign("abcd", 2);//将"abcd"的前两个字符赋值给str7
str7.assign("abcd", 2,3);//将"abcd"的后两个字符赋值给str7
cout << str7 << endl;
//---字符串删除
string str9 = "abcdefg";
str9.erase(2,3);//从第2个字符开始,删除3个字符
//str9.erase(0, str9.length());//清理从开始到末尾的字符,即清空字符串
cout << str9 << endl;
}
int main()
{
strChange();
system("pause");
return 0;
} 1】 2】 3】 4】 5】 6】 |
| |
| 字符串比较: #include <iostream>
#include <string>
using namespace std;
//string类修改
void strCompare()
{
string str1("abcd");
string str2("abcd");
string str3("abc");
cout << (str1.compare("abcd")) << endl;//完全相等,返回0
cout << (str1 == str2) << endl;//完全相等,返回0
cout << (str1.compare("dcba")) << endl;//返回小于0
cout << (str1.compare("ab")) << endl;//返回大于0
cout << (str1.compare(0, 2, str2, 0, 2)) << endl; //str1[0-2]和str2[0-2]比较,//完全相等,返回0
cout << (str1.compare(0, 2, "abc", 0, 2)) << endl; //str1[0-2]和"abc"[0-2]比较,//完全相等,返回0
}
int main()
{
strCompare();
system("pause");
return 0;
} 1】 2】 |
| 字符串复制: #include <iostream>
#include <string>
using namespace std;
void strCopy()
{
string str1("abcd");
string str2("***%%%");
char *s1 = new char[10];
memset(s1, 0, 10);//s1初始化为0
//char s1[10]={0}; //或者定义一个字符数组
str1.copy(s1, 3, 1);//从str1的第一个字符开始赋值3个字符给s1
cout << s1 << endl; //s1="bcd"
delete s1;
const char* s2 = str2.data();//将string类str2转为字符串数组,返回给s2
str1 = str2.substr(2); //提取子串,提取出 str2 的 下标为2 到末尾,给 str1
cout << str1 << endl;
str1 = str2.substr(2, 3); //提取子串,提取出 str2 的 下标为2 开始,提取3个字符,给 str1
cout << str1 << endl;
}
int main()
{
strCopy();
system("pause");
return 0;
} 1】 2】 |
| 成员操作函数: |
| |
| 查找字符串: 1】 2】 3】 4】 |
20200425 | string迭代器: #include <iostream>
#include <string>
using namespace std;
//string 迭代器
void funIterator()
{
string str1("abcdefg");
//定义一个string迭代器
string::iterator ite; //ite是一个指向string类的迭代器, 本质是一个char *指针
ite = str1.begin(); //ite指向str1字符串首地址
//ite = str1.end(); //ite指向str1最后一个字符的下一个位置
//使用ite迭代器遍历str1
for (size_t i = 0; i < str1.size(); i++) //str1.size()返回无符号整型size_t
{
cout << *ite << ' ';//string迭代器使用方法类似于指针
ite++;
}
//下标运算
ite = str1.begin(); //ite重新指向str1字符串首地址
for (size_t i = 0; i < str1.size(); i++) //str1.size()返回无符号整型size_t
{
cout << ite[i] << ' ';//下标运算
}
//
ite = str1.begin(); //ite重新指向str1字符串首地址
for (ite; ite != str1.end();ite++)
{
cout << *ite << ' ';
}
//迭代器失效
//str1.append(18, 'a'); //给str1末尾添加18个a,这时str1发生改变
//ite[3] = 'w';//无法再继续使用ite,要给ite重新赋值
//操作函数使用迭代器
string str2("abcd");
string str3("efg");
str2.append(str3.begin(),str3.end()-1); //在str2的尾部添加str3的第1个到最后一个
cout << str2 << endl;
str2.insert(str2.begin()+2, 3,'m');//在str2的第二个字符开始,插入3个m
}
int main()
{
funIterator();
system("pause");
return 0;
} 1】 2】 3】 4】 5】 6】 |
| |
| string迭代器主要还是用于链接容器和算法,实现泛型编程: 1】 算法头文件: #include <algorithm> 2】遍历 3】排序:
#include <iostream>
#include <string>
#include <algorithm>
#include <functional>
using namespace std;
//迭代器链接容器(string类)和算法
void show(char c) //string类元素是char(int),这里就是char(int)
{
cout << c;
}
void funForeach()
{
string str1("dcagabe");
//string str1("5648123");
for_each(str1.begin(),str1.end(),show);//遍历
sort(str1.begin(), str1.end());//排序,默认从小到大排序
cout << endl;
for_each(str1.begin(), str1.end(), show);//遍历
sort(str1.begin(), str1.end(),greater<char>());//排序,从大到小排序(greater是仿函数,#include <functional>)
}
int main()
{
funForeach();
system("pause");
return 0;
} |