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

最常用的字符串模板类

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

 字符串类型

  • 类型                                          定义
  • std::string                                  std::basic_string
  • std::wstring                                std::basic_string
  • std::u8string (C++20 起)            std::basic_string
  • std::u16string (C++11 起)          std::basic_string
  • std::u32string (C++11 起)          std::basic_string

模板原型

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

模板的成员数据类型

成员类型 定义
traits_type Traits
value_type CharT
allocator_type Allocator
size_type
Allocator::size_type
std::allocator_traits::size_type
difference_type
Allocator::difference_type
std::allocator_traits::difference_type
reference value_type&
const_reference const value_type&
pointer
Allocator::pointer
std::allocator_traits::pointer
const_pointer
Allocator::const_pointer
std::allocator_traits::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_iterator std::reverse_iterator
const_reverse_iterator std::reverse_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 

using namespace std;

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

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

typedef std::basic_string std::string;

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

#include 
#include 

using namespace std;

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

    //构造
    string s5("hello world");
    //赋值重载=
    s4 = s5;
    //元素访问,返回引用
    cout<::iterator 
    for(string::iterator it=s5.begin();it!=s5.end() ;it++)
    {
        cout<<*it<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<将world返回来
    cout<

vector容器   动态数组类模板

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

头文件

#include

模板原型

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

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

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

定义动态数组类模板对象

    std::vector v1(10);

容器实战

#include 
#include 

using namespace std;

//ostream& operator<<(ostream&out, std::vector &ra)
//{
//    for(int i=0; i v1(10);

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

    //编译器 会转换 成 运算符函数 operator<<(cout,v1)需要自己定义
    //cout<::iterator  it;
    for(it=v1.begin(); it!=v1.end(); it++){
        cout<<*it<<"\t";
    }
    cout<::reverse_iterator rit;
    for(rit = v1.rbegin(); rit!=v1.rend(); rit++)
    {
        cout<<*rit<<"\t";
    }cout<

简单的学生管理系统

#include 
#include 

using namespace std;

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

int main()
{
    std::vector 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::iterator it=v.begin(); it!=v.end(); it++)
    {
        cout<<"name:"<name<<" age:"<age<<" score:"<score<

list双向链表

头文件

 #include

模板原型

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

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

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

实战

#include 
#include 

using namespace std;

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

    void show()
    {
        cout< 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::iterator it;
    for(it=list1.begin(); it!=list1.end(); it++)
    {
        cout<<*it<<"\t";
    }cout< 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::iterator it;
    for(it=list.begin(); it!=list.end(); it++)
    {
        it->show();
    }

    return 0;
}

你可能感兴趣的:(c++,c++,学习,开发语言)