基本STL使用

一 、关于vector

在STL中有一个称为vector的数据结构,可以用来代替数组。

定义Book特性

    private:
        vector<string> shelf_books;

Notic :  类中不能使用类似的定义:vector<sttring> shelf_boos( 10 );

定义Book方法

    public:
        void setName(string name)
        {
            shelf_books.push_back(name);
        }

        string getName(unsigned short idx)
        {
            if (idx < shelf_books.size())
                return shelf_books[idx];
            else
                return "NULL";
        }

        unsigned short getSize(void)
        {
            return shelf_books.size();
        }

        vector<string>& getVector()
        {
            return shelf_books;
        }

        void display_all(void)
        {
            unsigned short idx = 0U;
            cout << "There are " << shelf_books.size() << " books:" << endl;
            for(string name : shelf_books)
            {
                cout << name << endl;
            }
        }

测试:

    Book book;

    book.setName("Issta");
    book.setName("Gao");
    book.setName("Hello");
    book.setName("你好");

    book.display_all();

    cout << "--------------------------------" << endl;

    vector<string>& b_v = book.getVector();
    for(string _name : b_v)
    {
        cout << _name << endl;
    }

    cout << "--------------------------------" << endl;

    for(unsigned short idx = 0; idx < book.getSize(); idx++)
    {
        cout << book.getName(idx) << endl;
    }

    cout << "--------------------------------" << endl;

输出:

二、静态vector与iterator

int key = itr->first; // 从迭代器中获得键
int value = itr->second; // 从迭代器中获得值

代码

   vector<string> wap(5);
    wap[0] = "激光大炮";
    wap[1] = "巡航导弹";
    wap[2] = "喀秋莎";
    wap[3] = "导弹";
    wap[4] = "东风快递使命必达";

    for(int i = 0; i < wap.size(); i++)
    {
        cout << wap[i] << endl;
    }

    cout << "Next using iterator..." << endl;

    vector<string>::iterator  wap_itr = wap.begin();
    vector<string>::iterator  wap_itr_end = wap.end();

    for(;  wap_itr != wap_itr_end; ++wap_itr)
    {
        cout  << *wap_itr << endl;
    }


    cout << "--------------------------------" << endl;

测试结果

三、map与iterator

特征

    private:
        map<string, string> name_to_email;

属性

       void add_context(string name, string email)
        {
            name_to_email["issta"] = "issta0309@163.com";
            name_to_email[name] = email;
        }

        void  show_email(void)
        {
            if(!name_to_email.empty())
            {
                cout <<  "there are :" << name_to_email.size() << endl;
                cout << name_to_email["issta"] << endl;
                cout << name_to_email["Lucy"] << endl;
            }

            cout << "Now using iterator..." << endl;
            
            map<string, string>::iterator  map_itr = name_to_email.begin();
            map<string, string>::iterator  map_itr_end = name_to_email.end();

            for(;  map_itr != map_itr_end; ++map_itr)
            {
                cout  << map_itr->first << " --> " << map_itr->second << endl;
            }

            map<string, string>::iterator  find_itr = name_to_email.find("Lucy");
            if(find_itr != map_itr_end)
            {
                cout << "Lucy's email address is : " << find_itr->second << endl;
            }



            name_to_email.clear();

            if(name_to_email.empty())
            {
                cout << "clear successful!" << endl;
            }

测试代码

    book.add_context("Lucy", "glb_moonkey@soho.com");
    book.show_email();

运行结果

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

打赏作者

issta

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

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

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

打赏作者

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

抵扣说明:

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

余额充值