#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;
}
C++标准模板库基础练习(必会)
最新推荐文章于 2024-06-06 14:45:21 发布