1. 泛型

泛型在我的理解里,就是可以泛化到多种基本的数据类型,例如整数、浮点数、字符和布尔类型以及自己定义的结构体。而容器就是提供能够填充任意类型的数据的数据结构。例如vector就很类似于python中的list。

#include <iostream>
using namespace std;
template <class T> // 模板 T min(T a[], int n)
{
int i;
T minv = a[0];
for (i = 1; i < n; i++) // n-1次
{
// n为数组长
if (minv > a[i])
{
minv = a[i];
}
} return minv;
} int main()
{
int a[] = {8, 10, 0, 1, 7, 4, 9, 6, 11};
double b[] = {1.2, -3.4, 6.9, 7.2, 8.9}; cout << "a数组的最小值为:" << min(a, 9) << endl;
cout << "b数组的最小值为:" << min(b, 5) << endl; return 0;
}

2. 容器

Vector支持push、pop等操作

#include <iostream>
#include <vector>
using namespace std;
int main()
{
vector <int> v1;
v1.push_back(1);
v1.push_back(2); //迭代器
v1.insert(v1.begin(), 0);//头部插入
v1.insert(v1.end(), 4); //尾部插入
v1.insert(v1.end()-1, 3);//倒数第二位置
v1[4] = 10; //v1[5] = 6;越界错误
for (int i=0; i<v1.size(); i++) {
cout << v1[i] << ' ';
}
cout << endl; v1.pop_back(); //删除尾部 10
v1.erase(v1.begin()); //删除头 0
v1.erase(v1.begin(), v1.end()); //全删
cout << "全删后:"; // v1.clear();
for (int i=0; i<v1.size(); i++) {
cout << v1[i] << ' ';
} vector <string> v;
v.push_back("food");
v.push_back("candy");
v.push_back("apple");
sort(v.begin(), v.end());
vector <string>::iterator it; //迭代器
for (it=v.begin(); it!=v.end(); it++) {
cout << *it << " ";
}
cout << endl; return 0;
}
#include <string.h>
#include <vector>
#include <iostream>
#include <algorithm>
using namespace std; int main()
{
vector<int> obj; //创建一个向量存储容器 int(定义了一系列操作的动态数组 for (int i=0; i<20; i++) // push_back(elem)在数组最后添加数据
{
obj.push_back(i);
cout << obj[i] << ",";
} cout << "\nmax_size:" << obj.max_size() << ",";
cout << "\ncapacity:" << obj.capacity() << ",";
cout << "\nsize:" << obj.size() << ",";
cout << "\nempty:" << obj.empty() << ","; for (int i=0; i<5; i++) //去掉数组最后一个数据
{
obj.pop_back();
}
obj.push_back(10);
obj.push_back(30); reverse(obj.begin(), obj.end()); //从大到小
cout << "\n从大到小 :" << endl;
for (int i=0; i<obj.size(); i++)
{
cout << obj[i] << ",";
}
cout << "\n" << endl; cout << "从小到大:" << endl;
sort(obj.begin(), obj.end()); //从小到大
for (int i=0; i<obj.size(); i++)
{
cout << obj[i] << ",";
} cout << "\n清除容器:" << endl;
obj.clear(); //清除容器中所以数据
for (int i=0; i<obj.size(); i++)
{
cout << obj[i] << endl;
} cout<<"\n"<<endl;
cout << "实际数据个数 :" << endl;
for (int i=0; i<obj.size(); i++) //size()容器中实际数据个数
{
cout << obj[i] << ",";
} //方法一
obj.push_back(112);
obj.push_back(120);
obj.push_back(112);
cout << "直接利用数组:";
for (int i=0; i<obj.size(); i++)
{
cout << obj[i] << " ";
}
cout << ", obj[2]=" << obj[2]; cout<<endl;
cout<<"利用迭代器:" ;
//方法二,使用迭代器将容器中数据输出(就是指针)
vector<int>::iterator it;//声明一个迭代器,来访问vector容器,作用:遍历或者指向vector容器的元素
for( it=obj.begin(); it != obj.end(); it++)
{
cout << *it << " ";
}
return 0;
}

3. 使用模板实现泛型类型的函数和类 

/*  使用模板实现泛型类型的函数和类 */
#include <iostream>
#include <vector>
#include <cstdlib>
#include <string>
#include <stdexcept> using namespace std; template <class T>
class Stack {
private:
vector<T> elems; // 元素 public:
void push(T const&); // 入栈
void pop(); // 出栈
T top() const; // 返回栈顶元素
bool empty() const{ // 如果为空则返回真。
return elems.empty();
}
}; template <class T>
void Stack<T>::push (T const& elem)
{
// 追加传入元素的副本
elems.push_back(elem);
} template <class T>
void Stack<T>::pop ()
{
if (elems.empty()) {
throw out_of_range("Stack<>::pop(): empty stack");
}
// 删除最后一个元素
elems.pop_back();
} template <class T>
T Stack<T>::top () const
{
if (elems.empty()) {
throw out_of_range("Stack<>::top(): empty stack");
}
// 返回最后一个元素的副本
return elems.back();
}
/* 模板函数 */
template <typename T>
inline T const& Max (T const& a, T const& b)
{
return a < b ? b:a;
} int main()
{
int i = 39;
int j = 20;
cout << "Max(i, j): " << Max(i, j) << endl; double f1 = 13.5;
double f2 = 20.7;
cout << "Max(f1, f2): " << Max(f1, f2) << endl; string s1 = "Hello";
string s2 = "World";
cout << "Max(s1, s2): " << Max(s1, s2) << endl; try {
Stack<int> intStack; // int 类型的栈
Stack<string> stringStack; // string 类型的栈 // 操作 int 类型的栈
intStack.push(7);
cout << intStack.top() <<endl; // 操作 string 类型的栈
stringStack.push("hello");
cout << stringStack.top() << std::endl;
stringStack.pop();
stringStack.pop();
}
catch (exception const& ex) {
cerr << "Exception: " << ex.what() <<endl;
return -1;
}
}

4. 其他容器stack, map

#include <iostream>
#include <stack>
#include <string>
#include <utility>
#include <map>
using namespace std; int main()
{
stack <int> s;
s.push(1);
s.push(2);
s.push(3);
s.push(4);
s.push(11); cout << "top of the stack:" << s.top() << endl;
cout << "the number of elements:" << s.size() << endl;
cout << "process of pop:" << endl; while (s.empty() != true) // stack isn't empty
{ cout << "\t\t";
cout << s.top() << endl; //read the top of the stack
s.pop(); // pop, and delete the top
} // key value
map <int, string> StuInfo;
StuInfo.insert(pair <int, string> (1, "Tom"));
StuInfo.insert(pair <int, string> (5, "Jack"));
StuInfo[2] = "Lily";
StuInfo[7] = "Bruce";
map <int, string>::iterator it; // 指针 for (it=StuInfo.begin(); it!=StuInfo.end(); it++)
{
cout << (*it).first << " " << (*it).second << endl;
} return 0;
}

了解C++常用容器,方便刷题。

最新文章

  1. 深入浅出JavaScript之原型链&amp;继承
  2. 【转载】桥接Microsoft Word和浏览器
  3. linux RedHat6.4下nginx安装
  4. Django学习笔记(一)
  5. 【问底】徐汉彬:亿级Web系统搭建——单机到分布式集群
  6. UI设计---&gt;全心全意为人民服务的宗旨----&gt;注重客户体验---&gt;软件持久的生命力
  7. 小程序组件中有bindinput监听报异常
  8. Mysql+jsp连接记录
  9. Tomcat配置及不依赖IDEA部署web应用
  10. 【Linux】【Kernel】一个简单的内核模块例子
  11. python 将汉字转换为拼音
  12. 百度学术导入endnote出现choose an import filter解决
  13. 单行显示三级分销记录(同表自join)
  14. Linux 小知识翻译 - 「Linux之父 Linus」
  15. C#基础 常用语&amp;数据类型定义&amp;类型转换
  16. CodeMirror tab转空格
  17. HDU 4310 Hero (贪心)
  18. CodeForces 342C Cupboard and Balloons (几何问题)
  19. Tensorflow中使用tfrecord方式读取数据-深度学习-周振洋
  20. Centos6配置开启FTP Server

热门文章

  1. Node.js躬行记(18)——半吊子的可视化搭建系统
  2. 基础学习:社会工程学---利用Kali下的setoolkit进行钓鱼网站制作
  3. VUE3 之 自定义指令的实现 - 这个系列的教程通俗易懂,适合新手
  4. 3D离线地图开发
  5. java高级用法之:JNA中的回调
  6. PCIe引脚PRSNT与热插拔
  7. windows 存储和切换 ip 配置
  8. coding++: java 操作FastDFS(上传 | 下载 | 删除)
  9. 基于.NetCore开发博客项目 StarBlog - (11) 实现访问统计
  10. MASA Auth - 从用户的角度看整体设计