C++ 模板和泛型编程(掌握Vector等容器的使用)
1. 泛型
泛型在我的理解里,就是可以泛化到多种基本的数据类型,例如整数、浮点数、字符和布尔类型以及自己定义的结构体。而容器就是提供能够填充任意类型的数据的数据结构。例如vector就很类似于python中的list。
#include
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#include << 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; iusing 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 ) { cout ) { 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#include << obj[i] << ","; } cout << "\n" << endl; cout << "从小到大:" << endl; sort(obj.begin(), obj.end()); //从小到大 for (int i=0; i#include 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 ) { cout ) { cout << obj[i] << ","; } cout << "\n清除容器:" << endl; obj.clear(); //清除容器中所以数据 for (int i=0; i ) { cout << obj[i] << endl; } cout<<"\n"<<endl; cout << "实际数据个数 :" << endl; for (int i=0; i //size()容器中实际数据个数 { cout << obj[i] << ","; } //方法一 obj.push_back(112); obj.push_back(120); obj.push_back(112); cout << "直接利用数组:"; for (int i=0; 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
#include
#include
#include <string>
#include
using namespace std;
template <class T>
class Stack {
private:
vector elems; // 元素
public:
void push(T const&); // 入栈
void pop(); // 出栈
T top() const; // 返回栈顶元素
bool empty() const{ // 如果为空则返回真。
return elems.empty();
}
};
template <class T>
void Stack::push (T const& elem)
{
// 追加传入元素的副本
elems.push_back(elem);
}
template <class T>
void Stack::pop ()
{
if (elems.empty()) {
throw out_of_range("Stack<>::pop(): empty stack");
}
// 删除最后一个元素
elems.pop_back();
}
template <class T>
T Stack::top () const
{
if (elems.empty()) {
throw out_of_range("Stack<>::top(): empty stack");
}
// 返回最后一个元素的副本
return elems.back();
}
/* 模板函数 */
template
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
#include
#include <string>
#include
#include
了解C++常用容器,方便刷题。