C++ STL 尝试学习与应用(1)

1 篇文章 0 订阅

STL是C++的精华之一,也是各项c++特性的集成者,熟练应用STL是熟悉C++编程的基础,自己在理解STL的基础上在VS下小试了一把:


#include "stdafx.h"
//#include"stdio.h"
#include <stdio.h>
#include <iostream> //#include <iostream.h> different , *.h can not use cout<<string type
#include <windows.h>
#include <map>
#include<string>
#include<vector>

using namespace std;

int main(int argc, char* argv[])

{

   // Example of  map<string, vector<int>>
    string a = "heh";
    map<string, int> mapList;
    vector<int> vecList;
    map<string, vector<int>>mapListVec;

    printf("-------------------map<string, vector<int>>---------------------------\n\n");
    mapListVec.insert(make_pair("001122334455", vector<int>(0)));
    mapListVec.insert(make_pair("001122334466", vector<int>(0)));
    mapListVec.insert(make_pair("001122334477", vector<int>(0)));
    mapListVec.insert(make_pair("001122334488", vector<int>(0)));
    int j = 0;
    for (auto &item : mapListVec)
    {
        j++;
        item.second.push_back( j );
        for (int i = 0; i < 3;i++)
             item.second.push_back( i );    
    }

    for (auto &item : mapListVec)
    {
        printf("item = %s , ", item.first.c_str());
        for (int i = 0; i < item.second.size(); i++)
             printf("%d    ", item.second.at(i));
        printf("\n");
    }
    printf("-------------------map<string, vector<int>>---------------------------\n\n");

    printf("----------------------vector-----------------------\n\n");
    for (int i = 0; i<5; i++)
        vecList.push_back(i*2);//放5个元素进去
    vecList.push_back(3);

    for (auto item : vecList)
    {
        printf("item=%d\n", item);
    }
    printf("-----------------------vector-----------------------\n\n");


    printf("-------------------map<string, int>---------------------------\n\n");
    mapList.insert(make_pair("001122334455", 1));
    mapList.insert(make_pair("001122334456", 2));
    mapList.insert(make_pair("001122334457", 3));
    // travelsal all method 1
    map<string, int>::iterator it;
    for (it = mapList.begin(); it != mapList.end(); it++)
    {         
        printf("it = %s, value= %d\n", it->first.c_str(), it->second);
      //    printf("it = %s, value= %d\n", it->first.c_str, it->second);  fiirt is string type, while printf %s only match char* type
    // we can use string.c_str()to transmit to char * type
        //    cout << it->first << "--->" << it->second << endl;
    }

    // travelsal all method 2
    for (auto &item : mapList)
    {
        printf("itemKey = %s,   value = %d \n", item.first.c_str(), item.second);
    }

    // travelsal all method 3
    for (auto item : mapList)
    {
        printf("itemKey = %s,   value = %d \n", item.first.c_str(), item.second);
    }
    printf("-------------------map<string, int>---------------------------\n\n");

    system("pause");
    return 0;

}

 

 

 

 

 

 

 

 

 

 

 

 

评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值