2024 4 24 作业

1. Application of the Vector class

  1. Define a vector container that stores integers, named iv1, and insert 10 random integers;
  2. Insert an integer at the head and tail of iv1, respectively;
  3. Use an iterator to traverse iv1 and output all element values;
  4. Sort the elements of the iv1 from largest to smallest using the 'generic algorithm sort', and output the sorted results;
  5. Delete the last element in iv1;
  6. Using the 'find algorithm' to find if a certain integer exists in iv1。

问题1:

应该怎么在vector container 里随机设置十个随机数

解决:

可以使用std::strand(std::time(0))

`std::srand(std::time(nullptr))` 是用于设置随机数生成器的种子(seed)。让我来解释它的含义:

1. `std::time(nullptr)`:这是一个获取当前时间的函数。`std::time()` 函数返回自 1970 年 1 月 1 日以来经过的秒数。当传入 `nullptr` 作为参数时,它返回的是一个指向 `time_t` 类型对象的指针,该对象表示当前时间的秒数。

2. `std::srand()`:这是 C 标准库中用于设置随机数生成器种子的函数。它接受一个整数作为种子,用于初始化随机数生成器的状态。

通过将 `std::time(nullptr)` 的返回值作为 `std::srand()` 的参数,我们可以使用当前时间来作为随机数生成器的种子。这样做的目的是为了使得每次运行程序时,生成的随机数序列都不同,因为种子是随着时间变化的。

设置完种子后应该配合std::rand一起使用

问题2:

怎么在数列中设置一个整数放在一头一尾

运用vector成员函数

Pushback和

Iv1.Insert(iv1.begin(),random)

问题3:

怎么删除在动态数组中的最后一个元素

应用pop_out函数()

问题4:

怎么在动态数组中找到特定的数

for(vector <int>::itrator it =iv1.begin();it !=iv1.end();++it)

知识点:

1.

Push_back 是vector容器的一个成员函数,用于在容器末尾添加一个新的元素

Push_front 则反之

2.

利用pop_back 函数删除数组中最后一个元素

3.利用find函数来寻找数列中是否存在一个相对应的元素

find(iv1.begin(),iv1.end(),target);

问题5:

如何展示动态数组中的所有元素

        for (int num:iv1)

        {

                 cout<<num<<" ";

        }

最终代码:

#include <iostream>

#include <vector>

#include <cstdlib>

#include <ctime>

#include <algorithm>

using namespace std;

int main ()

{

        int random;

        int target;

        vector <int> iv1;

        srand(time(0));

        //随机生成十个数字

        for (int i=0;i<8;i++)

        {

                  random=rand()%10;

                 iv1.push_back(random);

                

        };

        //将随机数字放在最后面和最前面

        random=rand()%10;

        iv1.push_back(random);

       

        random=rand()%10;

        iv1.insert(iv1.begin(),random);

        //输出

        for (vector<int>::iterator it =iv1.begin();it!=iv1.end();it++)

        {

                 cout<<*it<<" ";

        }

        cout<<endl;

       

        //数组排序

        sort(iv1.begin(),iv1.end());

        //输出

        for (vector<int>::iterator it =iv1.begin();it!=iv1.end();it++)

        {

                 cout<<*it<<" ";

        }

        cout<<endl;

        //删除最后一个元素

        iv1.pop_back();

        //查找元素

        cout<< "输入你要查找的值"<<endl;

        cin>>target;

       

    vector<int>::iterator it=find(iv1.begin(),iv1.end(),target);

       

        if(it!=iv1.end())

        {

                 cout<<"找到元素"<<target<<"在位置"<<(it-iv1.begin()+1)<<endl;

        }

        else

          {

        cout<<"未找到"<<target<<endl;

}

    return 0;

          

       

}

  • 4
    点赞
  • 9
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值