• STLCollections


    容器

    string

    概念

    C风格字符串(以'\0'结尾)太过复杂,不适合大型程序开发,所以C++标准库定义了string类,位于头文件

    string和C风格字符串对比:

    • char* 是一个指针,string是一个类
      • string封装了char *,管理这个字符串,是一个char *型的容器
    • string不用考虑内存释放和越界
      • string管理char *分配的内存。每一次string的复制都由string类负责维护,不用担心复制越界和取值越界等

    常用API

    构造函数

    string();			//创建一个空的字符串 例如: string str;      
    string(const string& str);	//使用一个string对象初始化另一个string对象
    string(const char* s);		//使用字符串s初始化
    string(int n, char c);		//使用n个字符c初始化
    

    基本赋值

    string& operator=(const string &str);	//把字符串s赋给当前的字符串
    string& operator=(const char* s);	//char*类型字符串 赋值给当前的字符串
    string& operator=(char c);		//字符赋值给当前的字符串
    string& assign(const char *s);		//把字符串s赋给当前的字符串
    string& assign(const char *s, int n);	//把字符串s的前n个字符赋给当前的字符串
    string& assign(int n, char c);		//用n个字符c赋给当前字符串
    string& assign(const string &str);	//把字符串s赋给当前字符串
    string& assign(const string &str, int subpos, int sublen);	//将s从start开始n个字符赋值给字符串
    

    长度

    size_t length();
    size_t size();
    

    存取字符

    char& operator[](int n);	//通过[]方式取字符
    char& at(int n);		//通过at方法获取字符
    越界时,[]会直接崩溃,at()会抛出异常
    

    拼接

    string& operator+=(const string& str);	//重载+=操作符
    string& operator+=(const char* s);	//重载+=操作符
    string& operator+=(char c);		//重载+=操作符
    string& append(const char *s);		//把字符串s连接到当前字符串结尾
    string& append(const char *s, int n);	//把字符串s的前n个字符连接到当前字符串结尾
    string& append(int n, char c);		//在当前字符串结尾添加n个字符c
    string& append(const string &str);	//同operator+=()
    string& append(const string &str, int subpos, int sublen);//把字符串s中从pos开始的n个字符连接到当前字符串结尾
    

    查找和替换

    int find(const string& str, int pos = 0) const; 	//查找str第一次出现位置,从pos开始查找
    int find(const char* s, int pos = 0) const;  		//查找s第一次出现位置,从pos开始查找
    int find(const char* s, int pos, int n) const;  	//从pos位置查找s的前n个字符第一次位置
    int find(char c, int pos = 0) const;  			//查找字符c第一次出现位置
    int rfind(const string& str, int pos = npos) const;	//查找str最后一次位置,从pos开始查找
    int rfind(const char* s, int pos = npos) const;		//查找s最后一次出现位置,从pos开始查找
    int rfind(const char* s, int pos, int n) const;		//从pos查找s的前n个字符最后一次位置
    int rfind(char c, int pos = 0) const; 			//查找字符c最后一次出现位置
    string& replace(int pos, int len, const string& str); 	//替换从pos开始n个字符为字符串str
    string& replace(int pos, int len, const char* s); 	//替换从pos开始的n个字符为字符串s
    

    比较

    /*
    compare函数在>时返回 1,<时返回 -1,==时返回 0。
    比较区分大小写,比较时参考字典顺序,排越前面的越小。
    */
    int compare(const string &str) const;//与字符串s比较
    int compare(const char *s) const;//与字符串s比较
    

    子串

    string substr(int pos = 0, int len = npos) const;	//返回由pos开始的n个字符组成的字符串
    

    插入和删除

    string& insert(int pos, const char* s);		//插入字符串
    string& insert(int pos, const string& str);	//插入字符串
    string& insert(int pos, int n, char c);		//在指定位置插入n个字符c
    string& erase(int pos = 0, int len = npos);	//删除从Pos开始的n个字符 
    

    string和c-style字符串转换

    //string 转 char*
    string str = "itcast";
    const char* cstr = str.c_str();
    
    //char* 转 string 
    char* s = "itcast";
    string str(s);
    
    • 在c++中存在一个从const char*到string的隐式类型转换,却不存在从一个string对象到C_string的自动类型转换。对于string类型的字符串,可以通过c_str()函数返回string对象对应的C_string.

    • 通常,程序员在整个程序中应坚持使用string类对象,直到必须将内容转化为char*时才将其转换为C_string.

    • 为了修改string字符串的内容,下标操作符[]和at都会返回字符的引用。但当字符串的内存被重新分配之后,可能发生错误.

    [];
    at();
    size();
    length();
    find();
    +=
    append();
    erase();
    push_back();
    pop_back();
    substr();
    

    set

    • set集合中的元素有序唯一(根据特定的规则排序,由内部的比较方法决定)
    • 允许根据值来快速检索元素。(具有恒定的平均时间复杂度)
    • 元素的值即是key,不能在容器中修改(元素始终为const),可以插入和删除。
    • 相较unordered_set容器,访问单个元素更慢,但是允许基于subset元素的顺序直接进行迭代
    • 典型的实现是BST

    set的元素值就是其键值,关系到set元素的排序规则。如果任意改变set元素值,会严重破坏set组织。换句话说,set的iterator是一种const_iterator.

    set拥有和list某些相同的性质。当对容器中的元素进行插入操作或删除操作时,操作之前所有的迭代器,在操作完成之后依然有效,被删除的那个元素的迭代器必然是一个例外。

    Container properties

    Associative

    • Elements in associative containers are referenced by their key and not by their absolute position in the container.
    • 关联式容器通过值来访问,而不是在容器中的绝对位置

    Ordered

    • The elements in the container follow a strict order at all times. All inserted elements are given a position in this order.
    • 容器中的元素遵循特定的排列顺序,所有插入的元素都按此规则指定插入位置

    Set

    • The value of an element is also the key used to identify it.
    • 元素的key和value相同

    Unique keys

    • No two elements in the container can have equivalent keys.
    • 不允许有两个相同的key

    Allocator-aware

    • The container uses an allocator object to dynamically handle its storage needs.
    • 使用分配器动态扩容

    multiset特性及用法和set完全相同,唯一的差别在于它允许键值重复。set和multiset的底层实现是红黑树。红黑树为平衡二叉树的一种。

    • Associative
    • Ordered
    • Set
    • Multiple equivalent keys
    • Allocator-aware

    常用API

    构造函数

    set<T> st;		//set默认构造函数:
    mulitset<T> mst; 	//multiset默认构造函数: 
    set(const set &st);	//拷贝构造函数
    

    赋值

    set& operator=(const set &st);	//重载等号操作符
    void swap(st);			//交换两个集合容器
    

    容量

    size_type size();	//返回容器中元素的数目
    bool empty();		//判断容器是否为空
    

    增删

    pair<iterator, bool> insert(elem);	//在容器中插入元素
    void clear();				//清除所有元素
    iterator erase(pos);                    //删除pos迭代器所指的元素,返回下一个元素的迭代器
    iterator erase(beg, end);        	//删除区间[beg,end)的所有元素 ,返回下一个元素的迭代器
    //对于multiset,会移除所有相等的元素
    size_type erase(elem);	        	//删除容器中值为elem的元素
    

    查找

    iterator find(key);		//查找键key是否存在,若存在,返回该键的元素的迭代器;若不存在,返回set.end()
    size_type count(key);		//查找键key的元素个数
    iterator lower_bound(keyElem);	//返回第一个key>=keyElem元素的迭代器
    iterator upper_bound(keyElem);	//返回第一个key>keyElem元素的迭代器
    pair<iterator, iterator> equal_range(keyElem);//返回容器中key与keyElem相等的上下限的两个迭代器
    

    unordered_set

    • unordered_set 集合中元素无序唯一。根据插入元素的哈希值组织成桶
    • 允许根据值来快速检索元素。(具有恒定的平均时间复杂度)
    • 元素的值即是key,不能在容器中修改,可以插入和删除
    • 相较set容器,访问单个元素更快,但是范围迭代效率更低

    Container properties

    Associative

    • 关联式容器通过值来访问,而不是在容器中的绝对位置

    Unordered

    • 无序容器通过hash表来组织元素,允许通过key来快速访问

    Set

    • 元素的key和value相同

    Unique keys

    • 不允许有两个相同的key

    Allocator-aware

    • 使用分配器动态扩容

    stack

    • LIFO stack
    • stack是一种容器适配器,特性:last in first out
    • 元素的插入和取出在同一端
    • 默认通过对deque的封装来实现

    常用API

    容量

    bool empty() const;		//容器是否为空
    size_type size() const;  	//返回容器中元素的数量
    void swap (stack& x) noexcept;	//Swap contents
    

    增删

    //Inserts a new element at the top of the stack, above its current top element. 
    //The content of this new element is initialized to a copy of val.
    void push (const value_type& val);
    void push (value_type&& val);
    
    void pop();	//Remove top element,This calls the removed element's destructor.会调用该元素的析构
    
    template <class... Args> void emplace (Args&&... args);	//调用移动构造插入元素
    

    查看

    //Returns a reference to the top element in the stack.
    //返回栈顶引用
    reference top();
    const_reference top() const;
    

    queue

    • FIFO queue

    • queue是一种容器适配器,特性:first-in first-out

    • 元素从一端插入,从另一端取出

    • 默认通过对deque的封装来实现

    常用API

    容量

    bool empty() const;		//容器是否为空
    size_type size() const;	        //返回容器中元素的数量
    void swap (queue& x) noexcept;	//Swap contents
    

    增删

    //Inserts a new element at the end of the queue, after its current last element.
    //The content of this new element is initialized to val.
    void push (const value_type& val);
    void push (value_type&& val);
    
    void pop();	//Remove top element,This calls the removed element's destructor.会调用该元素的析构
    
    template <class... Args> void emplace (Args&&... args);	//调用移动构造插入元素
    

    查看

    //Returns a reference to the next element in the queue.
    //返回队头引用
    value_type& front();
    const value_type& front() const;
    
    //Returns a reference to the last element in the queue
    //返回队尾引用
    reference& back();
    const_reference& back() const;
    

    priority_queue

    • priority_queue是一种容器适配器,特性:第一个元素总是容器中最大/小的(根据指定的排序规则)
    • 类似于heap,可以随时插入元素,但只能检索最大元素(位于最顶部)
    • 默认通过对vector的封装来实现

    常用API

    容量

    bool empty() const;		//容器是否为空
    size_type size() const;  	//返回容器中元素的数量
    void swap (queue& x) noexcept;	//Swap contents
    

    增删

    //Inserts a new element in the priority_queue. 
    //The content of this new element is initialized to val.
    void push (const value_type& val);
    void push (value_type&& val);
    
    void pop();	//Remove top element,This calls the removed element's destructor.会调用该元素的析构
    
    template <class... Args> void emplace (Args&&... args);	//调用移动构造插入元素
    

    查看

    //Returns a constant reference to the top element in the priority_queue.
    //返回队头引用
    const_reference top() const;
    
  • 相关阅读:
    [转]字符串相似度算法(编辑距离算法 Levenshtein Distance)
    [转]Earth Mover's Distance (EMD)
    [转]相似度计算常用方法综述
    小和问题和逆序对问题
    递归的理解
    验证方法是否正确——对数器
    CSS的几个核心概念(复盘大纲~)
    CSS3过渡
    CSS经典布局
    CSS3边框和圆角
  • 原文地址:https://www.cnblogs.com/oumae/p/16041502.html
Copyright © 2020-2023  润新知