C++复习——template,template类,memcmp
2016-06-14 15:25
288 查看
本文主要验证:C++模板、模板类、内存比较关键字
MapIndex.h文件:
#ifndef TEMPLATE_CLASS_H
#define TEMPLATE_CLASS_H
template <class Key, class Value>
class MapIndex{
private:
Key* keys;
Value* values;
int size = 0;
int inuse = -1;
~MapIndex();
public:
MapIndex(int);
bool add(Key, Value);
Value* get(Key);
};
#endif
test.cpp文件:
#include<iostream>
#include"MapIndex.h"
using namespace std;
template<class T_SW>
void do_sw(T_SW & a, T_SW & b){
T_SW c = a + b;
a = c - a;
b = c - a;
}
void do_sw(char & a, char & b){
cout << "显式具体化" << endl;
}
template <class Key, class Value>
MapIndex<Key, Value>::MapIndex(int _size){
this->keys = (Key*)malloc(sizeof(Key)*_size);
this->values = (Value*)malloc(sizeof(Value)*_size);
this->size = _size;
}
template <class Key, class Value>
MapIndex<Key, Value>::~MapIndex(){
free(this->keys);
free(this->values);
this->size = 0;
}
template <class Key, class Value>
bool MapIndex<Key, Value>::add(Key _key, Value _value){
if ((this->inuse + 1) >= this->size){
this->inuse = this->size - 1;
return false;
}
++(this->inuse);
this->keys[this->inuse] = _key;
this->values[this->inuse] = _value;
return true;
}
template <class Key, class Value>
Value* MapIndex<Key, Value>::get(Key _key){
for (int i = 0; i <= this->inuse; i++){
if (memcmp(&(this->keys[i]), &_key, sizeof(Key)) == 0){
return &this->values[i];
}
}
return NULL;
}
int main(int argc, char *argv[])
{
int i_a = 1;
int i_b = 2;
cout << "i_a is " << i_a << " i_b is " << i_b << endl;
do_sw(i_a, i_b);
cout << "i_a is " << i_a << " i_b is " << i_b << endl;
//------------------------------------------------------
double d_a = 1.1;
double d_b = 2.2;
cout << "d_a is " << d_a << " d_b is " << d_b << endl;
do_sw(d_a, d_b);
cout << "d_a is " << d_a << " d_b is " << d_b << endl;
//------------------------------------------------------
char c_a = 'a';
char c_b = 'b';
cout << "c_a is " << c_a << " c_b is " << c_b << endl;
do_sw(c_a, c_b);
cout << "c_a is " << c_a << " c_b is " << c_b << endl;
//------------------------------------------------------
int* a = new int(5);
a[2] = 5;
int* b = &a[2];
cout << *b << endl;
cout << memcmp(a, b, sizeof(int)) << endl;
//------------------------------------------------------
MapIndex<char, int>* list = new MapIndex<char, int>(2);
list->add('y', 1);
list->add('g', 2);
cout << *list->get('g') << endl;
//------------------------------------------------------
char end;
cin >> end;
}
运行结果:
MapIndex.h文件:
#ifndef TEMPLATE_CLASS_H
#define TEMPLATE_CLASS_H
template <class Key, class Value>
class MapIndex{
private:
Key* keys;
Value* values;
int size = 0;
int inuse = -1;
~MapIndex();
public:
MapIndex(int);
bool add(Key, Value);
Value* get(Key);
};
#endif
test.cpp文件:
#include<iostream>
#include"MapIndex.h"
using namespace std;
template<class T_SW>
void do_sw(T_SW & a, T_SW & b){
T_SW c = a + b;
a = c - a;
b = c - a;
}
void do_sw(char & a, char & b){
cout << "显式具体化" << endl;
}
template <class Key, class Value>
MapIndex<Key, Value>::MapIndex(int _size){
this->keys = (Key*)malloc(sizeof(Key)*_size);
this->values = (Value*)malloc(sizeof(Value)*_size);
this->size = _size;
}
template <class Key, class Value>
MapIndex<Key, Value>::~MapIndex(){
free(this->keys);
free(this->values);
this->size = 0;
}
template <class Key, class Value>
bool MapIndex<Key, Value>::add(Key _key, Value _value){
if ((this->inuse + 1) >= this->size){
this->inuse = this->size - 1;
return false;
}
++(this->inuse);
this->keys[this->inuse] = _key;
this->values[this->inuse] = _value;
return true;
}
template <class Key, class Value>
Value* MapIndex<Key, Value>::get(Key _key){
for (int i = 0; i <= this->inuse; i++){
if (memcmp(&(this->keys[i]), &_key, sizeof(Key)) == 0){
return &this->values[i];
}
}
return NULL;
}
int main(int argc, char *argv[])
{
int i_a = 1;
int i_b = 2;
cout << "i_a is " << i_a << " i_b is " << i_b << endl;
do_sw(i_a, i_b);
cout << "i_a is " << i_a << " i_b is " << i_b << endl;
//------------------------------------------------------
double d_a = 1.1;
double d_b = 2.2;
cout << "d_a is " << d_a << " d_b is " << d_b << endl;
do_sw(d_a, d_b);
cout << "d_a is " << d_a << " d_b is " << d_b << endl;
//------------------------------------------------------
char c_a = 'a';
char c_b = 'b';
cout << "c_a is " << c_a << " c_b is " << c_b << endl;
do_sw(c_a, c_b);
cout << "c_a is " << c_a << " c_b is " << c_b << endl;
//------------------------------------------------------
int* a = new int(5);
a[2] = 5;
int* b = &a[2];
cout << *b << endl;
cout << memcmp(a, b, sizeof(int)) << endl;
//------------------------------------------------------
MapIndex<char, int>* list = new MapIndex<char, int>(2);
list->add('y', 1);
list->add('g', 2);
cout << *list->get('g') << endl;
//------------------------------------------------------
char end;
cin >> end;
}
运行结果:
相关文章推荐
- 如何组织构建多文件 C 语言程序(二)
- 如何写好 C main 函数
- Lua和C语言的交互详解
- 关于C语言中参数的传值问题
- 简要对比C语言中三个用于退出进程的函数
- 深入C++中API的问题详解
- 基于C语言string函数的详解
- C语言中fchdir()函数和rewinddir()函数的使用详解
- C语言内存对齐实例详解
- C语言编程中统计输入的行数以及单词个数的方法
- C语言自动生成enum值和名字映射代码
- C语言练习题:自由落体的小球简单实例
- 使用C语言判断英文字符大小写的方法
- c语言实现的带通配符匹配算法
- C语言实现顺序表基本操作汇总
- C语言中进制知识汇总
- C语言判断一个数是否是2的幂次方或4的幂次方
- C语言中计算正弦的相关函数总结
- 使用C语言详解霍夫曼树数据结构
- C语言实现选择排序、冒泡排序和快速排序的代码示例