C++ 模板和泛型编程(掌握Vector等容器的使用)
阅读原文时间:2023年07月10日阅读:2

1. 泛型

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

#include
using namespace std;
template // 模板

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
using namespace std;
int main()
{
vector 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
#include
#include
#include
using namespace std;

int main()
{
vector 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
#include
#include
#include
#include

using namespace std;

template
class Stack {
private:
vector elems; // 元素

public:
void push(T const&); // 入栈
void pop(); // 出栈
T top() const; // 返回栈顶元素
bool empty() const{ // 如果为空则返回真。
return elems.empty();
}
};

template
void Stack::push (T const& elem)
{
// 追加传入元素的副本
elems.push_back(elem);
}

template
void Stack::pop ()
{
if (elems.empty()) {
throw out_of_range("Stack<>::pop(): empty stack");
}
// 删除最后一个元素
elems.pop_back();
}

template
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
#include
#include
using namespace std;

int main()
{
stack 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++常用容器,方便刷题。

手机扫一扫

移动阅读更方便

阿里云服务器
腾讯云服务器
七牛云服务器

你可能感兴趣的文章