c++学习:STL库(框架)+字符串模板类string+vector容器+list链表

目录

stl库

常用组件包括

字符串库  字符串模板类string

头文件

最常用的字符串模板类

 字符串类型

模板原型

模板的成员数据类型

模板成员函数

有些函数会有重载,可以去下面网址查看std::basic_string - cppreference.comhttps://zh.cppreference.com/w/cpp/string/basic_string

定义字符串模板对象

字符串实战  部分成员函数和非成员函数

vector容器   动态数组类模板

头文件

模板原型

模板的成员数据类型和成员函数

定义动态数组类模板对象

容器实战

简单的学生管理系统

list双向链表

头文件

模板原型

模板得成员数据类型和成员函数

实战


stl库

STL是指标准模板库,提供了通用的模板类和函数,可以实现多种流行和常用的算法和数据结构,例如字符串操作,链表,队列等等

常用组件包括

  • 容器(顺序容器,关联容器)
    • 例如数组,链表,栈,二叉树等等
  • 算法 
    • 作用于容器,提供操作方法,初始化,排序,搜索,转换等等
  • 迭代器
    • 遍历对象集合的元素,类似指针

字符串库  字符串模板类string

头文件

#include <string>

最常用的字符串模板类

  • std::basic_string
    • 为操作任何字符串类型的字符串设计的模板类

 字符串类型

  • 类型                                          定义
  • std::string                                  std::basic_string<char>
  • std::wstring                                std::basic_string<wchar_t>
  • std::u8string (C++20 起)            std::basic_string<char8_t>
  • std::u16string (C++11 起)          std::basic_string<char16_t>
  • std::u32string (C++11 起)          std::basic_string<char32_t>

模板原型

template<
    class CharT,//字符串类型
    class Traits = std::char_traits<CharT>,//指定字符串类型上操作的特性类
    class Allocator = std::allocator<CharT>//用于分配内存存储的分配器类型
>class basic_string;

模板的成员数据类型

成员类型定义
traits_typeTraits
value_typeCharT
allocator_typeAllocator
size_type
Allocator::size_type
std::allocator_traits<Allocator>::size_type
difference_type
Allocator::difference_type
std::allocator_traits<Allocator>::difference_type
referencevalue_type&
const_referenceconst value_type&
pointer
Allocator::pointer
std::allocator_traits<Allocator>::pointer
const_pointer
Allocator::const_pointer
std::allocator_traits<Allocator>::const_pointer
iterator

指向 value_type 的老式随机访问迭代器 (LegacyRandomAccessIterator) 老式连续迭代器 (LegacyContiguousIterator)

指向 value_type 的老式随机访问迭代器 (LegacyRandomAccessIterator) contiguous_iterator 及常量表达式迭代器 (ConstexprIterator)

const_iterator

指向 const value_type 的老式随机访问迭代器 (LegacyRandomAccessIterator) 老式连续迭代器 (LegacyContiguousIterator)

指向 const value_type 的老式随机访问迭代器 (LegacyRandomAccessIterator) contiguous_iterator 及常量表达式迭代器 (ConstexprIterator)

reverse_iteratorstd::reverse_iterator<iterator>
const_reverse_iteratorstd::reverse_iterator<const_iterator>

模板成员函数

成员函数
(构造函数)    构造 basic_string(公开成员函数)
(析构函数)    销毁字符串,在使用内部存储时解分配它(公开成员函数)
operator=    为字符串赋值(公开成员函数)
assign    赋值字符给字符串(公开成员函数)
assign_range    赋值范围内的字符到字符串(公开成员函数)
get_allocator    返回关联的分配器(公开成员函数)

元素访问
at    访问指定字符,有边界检查(公开成员函数)
operator[]    访问指定字符(公开成员函数)
front    访问首字符(公开成员函数)
back    访问最后的字符(公开成员函数)
data    返回指向字符串首字符的指针(公开成员函数)
c_str    返回字符串的不可修改的 C 字符数组版本(公开成员函数)
operator basic_string_view    返回到整个字符串的不可修改的 basic_string_view(公开成员函数)

迭代器
begin、cbegin    返回指向起始的迭代器(公开成员函数)
end、cend    返回指向末尾的迭代器(公开成员函数)
rbegin、crbegin    返回指向起始的逆向迭代器(公开成员函数)
rend、crend    返回指向末尾的逆向迭代器(公开成员函数)

容量
empty    检查字符串是否为空(公开成员函数)
size、length    返回字符数(公开成员函数)
max_size    返回字符数的最大值(公开成员函数)
reserve    保留存储(公开成员函数)
capacity    返回当前对象分配的存储空间能保存的字符数量(公开成员函数)
shrink_to_fit    通过释放不使用内存减少内存使用(公开成员函数)

操作
clear    清除内容(公开成员函数)
insert    插入字符(公开成员函数)
insert_range    插入范围内的字符(公开成员函数)
erase    移除字符(公开成员函数)
push_back    后附字符到结尾(公开成员函数)
pop_back    移除末尾字符(公开成员函数)
append    后附字符到结尾(公开成员函数)
append_range    后附范围内的字符到结尾(公开成员函数)
operator+=    后附字符到结尾(公开成员函数)
compare    比较二个字符串(公开成员函数)
starts_with    检查字符串是否始于给定前缀(公开成员函数)
ends_with    检查字符串是否终于给定后缀(公开成员函数)
contains    检查字符串是否含有给定的子串或字符(公开成员函数)
replace    替换字符串的指定部分(公开成员函数)
replace_with_range    以范围中的字符替换字符串的指定部分(公开成员函数)
substr    返回子串(公开成员函数)
copy    复制字符(公开成员函数)
resize    更改存储的字符数(公开成员函数)
resize_and_overwrite    更改存储的字符数并可能经由用户提供的操作重写不确定的内容(公开成员函数)
swap    交换内容(公开成员函数)

查找
find    于字符串中寻找字符(公开成员函数)
rfind    寻找子串的最后一次出现(公开成员函数)
find_first_of    寻找字符的首次出现(公开成员函数)
find_first_not_of    寻找字符的首次缺失(公开成员函数)
find_last_of    寻找字符的最后一次出现(公开成员函数)
find_last_not_of    寻找字符的最后一次缺失(公开成员函数)

有些函数会有重载,可以去下面网址查看std::basic_string - cppreference.comicon-default.png?t=N7T8https://zh.cppreference.com/w/cpp/string/basic_string

定义字符串模板对象

#include <string>

using namespace std;

int main()
{
    //实例化一个STL库中的字符串类模板的对象
    std::basic_string<char>  s1;
    basic_string<char>  s2;
    std::string  s3;
    string s4;
}

上面的s1,s2,s3,s4定义其实都是一样的,c++为了方便,将std::basic_string<char>另起名为std::string

typedef std::basic_string<char> std::string;

字符串实战  部分成员函数和非成员函数

#include <iostream>
#include <string>

using namespace std;

int main()
{
    //实例化一个STL库中的字符串类模板的对象
    std::basic_string<char>  s1;
    basic_string<char>  s2;
    std::string  s3;
    string s4;

    //构造
    string s5("hello world");
    //赋值重载=
    s4 = s5;
    //元素访问,返回引用
    cout<<s4.at(1)<<endl;
    s4.at(0) = 'x';
    //赋值重载[]
    s4[2] = 'p';
    //赋值重载<<
    cout<<s4<<endl;

    //data返回的是  string转化为的const char*指针
    //返回指向字符串首字符的指针
    cout<<"s5:"<<s5.data()<<endl;
    //返回字符串的不可修改的 C 字符数组版本
    cout<<"s5:"<<s5.c_str()<<endl;

    //通过迭代器 遍历 容器中的每个元素
    //begin返回字符串首字符的迭代器 数据类型是iterator 
    //end返回字符串最后字符的迭代器 数据类型是iterator 
    //iterator在类模板里  正常写法是std::basic_string<char>::iterator 
    for(string::iterator it=s5.begin();it!=s5.end() ;it++)
    {
        cout<<*it<<endl;
    }

    //容量
    //判断是否为空,返回blue类型
    string s6;
    if(s6.empty())
    {
        cout<<"s6.empty"<<endl;
    }
    //capacity返回当前这个对象里面的指针成员 指向的堆空间,能够容纳存储多少个字符
    cout<<s6.capacity()<<endl;

    //size、length当前这个对象中有效的字符串的长度
    cout<<"size:"<<s6.size()<<endl;
    cout<<"length:"<<s6.length()<<endl;

    //插入操作
    string s77("nihao");
    s77.insert(2,1,'a');
    cout<<s77<<endl;//niahao
    //尾插法
    //nihao---->nihao,zaime
    //push_back只能插一个字符
    string s7("nihao");
    s7.push_back(',');
    s7.push_back('z');
    s7.push_back('a');
    s7.push_back('i');
    s7.push_back('m');
    s7.push_back('e');
    cout<<s7<<endl;

    //尾部删除删除
    s7.pop_back();
    cout<<s7<<endl;

    //append追加
    string s8("hello");
    s8.append(" world");
    cout<<s8<<endl;
    //赋值重载+=
    s8 += "123456";
    cout<<s8<<endl;

    //返回子串
    //要求 将 s8("hello world123456")-->将world返回来
    cout<<s8.substr(6,5)<<endl;

    //查找
    string s9("abc123456efg");
    //想要在上面的字符串中查找 是否存在123456
    int ret = s9.find("www");
    //如果找到了,则返回 子串的起始位置 int ,没有找到返回 -1
    cout<<ret<<endl;


    //以下是类的  非成员函数 ,不能通过对象进行调用
    string s10("hello");
    if(s10 == "hello") //等同于调用operator==(s10,"hello")
    {

    }

    string s11("123456");
    //将string 转换成 int
    int val = std::stoi(s11);

    //将整型 100转换成 string
    int data = 100;
    string s12 = std::to_string(data);
    cout<<s12<<endl;

    return 0;
}

vector容器   动态数组类模板

std::vector是封装动态数组的顺序容器,简单来说就是动态数组类模板

头文件

#include<vector>

模板原型

template<
    class T,//元素的类型
    class Allocator = std::allocator<T>//用于获取/释放内存及构造/析构内存中元素的分配器
> class vector;

模板的成员数据类型和成员函数

std::vector - cppreference.comicon-default.png?t=N7T8https://zh.cppreference.com/w/cpp/container/vector

定义动态数组类模板对象

    std::vector<int> v1(10);

容器实战

#include <iostream>
#include <vector>

using namespace std;

//ostream& operator<<(ostream&out, std::vector<int> &ra)
//{
//    for(int i=0; i<ra.size(); i++)
//    {
//        out<<ra.at(i)<<"\t";
//    }
//}

int main()
{
    //实例化一个STL库中的动态数组类模板的对象
    //注意:如果实例化vector类对象的时候,没有指定元素的个数
    //那么,容器里面默认的大小为0,也就是没有空间
    std::vector<int> v1(10);

    for(int i=0; i<10; i++)
    {
        //v1.at(i) = i+100;
        v1[i] = i+10;
    }

    //编译器 会转换 成 运算符函数 operator<<(cout,v1)需要自己定义
    //cout<<v1<<endl; 
    
    //返回首元素指针
    int *arr = v1.data();

    //通过迭代器遍历容器
    //正向迭代器  需要将运算符函数 operator<<注释掉,要不然测不出来
    std::vector<int>::iterator  it;
    for(it=v1.begin(); it!=v1.end(); it++){
        cout<<*it<<"\t";
    }
    cout<<endl;

    //反向迭代器遍历容器
    std::vector<int>::reverse_iterator rit;
    for(rit = v1.rbegin(); rit!=v1.rend(); rit++)
    {
        cout<<*rit<<"\t";
    }cout<<endl;

    //尾插 并且 会给你进行扩容
    v1.push_back(1000);
    //遍历查看
    for(it=v1.begin(); it!=v1.end(); it++){
        cout<<*it<<"\t";
    }cout<<endl;

    return 0;
}

简单的学生管理系统

#include <iostream>
#include <vector>

using namespace std;

struct Student{
    string name;
    int age;
    float score;
};

int main()
{
    std::vector<struct Student> v;

    struct Student s1 = {"zhang3",22,100};
    v.push_back(s1);

    struct Student s2 = {"zhang4",22,100};
    v.push_back(s2);

    struct Student s3 = {"zhang5",22,100};
    v.push_back(s3);

    for(std::vector<struct Student>::iterator it=v.begin(); it!=v.end(); it++)
    {
        cout<<"name:"<<it->name<<" age:"<<it->age<<" score:"<<it->score<<endl;
    }

    return 0;
}

list双向链表

头文件

 #include<list>

模板原型

template<
    class T,//元素的类型
    class Allocator = std::allocator<T>//用于获取/释放内存及构造/析构内存中元素的分配器
> class list;

模板得成员数据类型和成员函数

std::list - cppreference.comicon-default.png?t=N7T8https://zh.cppreference.com/w/cpp/container/list

实战

#include <iostream>
#include <list>

using namespace std;

class Student
{ 
public:
    Student(string n,int a,float s):name(n),age(a),score(s){}

    void show()
    {
        cout<<name<<"\t"<<age<<"\t"<<score<<endl;
    }
private:
    string name;
    int age;
    float score;
};



int main()
{

    //实例化一个双向链表容器的对象
    std::list<int> list1;

    //尾插
    list1.push_back(10);
    list1.push_back(20);
    list1.push_back(30);
    list1.push_back(40);
    list1.push_back(50);

    // 使用[]+ 下标的方式 访问 容器,一般来说,这个容器的每个元素的内存空间一定是连续的
    // int arr[3];    arr[2]等同于*(arr+2)
    //list1[0] =  1000;  错误 ,链表的内存空间不是连续的,不能使用 [ ]

    //迭代器遍历
    std::list<int>::iterator it;
    for(it=list1.begin(); it!=list1.end(); it++)
    {
        cout<<*it<<"\t";
    }cout<<endl;

    //学生信息
    std::list<Student> list;
    list.push_back(Student("zhang3",22,100));
    list.push_back(Student("zhang4",25,100));
    list.push_back(Student("zhang5",26,100));
    list.push_back(Student("zhang6",27,100));

    std::list<Student>::iterator it;
    for(it=list.begin(); it!=list.end(); it++)
    {
        it->show();
    }

    return 0;
}

  • 20
    点赞
  • 16
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

码农小白

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值