再读C++ Primer 写了个小例子——实现Vector类的自动添加元素功能(08-12-10)

  再读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;
}


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值