1.template :函数模板,放在函数定义前面
//
template <typename T> //对类型进行匹配
T max(T a,T b) //函数名和参数前面都有T,对类型进行匹配
{
return a>b?a:b;
}
//比如 int max(int a,int b);
2.template :类模板,定义一个类时放在类的前面,类的成员函数前面
//类模板的使用
template <class T>
class stack{
public:
void push(); //入栈
void pop(); //出栈
};
template <class T> //类的成员函数定义
void stack<T>::push() // 类::成员函数
{
elems.push_back();
}
template <class T>
void stack<T>::pop()
{
elems.pop_back();
}
//派生类(继承)
class temp::public stack{
}
3.#include 输入/输出头文件, iostream 标准库,提供了 cin 和 cout 方法分别用于从标准输入读取流和向标准输出写入流。
4.include vector是向量,也是动态数组,在堆上,比array(数组)更常用。
不需要变长,容量较小用array;需要变长,容量较大用vector。
//
#include <iostream>
#include <vector>
using namespace std;
void main()
{
std::vector<std:string>string1; //创建一个字符串数组
string1.clear(); //清空
string1.push_back("nate"); //尾部加入一个字符串
std::vector<int>myvector;//创建一个数组变量,元素是int类型
myvector.push_back(32); 加入32
std::vector<std::vector<int>>allvector;//创建一个数组变量,元素就是数组
allvector.push_back(myvector);
for(int i =0;i<allvector.size();i++); //遍历
{
for(int j = 0; j<allvector[i].size();j++)
{
std::cout<<allvector[i][j];
}
std::cout<<std::endl; //end line 结束此行
}
system("pause");
}
4.#include"stdafx.h"
Standard Application Frame Extend没有函数库,只是定义了一些环境参数,使得编译出来的程序能在32位的操作系统环境下运行。
5.#include <string.h>
包含一些字符串操作函数的调用。
//strcpy
#include <string.h>
char *strcpy(char *str1,const char*str2);//把字符串str2(包括'\0')拷贝到字符串str1当中,并返回str1