数据结构:字符串、栈、队列、数组、链表、二叉树
c++中STL常用的数据结构:
string、stack、queue、deque、vector、list、map、iterators
数据结构:(1)字符串
目录
参考课程:http://www.runoob.com/cplusplus/cpp-strings.html
1C风格字符串
C风格的字符串起源于 C 语言,并在 C++ 中继续得到支持。
字符串实际上是使用 null 字符 ‘\0’ 终止的一维字符数组。
因此,一个以 null 结尾的字符串,包含了组成字符串的字符。
下面的声明和初始化创建了一个 “Hello” 字符串。
由于在数组的末尾存储了空字符,所以字符数组的大小比单词 “Hello” 的字符数多一个。
char greeting[6] = {'H', 'e', 'l', 'l', 'o', '\0'};
//依据数组初始化规则,您可以把上面的语句写成以下语句:
char greeting[] = "Hello";
以下是 C/C++ 中定义的字符串的内存表示:
其实,您不需要把 null 字符放在字符串常量的末尾。
C++ 编译器会在初始化数组时,自动把 ‘\0’ 放在字符串的末尾。
让我们尝试输出上面的字符串:
#include <iostream>
using namespace std;
int main (){
char s1[6] = {'H', 'e', 'l', 'l', 'o', '\0'};
char s2[6] = {'H', 'e', 'l', 'l', 'o'};
char s3[] = "hello";
cout << s1 << endl;
cout << s2 << endl;
cout << s3 << endl;
}
Hello
Hello
hello
2C风格字符串 处理函数
C++ 中有大量的函数用来操作以 null 结尾的字符串
下面的实例使用了上述的一些函数:
#include <iostream>
#include <cstring>
using namespace std;
int main ()
{
char str1[11] = "Hello";
char str2[11] = "World";
char str3[11];
int len ;
// 复制 str1 到 str3
strcpy( str3, str1);
cout << "strcpy( str3, str1) : " << str3 << endl;
// 比较 str1 和 str2的大小
cout<< strcmp(str1, str2) <<endl;
if(strcmp(str1, str2)>0)
cout<<"str1 > str2"<<endl;
else if(strcmp(str1, str2)<0)
cout<<"str1 < str2"<<endl;
else
cout<<"str1 = str2"<<endl;
// 连接 str2 到 str1
strcat( str1, str2);
cout << "strcat( str1, str2): " << str1 << endl;
// 连接后,str1 的总长度
len = strlen(str1); //strlen计算字符串长度的时候,没有计算尾部 '\0'元素
cout << "strlen(str1) : " << len << endl;
// 返回一个指针,指向字符串 str1 中字符 ch 的第一次出现的位置
char *p;
char ch = 'e';
p = strchr(str1, ch);
cout<<*(p+3)<<endl;
// 返回一个指针,指向字符串 s1 中字符串 s2 的第一次出现的位置
char *p2;
char s2[] = "ll";
p2 = strstr(str1, s2);
cout<<*(p2+2)<<endl;
}
3Cpp中的String类
C++ 标准库提供了 string 类类型,支持上述所有的操作,另外还增加了其他更多的功能。我们将学习 C++ 标准库中的这个类,现在让我们先来看看下面这个实例:
#include <iostream>
#include <string>
using namespace std;
int main ()
{
string str1 = "Hello";
string str2 = "World";
string str3;
int len ;
// 复制 str1 到 str3
str3 = str1;
cout << "str3 : " << str3 << endl;
// 连接 str1 和 str2
str3 = str1 + str2;
cout << "str1 + str2 : " << str3 << endl;
// 连接后,str3 的总长度
len = str3.size();
cout << "str3.size() : " << len << endl;
}
str3 : Hello
str1 + str2 : HelloWorld
str3.size() : 10
4string类的基本操作
//string类提供了一系列针对字符串的操作,比如:
string str="Malele is a student."
1. append() -- 在字符串的末尾添加字符
2. find() -- 在字符串中查找字符串
4. insert() -- 插入字符
5. length() -- 返回字符串的长度
6. replace() -- 替换字符串
7. substr() -- 返回某个子字符串
8. ...
#include <iostream>
#include <string>
using namespace std;
int main()
{
//定义一个string类对象
string str = "Malele is a student.";
cout<<str<<endl;
// 1、字符串的长度
cout<<str.size()<<endl;
cout<<str.length()<<endl;
// 2、连接字符串
cout<<str+" Haha."<<endl;
cout<<str<<endl;
// 3、复制字符串
string str2;
str2=str;
cout<<str2<<endl;
// 4、在字符串的末尾添加字符
str.append("malele");
cout<<str<<endl;
// 5、查找 "malele”在str中的位置
int pos=str.find("malele");
cout<<pos<<endl;
// 6、从位置pos开始,之后的4个字符替换为空(即删除)
str.replace(pos,4," ");
cout<<str<<endl;
// 7、从头、尾分别寻找字符 " " 的位置
int first = str.find_first_of(" "); //从头开始寻找字符' '的位置
int last = str.find_last_of(" "); //从尾开始寻找字符' '的位置
cout<<first<<" "<<last<<endl;
// 8、提取子串,第一个参数起始位置,第二个参数元素个数
cout<< str.substr(first+1, last-first-1) <<endl;
str = str.substr(0,last);
cout<<str<<endl;
}
5string类的操作汇总
字符串操作函数
这里是C++字符串的重点,把各种操作函数罗列出来
a) =,assign() //赋以新值
b) swap() //交换两个字符串的内容
c) +=,append(),push_back() //在尾部添加字符
d) insert() //插入字符
e) erase() //删除字符
f) clear() //删除全部字符
g) replace() //替换字符
h) + //串联字符串
i) ==,!=,<,<=,>,>=,compare() //比较字符串
j) size(),length() //返回字符数量
k) max_size() //返回字符的可能最大个数
l) empty() //判断字符串是否为空
m) capacity() //返回重新分配之前的字符容量
n) reserve() //保留一定量内存以容纳一定数量的字符
o) [ ], at() //存取单一字符
p) >>,getline() //从stream读取某值
q) << //将谋值写入stream
r) copy() //将某值赋值为一个C_string
s) c_str() //将内容以C_string返回
t) data() //将内容以字符数组形式返回
u) substr() //返回某个子字符串
v)查找函数
w)begin() end() //提供类似STL的迭代器支持
x) rbegin() rend() //逆向迭代器
y) get_allocator() //返回配置器