C++标准模板库基础练习(必会)

#include <iostream>
#include<string>
#include<stdlib.h>
#include<vector> //vector向量
#include<list>  //list链表
#include<map>  //map表示映射
using namespace std;

/***************************************************/
/*通过使用标准模板库,学习其用法*/
/***************************************************/

void funtest1()
{
    vector<int>vec;//定义了vec向量(vector为一个类)
    vec.push_back(3);
    vec.push_back(4);
    vec.push_back(6);


    //vec.pop_back();
    //cout << vec.size() << endl;

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

    //也可以用迭代器遍历向量表
    vector<int>::iterator itor = vec.begin();
    //cout << *itor << endl;
    for (; itor != vec.end(); itor++)
    {
        cout << *itor << endl;
    }

}



//学习关于list的操作
void funtest2()
{
    list<int>list1;
    list1.push_back(2);
    list1.push_back(3);
    list1.push_back(5);

    //这种方法不可以访问
    /*for (int i = 0; i < list1.size(); i++)
    {

        cout << list1[i] << endl;
    }*/


    //那就必须使用迭代器了
    list<int>::iterator itor=list1.begin();
    for (; itor!=list1.end(); itor++)
    {
        cout << *itor << endl;
    }


}



void funtest3()
{
    map<int, string>m;  //成对出现对应关系
    pair<int, string>p1(3, "hello");
    pair<int, string>p2(6, "world!");

    m.insert(p1);
    m.insert(p2);
    cout << m[3] << endl;
    cout << m[6] << endl;

    map<int, string>::iterator itor = m.begin();
    for (; itor != m.end(); itor++)
    {
        cout << itor->first << endl;
        cout << itor->second << endl;
        cout << endl;
    }

}


void funtest4()
{
    map<string, string>m;  //成对出现对应关系
    pair<string, string>p1("a", "hello");
    pair<string, string>p2("b", "world!");

    m.insert(p1);
    m.insert(p2);
    cout << m["a"] << endl;
    cout << m["b"] << endl;

    map<string, string>::iterator itor = m.begin();
    for (; itor != m.end(); itor++)
    {
        cout << itor->first << endl;
        cout << itor->second << endl;
        cout << endl;
    }

}
int main()
{

    funtest4();
    system("pause");
    return 0;
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值