【cpp基础篇-04】STL

STL

容器

string

string、char[]、char*

  1. C风格字符串需要以'\0'结尾,C++风格字符串封装为string类形式

  2. 内存分布

    (1)char s1[] = "hello"

    ​ "hello"被保存两个副本:“hello”字符串保存在常量存储区;s1[]被赋值为“hello”保存在栈上。

    ​ s1指向的内存区域总是可写的。

    (2)char* s2 = "hello"

    ​ "hello"保存在常量存储区,char*指针指向hello存储的地址。

    ​ s2指向的区域有时可写。

  3. 对比

    操作string需要创建和销毁临时变量,时间复杂,但是对类操作安全。

    C风格的字符串操作快速,但是存在内存泄漏等问题。

相关函数

创建

#include <string>
using namespace std;
    
    //通过const char * 初始化
    string s1 = "hello";
    
    //构造函数初始化
    string s2("hello");
    
    //通过拷贝构造函数来初始化对象s3
    string s3 = s2;
    
    //用10个'a'字符来初始化字符串
    string s4(10, 'a');
    

遍历

	//数组遍历
	for (int i = 0; i < str.length(); i++) {
        cout<< str[i] << endl;
    }
    
    //迭代器遍历
    for (string::iterator it = str.begin(); it != str.end(); it++) {
        cout << *it << endl;
    }

string和char*转化

	//1 string转char *
    string str("hello");
    const char *p = str.c_str();

    
    //2 char *转string
    const char *p1 = "12345";
    string str2 = p1;
    
    //3 string拷贝到buf[]中
    char buf[128] = {0};
    //从0开始,拷贝3个字符到buf指向的内存空间
    //如果buf空间容纳不下,会越界
    //拷贝过去时,不会给buf末尾添加\0
	//copy(buf,size,begin)
    str.copy(buf, 3, 0);

拼接

    //直接使用加号运算符拼接
    string s3 = s1 + s2;
    
    //使用成员函数拼接
    string s4 = s1.append(s2);

查找与替换

s.find(stringA, pos)

s.replace(pos,sizeof(stringA),stringB)

示例

//例:求hello出现的次数,以及对应的下标
    
    int count = 0;
    size_t offindex = s1.find("hello",0);
    while (offindex != string::npos) {  //如果 offindex != -1
        //找到了
        cout << "索引:" << offindex <<endl;
        count++;
        offindex++;
        offindex = s1.find("hello", offindex);
    }
    
//例:把hello替换成welcome
    size_t offindex1 = s1.find("hello", 0);
    while (offindex1 != string::npos) {
        
        //从offindex1的位置开始删除5个位置,并插入新的字符串welcome
        s1.replace(offindex1, strlen("hello"), "welcome");
        
        //从后面的位置开始
        offindex1 += strlen("welcome");
        
        //继续查找
        offindex1 = s1.find("hello", offindex1);
    }

插入 删除

insert

erase

大小写转化

transform(s1.begin(), s1.end(), s1.begin(), (int (*)(int))toupper);

transform(s1.begin(), s1.end(), s1.begin(), (int (*)(int))tolow
er);

vector

相关函数

初始化

//一维
vector<int> v1(size, nums);
vector<int> v2(v1);
vector<int> v2(v1.begin(), v1.end());

//二维
vector<vector<int>> v1(M);
vector<vector<int>> v1(M, vector<int>(N));

遍历

//正向
for(vector<int>::iterator it = v1.begin(); it != v1.end(); it++){
	cout<<*it<<endl;
}
//反向
for(vector<int>::reverse_iterator it = v1.rbegin(); it != v1.rend(); it++){

}

插入 删除

//insert(pos, num)
v1.insert(v1.begin()+i, x); //{0,1,...,i-1,x,i,...}

//erase(pos)
v1.erase(v1.begin()+i); //迭代删除时会指向下一个元素

示例
for(vector<int>::iterator it = c.begin(); it != c.end();){
	if(*it)
  		it = c.erase(it);
 	else
  		++it; 
}

list

链表结构,只能it++顺序访问,不能随机访问

相关函数

push_back() 
push_front()
erase()
remove()

deque

双端队列

相关函数

push_back();
pop_back();
push_front();
pop_front();
distance(it1, it2);

适配器

stack

queue