再读C++ Primer 写了个小例子——实现Vector类的自动添加元素功能
#pragma
once
#define MAXSIZE 100
#include " string.h "
class Vector
... {
public:
int numberOfElements, maxSize;
string elements[MAXSIZE];
public:
Vector()
...{
numberOfElements = 0;
maxSize = 100;
}
void AddElement(String element)
...{
if (numberOfElements == maxSize)
cout<<"Vector is full, cannot add!"<<endl;
else
...{
elements[numberOfElements] = element;
numberOfElements++;
}
}
} ;
#pragma once
class String
... {
public:
String()
...{
memset(str,0,sizeof(str));
}
String(char* str1)
...{
memcpy(str,str1,sizeof(str1));
}
public:
char str[20];
} ;
// class08012101.cpp : Defines the entry point for the console application.
//
#include " stdafx.h "
// #include "VectorT.h"
#include " string.h "
#include " Vector.h "
using namespace std;
template < class T, class P >
T Search( const T * pTemp, const P & temp)
... {
while(*pTemp != NULL)
...{
if(*pTemp == temp)
...{
return *pTemp;
}
pTemp++;
}
}
// template <class T>
// string Search(const T* data,string temp)
// {
// while(data != NULL)
// {
// if(*data == temp)
// {
// return *data;
// }
// data++;
// }
// }
template < class T >
String Search( const T * data,String temp)
... {
while(data != NULL)
...{
if(*data-> numberOfElements== temp)
...{
return *data;
}
data++;
}
}
String Search(Vector * data,String temp)
... {
for(int i= 0;i<data->maxSize;i++)
...{
if(data->elements[i] == temp)
...{
return data->elements[i];
}
}
return "meizhaodap";
}
int _tmain( int argc, _TCHAR * argv[])
... {
/**//* VectorT<string> vec;
string a="123";
string b = "456";
vec.AddElement(a);
vec.AddElement(b);
string c = Search(vec,b);
*/
// VectorT<String> vec1;
Vector vec1;
String a("1234");
String b("5678");
vec1.AddElement(a);
vec1.AddElement(b);
// Search<VectorT<String> >(vec1,b);
Search(vec1,a);
int d;
cin>>d;
return 0;
}
#define MAXSIZE 100
#include " string.h "
class Vector
... {
public:
int numberOfElements, maxSize;
string elements[MAXSIZE];
public:
Vector()
...{
numberOfElements = 0;
maxSize = 100;
}
void AddElement(String element)
...{
if (numberOfElements == maxSize)
cout<<"Vector is full, cannot add!"<<endl;
else
...{
elements[numberOfElements] = element;
numberOfElements++;
}
}
} ;
#pragma once
class String
... {
public:
String()
...{
memset(str,0,sizeof(str));
}
String(char* str1)
...{
memcpy(str,str1,sizeof(str1));
}
public:
char str[20];
} ;
// class08012101.cpp : Defines the entry point for the console application.
//
#include " stdafx.h "
// #include "VectorT.h"
#include " string.h "
#include " Vector.h "
using namespace std;
template < class T, class P >
T Search( const T * pTemp, const P & temp)
... {
while(*pTemp != NULL)
...{
if(*pTemp == temp)
...{
return *pTemp;
}
pTemp++;
}
}
// template <class T>
// string Search(const T* data,string temp)
// {
// while(data != NULL)
// {
// if(*data == temp)
// {
// return *data;
// }
// data++;
// }
// }
template < class T >
String Search( const T * data,String temp)
... {
while(data != NULL)
...{
if(*data-> numberOfElements== temp)
...{
return *data;
}
data++;
}
}
String Search(Vector * data,String temp)
... {
for(int i= 0;i<data->maxSize;i++)
...{
if(data->elements[i] == temp)
...{
return data->elements[i];
}
}
return "meizhaodap";
}
int _tmain( int argc, _TCHAR * argv[])
... {
/**//* VectorT<string> vec;
string a="123";
string b = "456";
vec.AddElement(a);
vec.AddElement(b);
string c = Search(vec,b);
*/
// VectorT<String> vec1;
Vector vec1;
String a("1234");
String b("5678");
vec1.AddElement(a);
vec1.AddElement(b);
// Search<VectorT<String> >(vec1,b);
Search(vec1,a);
int d;
cin>>d;
return 0;
}