赞
踩
目录
5. 输入一个字符串,用map统计每个字符出现的次数并输出字符及对应的次数
C++ STL(标准模板库)是一套功能强大的 C++ 模板类,提供了通用的模板类和函数,这些模板类和函数可以实现多种流行和常用的算法和数据结构,如向量、链表、队列、栈。
C++ STL 一共提供了六大组件,包括容器,算法,迭代器,仿函数,配接器和配置器,彼此可以组合套用。
容器通过配置器取得数据存储空间;
算法通过迭代器存取容器内容;
仿函数可以协助算法完成不同的策略变化;
配接器可以应用于容器、仿函数和迭代器。
1. 容器:包括各种数据结构,如vector,queue, deque,,set, map,用来存放数据,分为序列式容器和关联式容器,实现的角度来讲是一种类模板。
2. 算法:各种常用的算法,如sort (插入,快排,堆排序), search (二分查找),从实现的角度来讲是一种方法模板。
3. 迭代器:从实现的角度来看,迭代器是一种将 operator,operator->,operator++,operator-等指针相关操作赋予重载的类模板,所有的 STL 容器都有自己的迭代器。
4. 仿函数:从实现的角度看,仿函数是一种重载了 operator() 的类或者类模板。可以帮助算法实现不同的策略。
5. 配接器:一种用来修饰容器或者仿函数或迭代器接口的东西。
6. 配置器:负责空间配置与管理,从实现的角度讲,配置器是一个实现了动态空间配置、空间管理,空间释放的类模板。
- void transInv(int a[],int b[],int nNum)
- {
- for(int i=0;i<nNum;i++)
- {
- b[i] = -a[i];
- }
- }
- // 求平方
- void transSqr(int a[],int b[],int nNum)
- {
- for(int i=0;i<nNum;i++)
- {
- b[i] = a[i]*a[i];
- }
- }
- // 模板取反
- template <typename T>
- void transInvT(T a[],T b[],int nNum)
- {
- for(int i=0;i<nNum;i++)
- {
- b[i] = -a[i];
- }
- }
- // 模板求平方
- template <typename T>
- void transInvT(T a[],T b[],int nNum)
- {
- for(int i=0;i<nNum;i++)
- {
- b[i] = a[i]*a[i];
- }
- }
- // 模板求立方
- template <typename T>
- void transInvT(T a[],T b[],int nNum)
- {
- for(int i=0;i<nNum;i++)
- {
- b[i] = a[i]*a[i]*a[i];
- }
- }
- // 输出的函数模板
- template <typename T>
- void outputCont(string strNme,ostream& os, T begin, T end)
- {
- os<<strNme<<":";
- for(;begin!=end;begin++)
- {
- os<<*begin<<"\t";
- }
- os<<endl;
- }
-
- // 测试函数
- void Test()
- {
- const int N = 5;
- int a[N] = {1,2,3,4,5};
- outputCont("a",cout,a,a+N);
- int b[N];
- // 输出到vector
- vector<double> vb(N);
- vector<double> vc(N);
- transInv(a,b,N);
- outputCont("Inv a",cout,b,b+N);
- transSqr(a,b,N);
- outputCont("Sqr a",cout,b,b+N);
- transInvT(a,b,N);
- outputCont("Inv a T",cout,b,b+N);
- }

运行结果:
- template<typename T>
- class MyThreshold {
- public:
- //带参构造函数,后面的则是初始化,这样的初始化方式效率比较高
- MyThreshold(int n = 128) : _nThreshold(n)
- {
-
- }
- int operator()(T val)
- {
- return val < _nThreshold ? 0 : 1;
- }
- int _nThreshold;
- };
运行结果:
关于set,必须说明的是set关联式容器。set作为一个容器也是用来存储同一数据类型的数据类型,并且能从一个数据集合中取出数据,在set中每个元素的值都唯一,而且系统能根据元素的值自动进行排序。应该注意的是set中数元素的值不能直接被改变。C++ STL中标准关联容器set, multiset, map, multimap内部采用的就是一种非常高效的平衡检索二叉树:红黑树,也成为RB树(Red-Black Tree)。
- class studentInfo{
- public:
- studentInfo(string strNo,string strName){
- _strNo = strNo;
- _strName = strName;
- }
- string _strNo;
- string _strName;
- friend ostream& operator<<(ostream& os, const studentInfo& info)
- {
- os<<info._strNo<<" "<<info._strName;
- return os;
- }
- friend bool operator<(const studentInfo& info1, const studentInfo& info2){
- return info1._strNo<info2._strNo;
- }
- };
- void TestSet()
- {
- vector<studentInfo> students;
- students.push_back(studentInfo("10021","Zhang san"));
- students.push_back(studentInfo("10002","Li si"));
- students.push_back(studentInfo("10003","Wang wu"));
- students.push_back(studentInfo("10011","Wang Liu"));
- students.push_back(studentInfo("10010","Wu Liu"));
- set<studentInfo> studentSet(students.begin(),students.end());
- outputCont("student set",cout,studentSet.begin(),studentSet.end());
- }
- void TestVector()
- {
- vector<studentInfo> students;
- students.push_back(studentInfo("10021","Zhang san"));
- students.push_back(studentInfo("10002","Li si"));
- students.push_back(studentInfo("10003","Wang wu"));
- students.push_back(studentInfo("10011","Wang Liu"));
- students.push_back(studentInfo("10010","Wu Liu"));
- // 自定义排序
- vector<studentInfo>::iterator it = students.begin();
- advance(it,2);
- sort(it, students.end());
- outputCont("students sorted",cout,students.begin(),students.end());
- // 更改学生姓名
- students[2]._strName = "zy";
- for(vector<studentInfo>::iterator it=students.begin();
- it!=students.end();it++)
- {
- it->_strName = "zy";
- }
- outputCont("students modified",cout,students.begin(),students.end());
- }
- void Test()
- {
- TestVector();
- }

运行结果:
Map是STL的一个关联容器,它提供一对一(其中第一个可以称为关键字,每个关键字只能在map中出现一次,第二个可能称为该关键字的值)的数据 处理能力,由于这个特性,它完成有可能在我们处理一对一数据的时候,在编程上提供快速通道。这里说下map内部数据的组织,map内部自建一颗红黑树(一 种非严格意义上的平衡二叉树),这颗树具有对数据自动排序的功能,所以在map内部所有的数据都是有序的,后边我们会见识到有序的好处。
- #include<iostream>
- #include<string>
- #include<map>
- using namespace std;
-
- int main() {
- string str;
- map<char, int> map_s;
- while (cin >> str) {
- pair<map<char, int>::iterator, bool> ret;
- for (int i = 0; i < str.length(); ++i) {
- ret = map_s.insert(pair<char, int>(str[i], 1));
- if (ret.second == false) {//如果插入失败,则该迭代器的第一个参数的value++
- ret.first->second++;
- }
- }
- map<char, int>::iterator iter = map_s.begin();
- for (; iter != map_s.end(); iter++) {
- cout << iter->first << ' ' << iter->second << endl;
- }
- cout << endl;
-
- }
- }

运行结果:
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。