STL的容器包括:string list map set vector
string
c++标准库中string表示可变长的字符串,它在头文件String中。
#include <string>
string s1;//初始化字符串,为空字符串
string s2 = s1;//拷贝初始化,深拷贝字符串
string s3 = "Yasuo";//直接初始化,s3存了字符串
string s4(4, 'a');//s4中存放的字符串是aaaa
string s5("Yasuo");//直接初始化
string s6(s5);//拷贝初始化
string s7 = string(6,"c");//拷贝初始化
对string的操作
os<<s;//将s写到输出流中,返回os
is>>s;//从is中读取字符串s,字符串以空白分隔,返回is
getline(s1, s);//从s1中读入一行赋给s,返回s1;
s.empty();//s为空则返回true,s不为空则返回false;
s.size();//返回s中字符的个数
s[n];//返回s中第n个字符的引用,位置n从0记起;
s1+s2;//返回s1和s2连接后的结果
s1=s2;//用s2的副本代替原来s1中原来的字符
s1==s2;//如果s1和s2中所含的字符完全一样,则他们相等,string对象的相等性对大小写敏s1!=s2;//感
<,<=,>=,>//对字符的字典序进行比较,且对大小写敏感
测试
1 #include<string>
2 #include<stdio.h>
3 #include<string>
4 #include<iostream>
5
6 using namespace std;
7
8 int main(){
9
10 string s1;//初始化字符串,为空字符串
11 string s2 = s1;//拷贝初始化,深拷贝字符串
12 string s3 = "Yasuo";//直接初始化,s3存了字符串
13 string s4(4, 'a');//s4中存放的字符串是aaaa
14 string s5("Yasuo");//直接初始化
15 string s6(s5);//拷贝初始化
16 string s7 = string(6,'c');//拷贝初始化
17
18 cout<<s1<<endl;
19 cout<<s2<<endl;
20 cout<<s3<<endl;
21 cout<<s4<<endl;
22 cout<<s5<<endl;
23 cout<<s6<<endl;
24 cout<<s7<<endl;
25
26 string s8 = s3 + s6;
27 cout<<s8.size()<<endl;
28 cout<< (s3.empty() ? "This is empty " : "This string is not empty ")<<endl;
29 cout<<s3[2]<<endl;
30 cout<< (s2 == s3 ? "s2 is same as s3" : "s2 is not same as s3")<<endl;
31 cout<< (s2 >= s3 ? "s2 is bigger than s3" : "s2 is smaller than s3")<<endl;
32
33 return 0;
34 }
string io的注意事项
使用cin来读取字符串的时候,遇到空白就停止读取。“ hello world”只能读取到hello
所以要想读取hello world则可以通过
string s1 = "hello";
string s2 = "world";
cin>>s1>>" ">>s2;
或者通过
string str;
getline(cin,str);
cout<<str<<endl;
测试代码如下
1 #include<iostream>
2
3 using namespace std;
4
5 int main(){
6
7 string s1="hello",s2="world";
8 string s3;//用来接收“hello world”
9 string s4 = s1 + s2;
10 string s5;
11 cout<<"输入hello world"<<endl;
12 getline(cin,s5);
13 string s6;
14 cout<<"再次输入hello world"<<endl;
15 cin>>s6;
16
17 cout<<s1<<endl;
18 cout<<s2<<endl;
19 cout<<s3<<endl;
20 cout<<s4<<endl;
21 cout<<s5<<endl;
22 cout<<s6<<endl;
23
24 return 0;
25 }
结果如下:
处理string中的字符
访问string中的每个字符的两种方式
1、用string内部的方法
2、使用迭代器
1 #include<iostream>
2
3 using namespace std;
4
5 int main(){
6
7 string str = "Hello world";
8 for(int i = 0; i < str.size(); ++i){
9 cout<<str[i]<<" ";
10 }
11 cout<<endl;
12 for(string::iterator it = str.begin(); it != str.end(); ++it){
13 cout<< *it<<" ";
14 }
15
16 return 0;
17 }
isalnum(c);//当c是字母或数字时为真
isalpha(c);//当c是字母时为真
iscntrl(c);//当c是控制字符时为真
isdigit(c);//当c是数字时为真
isgraph(c);//当c不是空格但可以打印时为真
islower(c);//当c是小写字母时为真
isprint(c);//当c是可打印字符时为真(即c是空格或c具有可视形式)
ispunct(c);//当c是标点符号时为真(即c不是控制字符、数字、字母、可打印空白中的一种)
isspace(c);//当c是空白时为真(即c是空格、横向制表符、纵向制表符、回车符、换行符、进纸符中的一种)(回车是去到下一行的行首,换行符是换到下一行,而不一定是行首)
isupper(c);//当c是大写字母时为真
isxdigit(c);//当c是十六进制数字时为真
tolower(c);//若果c是大写字母,输出对应的小写字母,否则原样输出c
toupper(c);//如果c是小写字母,输出对应的大写字母,否则原样输出c
string中找查找子串的函数string.find();
string str = “hello world”;
cout << str.find("e",0) << endl;
if(str.find("1",0) == string::npos){
cout<< "找不到该子串!" << endl;
}
vector
vector是初始化不必限制大小的数组
要想使用vector就必须包含vector的头文件
#include <vector>
vector初始化的方法
vector<T> v1;//v1是一个空vector,它潜在的元素是T类型的,执 行默认初始化
vector<T> v2(v1);//v2中包含有v1所有元素的副本
vector<T> v2 = v1;//等价于v2(v1),v2中包含有v1所有元素的副本
vector<T> v3(n,val);//v3包含了n个重复的元素,每个元素的值都是val
vector<T> v5(a,b,c……);//v5包含了初始值个数的元素,每个元素被赋予相应的初始值
vector<T> v6 = {a,b,c……};//等价于v5
如果vector中元素的类型是int,默认初始化为0;
如果vector中元素的类型是string,默认初始化时空字符串
vector<int> v1;
vector<string> v2;
vector<vector<int> >;//注意空格,这里相当于二维数组int a[n][n]
vector<int> v5 = {1,2,3,4,5};//列表初始化,使用的是花括号
vector<string> v6 = {"hi","my","name","is","lee"};
vector<int> v7(5,-1);//5是初始化的个数,-1为初始化的值
vector<string> v8(3,"hi");
vector<int> v9(10);//默认初始化为0;
vector<int> v10(4);//默认初始化为空字符串
向vector中添加元素,使用push_back()方法,将元素添加到尾部
for(int i = 0; i < 20; ++i){
v.push_back(sth);
}
vector中的其他方法
v.empty();//若果vector是空的,则返回true,否则返回false;
v.size();//返回v中元素的个数
v.push_back(sth);//想v的尾端添加一个值为sth的元素
v[n];//返回v中第n个位置上元素的引用
v1 = v2;//用v2中元素拷贝替换v1中的元素
v1 = {a,b,c……};//用列表中元素的拷贝替换v1中的元素
v1 == v2;//v1和v2相等当且仅当他们的元素数量相同且对应位置的元素值都相同
v1 != v2;//v1和v2不相等
<,<=,>,=>//用字典序进行比较
访问和操作vector中的每个元素
第一种:
for(int i= 0; i < v.size(); ++i){
cout<< v[i] <<endl;
}
第二种
for(vector<int>::iterator iter = v.begin(); iter != v.end(); iter++){
cout << *iter <<endl;
}
迭代器逆序访问:
for( vector::reverse_iterator iter = v.rbegin(); iter != v6.rend(); iter++){
cout << *iter <<endl;
}
只能对已存在的元素进行复制或者修改操作,如果是加入新元素,务必使用push_back。push_back的作用有两个:1、告诉编译器为新元素开辟空间。2、将新元素存入新空间里。
vector vec;
vec[0] = 1;//这是个错误的例子
vector最常用的增删操作
push_back();//把传送的参数添加到vector的最后
pop_back();//删除vector最后的元素
erase();//删除一个或多个元素
clear();//删除所有元素
insert();//插入一个或多个对象,使用vector容器时,最好避免这个操作
set
set和vector基本上相同,但与vector不同的是set中的元素是有序且唯一的。
只要向set中添加一个元素,就会为它自动排序。
使用set容器,需要添加头文件
#include <set>
#include<stdio.h>
#include<iostream>
#include<set>
#include<string>
using namespace std;
template<typename T>
void showset(set<T> t){
for(typename std::set<T>::iterator it=t.begin();it!=t.end();++it){
cout<< *it;
}
cout<<endl;
}
int main(){
set<int> s1={9,2,2,3,3,4,5,6,7,8};
showset(s1);
set<string> s2={"hello","world","hello world"};
showset(s2);
s1.insert(2);
showset(s1);
s1.insert(1);
showset(s1);
s2.insert("aaa");
showset(s2);
return 0;
}
list
list其实就是一个链表,c语言中的链表都是自己去实现,但是c++就比较好了。
list对应的是一个双向量表,而单向链表对应的是forward_list。
要想使用list,先加个头文件吧。
#include<list>
#include<stdio.h>
#include<iostream>
#include<list>
#include<string>
using namespace std;
template <typename T>
void showlist(list<T> t){
for(typename std::list<T>::iterator it=t.begin();it!=t.end();++it){
cout<<*it;
}
cout<<endl;
}
int main(){
list<int> t1={7,8,5,5,1,9,3,2,1};
list<string> t2={"hello","world","hello world"};
showlist(t1);
showlist(t2);
return 0;
}
list不能调用algorithm下的sort函数进行排序,因为sort函数要求容器可以随机存储,但是list做不到。
所以list容器下有一个自己的sort函数。
list L={7,8,5,5,1,9,3,2,1}
list.sort();
#include<list>
#include<iostream>
using namespace std;
void showlist(list<int> L){
for(list<int>::iterator it=L.begin();it!=L.end();it++){
cout<<*it;
}
}
int main(){
list<int> L={7,8,5,5,1,9,3,2,1};
L.sort();
showlist(L);
return 0;
}
map
map运用的是哈希地址映射的思想,也就是key-value的思想。
使用map容器的话,需要添加map头文件
#include<map>
测试代码:
#include<iostream>
#include<map>
#include<string>
using namespace std;
void showmap(map<string,int> m){
for(map<string,int>::iterator it=m.begin();it!=m.end();++it){
cout<<it->first<<" "<<it->second<<endl;
}
cout<<endl;
}
int main(){
map<string,int> m;
m["zhao"]=1;
m["tian"]=2;
m["sun"]=3;
string s("li");
m[s]=4;
showmap(m);
cout<<m["tian"]<<endl;
cout<<m["li"]<<endl;
m.erase("li");
showmap(m);
m.insert(pair<string,int>("yang",100));
showmap(m);
m.clear();
return 0;
}
如果想查看某个key是否存在,可以通过如下方式:
if(m.count("handsome")){
cout<<"yes, it is"<<endl;
}
else{
cout<<"no, there isn't"<<endl;
}