c++作业

#include <iostream>

using namespace std;
template <typename T>
class myvector
{
private:
    T data;
     size_t size;
public:

    T *begin;
    T *end;
    T *last;
public:
    myvector():data(0),size(0),begin(new T [0]),end(begin),last(begin+1){}
    myvector(size_t num,const T &val):data(val),size(num)
    {
        begin=new T [size];
        last=begin+size;
        for(end=begin;end!=last;end++)
        {
            *end=val;
        }
    }
    myvector( const myvector &from ):size(from.size)
    {
        begin=new T [size];
        end=begin;
        last=begin+size;
        for(size_t i=0;i<size;i++)
        {
            *end=from.begin[i];
            end++;
        }

    }
    ~myvector()
    {
        delete [] begin;
    }
    size_t mysize()
    {
        return end-begin;
    }
    size_t capacity()
    {
        return last-begin;
    }
    bool empty()
    {
        return (begin==end)?true:false;
    }
    void push_back( const T &val)
    {
        if(end==last)
        {

            size= last-begin;
            T *temp=new T [(last-begin)*2];
            memcpy(temp,begin,sizeof (T)*(last-begin));
            delete [] begin;
            begin = temp;
            end = begin + size;
            last= begin+2*size;
            size=size*2;
        }


        *end++=val;
    }
    void push_pop( )
    {
        end--;
    }
    void slinsert(int loc,T val)
    {
       if(end==last)
       {
           size=last-begin;
           T *temp=new T [(last-begin)*2];
           memcpy(temp,begin,sizeof (T)*(last-begin));
           delete [] begin;
           begin = temp;
           end = begin + size;
           last=begin+2*size;
           size=size*2;
       }
       end++;
       for(int i=end-begin-1;i>=loc;i--)
       {
           begin[i+1]=begin[i];
       }
       begin[loc]=val;
    }
    T at(int n)
    {
        
   
        
            if(n>end-begin-1)
            {
                throw -1;
            }
       
       return begin[n]; 
    }

T front()
{
    return begin[0];
}
T back()
{
    return begin[end-begin-1];
}


};


int main()
{
    myvector<int>v1;

    if(v1.empty())
        {
            cout<<"empty"<<endl;
        }else
        {
            cout<<"not empty"<<endl;
        }
    for(int i=1; i<=20; i++)
       {
           v1.push_back(i);
           cout<<"v1.size = "<<v1.mysize()<<"   v1.capaticy = "<<v1.capacity()<<endl;
       }
    for(int i=0; i<v1.mysize(); i++)
       {
           cout<<v1.begin[i]<<" ";        //[]不会进行下标越界检查
       }
    cout<<endl;
    v1.slinsert(0, 520);
    for(int i=0; i<v1.mysize(); i++)
       {
           cout<<v1.begin[i]<<" ";        //[]不会进行下标越界检查
       }
       cout<<endl;
   
     try {
            
            cout<<v.at(21)<<endl;
        } catch (int e) {
            if(e==-1)
                cout<<"超出范围"<<endl;
        }

    return 0;
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
根据引用提供的信息,可以得知该算法使用C++实现。以下是该算法的主要步骤和代码框架: 1. 初始化种群 2. 计算适应度函数 3. 选择操作 4. 交叉操作 5. 变异操作 6. 更新种群 7. 判断是否满足终止条件 以下是该算法C++代码框架: ``` // 定义染色体结构体 struct Chromosome { // ... }; // 初始化种群 void initPopulation(vector<Chromosome>& population) { // ... } // 计算适应度函数 void calculateFitness(vector<Chromosome>& population) { // ... } // 选择操作 void selection(vector<Chromosome>& population) { // ... } // 交叉操作 void crossover(vector<Chromosome>& population) { // ... } // 变异操作 void mutation(vector<Chromosome>& population) { // ... } // 更新种群 void updatePopulation(vector<Chromosome>& population) { // ... } // 判断是否满足终止条件 bool isTerminated(int generation) { // ... } // 遗传算法主函数 void geneticAlgorithm() { // 初始化种群 vector<Chromosome> population; initPopulation(population); // 迭代直到满足终止条件 int generation = 0; while (!isTerminated(generation)) { // 计算适应度函数 calculateFitness(population); // 选择操作 selection(population); // 交叉操作 crossover(population); // 变异操作 mutation(population); // 更新种群 updatePopulation(population); // 更新迭代次数 generation++; } } ``` 需要注意的是,以上代码框架只是一个简单的示例,实际的代码实现可能会更加复杂。如果您需要更详细的代码实现,请参考引用中提供的C++代码。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值