STL与容器


STL与容器

基本概念

STL(standard template library)标准模板库

容器container

算法algorithm

迭代器iterator

容器:各种数据结构,用于存放数据,如:vector,list,deque,set,map

​ STL容器就是将最常用的数据结构实现出来

​ 最常用的数据结构:数组,链表,树,图,队列,集合

算法:操作容器,如:sort,find,copy,for_each

迭代器:提供一种方法,使之能遍历容器中所有的元素,而又不暴露元素内部的方法

​ 类似于指针

容器算法迭代器初识

vector存放内置数据类型

容器:vector

算法:for_each

迭代器:vector<int,>iterator

void print(int val)
{
    cout << val << endl;
}
void test01()
{
    vector v;
    v.push_back(10);
    v.push_back(20);
    v.push_back(30);
    v.push_back(40);
    vector::iterator begin = v.begin();
    vector::iterator end = v.end();
    /*
    * while (begin != end)
    {
        cout << *begin << endl;
        begin++;
    }
    */
    for_each(begin, end, print);
}

vector存放自定义数据类型

#include
using namespace std;
#include
class person {
public:
    string m_name;
    int m_age;
public:
    person(string name, int age)
    {
        this->m_name = name;
        this->m_age = age;
    }

};
void test01()
{
    vector p;
    person p1("aaa", 0);
    person p2("bbb", 3);
    p.push_back(p1);
    p.push_back(p2);
    for (vector::iterator it = p.begin(); it != p.end(); it++)
    {
        cout << "name:" << (*it).m_name << endl << "age:" << (*it).m_age << endl;
    }
}
void test02()
{
    vector p;  //参数列表中为person*
    person p1("aaa", 0);
    person p2("bbb", 3);
    p.push_back(&p1);
    p.push_back(&p2);
    for (vector::iterator it = p.begin(); it != p.end(); it++)
    {
        person* p = (*it);  //it为一个指向指针的指针,it解引用是一个指针赋给p
        cout << "name:" << p->m_name << endl << "age:" << p->m_age << endl;
    }
}

vector容器嵌套容器

STL常用容器

string类

string类构造
  • string();
  • string(const char*s);
  • string(const string&str);
  • string(int n,char c);
void test01()
{
    //string字符串的4种构造方式
    string str1;//空字符串
    cout << str1 << endl;

    const char* s = "hello";
    string str2 = s;        //字符数组
    cout << str2 << endl;

    const string str = "world";
    string str3 = str;        //同类stirng类对象
    cout << str3 << endl;

    string str4 = string(10, 'a');        //相同数量的字符
    cout << str4 << endl;
}
/*
=>

hello
world
aaaaaaaaaa
*/
string赋值操作

注意一些方法不能用于初始化,但是可以用于赋值

  • string& operator=(const char*s);
  • string& operator=(const string &str);
  • string&operator=(char c);
  • string& assign(const char*s);
  • string&assign(const char*s ,int n);
  • string&assign(const string &s);
  • string&assign(int n,char c);
void test01()
{
    string str1 = "hell";    //用字符串赋值
    string str2 = str1;        //同类对象赋值
    //string str3 = 'o';
    string str3;
    str3 = 'o';                //可以使用字符赋值,但不可用字符初始化
    //string str4.assign("wor"); //同上
    string str4;
    str4.assign("wor");
    string str5;
    str5.assign(str4);
}

assign不是很常用的方法,用作了解

string拼接
void test01()
{
    string str1 = "i ";
    str1 += "love ";
    string str2 = "game";
    str1 += str2;
    str1 += ':';
    cout << str1;
}
=> i love game:
void test02()
{
    string str1;
    str1 = 'i';
    str1.append(" love");
    str1.append(" game abcde", 5);        //从开始读取5个字符
    str1.append("dnf lol wzry", 3, 4);    //从下标3开始读取4个字符
    cout << str1;
}
=> i love game lol
string查找与替换

函数原型:

  • int find(const char *s,int pos=0) const;查找字符串出现的位置,从pos开始查找
  • int rfind(const char*s,int pos=npos) const;同上
  • string& replace(int pos,int n,const char*s);替换从pos开始的n个字符为字符串s
void test01()
{
    string str = "abcdefgde";
    int pos = str.find("de");
    cout << "pos: " << pos << endl;
    pos = str.rfind("de");
    cout << "pos: " << pos << endl;
}
//find查找从左往右,rfind从右往左
//find找到字符串后返回查找的第一个字符位置,找不到返回-1
=>
    pos: 3
    pos: 7
void test02()
{
    string str = "1234567";
    str.replace(2, 3, "11111");
    cout << str;
}
//replace在替换时,要指定从哪个位置起,多少个字符,替换成什么
=> 
    121111167
string字符比较

函数原型:

  • int compare(const char *s) const
void test03()
{
    string str1 = "hello";
    string str2 = "hello ";
    int ret = str1.compare(str2);
    if (ret == 0)
    {
        cout << "str1=str2";
    }
    else
        cout << "str1!=str2";
}
//等于返回0
//大于返回1
//小于返回-1
=> 
    str1!=str2

字符串比较主要用于判断两个字符串是否相等

string字符串存取

函数原型:

  • char& operator[](int n);
  • char& at(int n);
void test04()
{
    string str = "hello world";
    str[3] = 'x';
    for (int i = 0; i < str.size(); i++)
        cout << str[i];
    cout << endl;
    str.at(5) = 'x';
    for (int i = 0; i < str.size(); i++)
        cout << str.at(i);
    cout << endl;
}
=> 
    helxo world
    helxoxworld
string插入和删除

函数原型:

  • string& insert(int pos ,const char*s);
  • string& erase(int pos,int n=npos);
void test05()
{
    string str;
    str = "hello world";
    str.insert(5, "   ");  //插入位置,插入字符
    cout << str << endl;
    str.erase(2, 5);         //从2号位置开始5个字符
    cout << str << endl;
}
//插入和删除的起始下标都是从0开始的
=> 
    hello    world
    he   world
string子串

函数原型:

  • string sbustr(int pos=0,int n=npos) const;
void test06()
{
    string str = "abcd";
    string str1 = str.substr(0, 2);
    cout << str1 << endl;
    string str2 = "228178@qq.com";
    int pos = str2.find("@");
    string str3 = str2.substr(0, pos);
    cout << str3;
}
=>
    ab
    228178

文章作者: 1doctorc1
版权声明: 本博客所有文章除特別声明外,均采用 CC BY 4.0 许可协议。转载请注明来源 1doctorc1 !
  目录