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;
}