C++基础:找出vector容器中最大的两个数
文章目录
1. 前言
👉👉定义一个vector数组,并进行初始化,向其中填充10个不同的随机数(1-100),找出容器中最大的两个数并输出。
2. 动态数组
👉👉vector容器是STL标准模板库中最常用的容器之一,实现的是一个动态数组,即可以进行元素的插入和删除,提供了对数组元素的快速随机访问,以及在数组尾端增加和删除元素的高效实现,在此过程中,vector会动态调整所占用的内存空间。
2.1 构造函数
创建vector容器。
函数 | 描述 |
---|---|
vector<T> v | T 表示数据元素的类型,可以是int 类型,也可以是string 对象 |
vector(v.begin(), v.end()) | 将[v.begin(),v.end()) 区间中的元素拷贝给本身,v.begin() 表示返回指向容器第一个元素,v.end() 表示返回指向容器最后一个元素之后的迭代器 |
vector(n, element) | 构造函数将n 个element 拷贝给本身 |
vector(const vector &vec) | 拷贝构造函数,如vector<int> v3(10, 100); vector<int> v4(v3); |
2.2 赋值操作
给vector容器进行赋值。
函数 | 描述 |
---|---|
vector& operator=(const vector &vec) | 重载等号运算符 |
vec.assign(begin, end) | 将[begin,end) 区间中的数据拷贝赋值给vec |
vec.assign(n, element) | 将n 个element 拷贝赋值给vec |
2.3 插入和删除
对vector容器进行插入、删除操作。
函数 | 描述 |
---|---|
vec.push_back(element) | 在尾部加入一个元素element |
vec.pop_back() | 删除最后一个元素 |
vec.insert(pos,element) | 在pos 位置插入一个element 拷贝 |
vec.erase(pos) | 删除pos位置的数据 |
vec.erase(begin,end) | 删除[begin,end) 区间的数据 |
2.4 容量和大小
对vector容器的容量和大小操作。
函数 | 描述 |
---|---|
vec.resize(num) | 用于设定容器的内存大小 |
vec.reserve(num) | 用于预分配容器所需的内存空间 |
vec.size() | 返回容器的数据个数 |
vec.capacity() | 返回容器的存储容量 |
vec.empty() | 判断容器是否为空 |
2.5 数据存取
对vector中的数据进行存取操作。
函数 | 描述 |
---|---|
vec.front() | 返回容器中第一个数据元素 |
vec.back() | 返回容器中最后一个数据元素,不检查这个数据是否存在 |
vec.at(index) | 返回索引为index 所指的数据,如果index 越界,抛出std::out_of_range 异常 |
operator[ ] | 通过索引访问vector 中的元素,如vector<int> nums = {1, 2, 3, 4, 5}; int element = nums[1]; |
2.6 互换容器
实现两个容器内元素进行互换。
函数 | 描述 |
---|---|
vec1.swap(vec2) | 将vec1 中的元素和vec2 中的元素整体交换 |
2.7 释放内存
清空容器中所有数据,释放内存。
函数 | 描述 |
---|---|
vec.clear() | 清空容器中所有数据,但不释放内存 |
vec.shrink_to_fit() | 减少容量以释放内存 |
std::vector().swap(vec) | 通过赋予一个空的vector 来释放容器占用的内存空间 |
3. 迭代器
👉👉在C++中,
iterator
是一种特殊类型的对象,它能使得开发者用来遍历容器(如vector
、list
、map
等)的元素,又不需暴露该对象的内部细节。
请问:一年中有哪几个大月?
#include <iostream>
#include <algorithm>
#include <vector>
using namespace std;
int main()
{
vector<int> vec = {1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12};
vector<int>::iterator it;
// 1.使用迭代器删除元素
for (it = vec.begin(); it != vec.end();)
{
if (*it % 2 == 0) // 删掉偶数元素
{
it = vec.erase(it);
}
else
{
++it;
}
}
it = vec.erase(vec.begin() + 4, vec.end()); // 删掉元素9,11
// 2.使用迭代器插入元素
it = find(vec.begin(), vec.end(), 7) + 1; // 返回值是目标元素的下标,找不到时返回值为迭代器结尾
vector<int> vec2 = {8, 10, 12}; // 插入元素8,10,12
vec.insert(it, vec2.begin(), vec2.end());
// 3.使用迭代器返回容器的某个元素
it = vec.begin() + 1;
cout << "当前月:" << *it << endl;
// 4.使用迭代器遍历容器
cout << "公历的大月(31天):";
for (it = vec.begin(); it != vec.end(); ++it)
{
cout << *it << ' ';
}
return 0;
}
🆚🆚运行结果:
当前月:3
公历的大月(31天):1 3 5 7 8 10 12
4. 算法
👉👉算法是STL标准模板库中的算法库,提供了大量的算法函数,可用于各种容器(如
vector
、list
、map
等)的操作。这些算法函数可以大大简化程序员的编程工作,同时提高代码的可读性和可维护性。
4.1 排序
sort()
用来对一个序列进行排序。
vector<int> nums = {1, 4, 5, 2, 3};
sort(nums.begin(), nums.end());
4.2 倒序
reverse()
用来反转容器中的元素顺序。
vector<int> nums = {1, 4, 5, 2, 3};
sort(nums.begin(), nums.end());
reverse(nums.begin(), nums.end());
4.3 去重
unique()
用来实现相邻相同元素的去重。
vector<int> nums = {1, 4, 5, 2, 4, 1, 3};
sort(nums.begin(),nums.end());
auto it = unique(nums.begin(), nums.end());
nums.erase(it, nums.end());
4.4 复制
copy()
可以将容器中的元素复制到另一个容器中。
int arr[] = {1, 2, 3, 4, 5};
int n = sizeof(arr) / sizeof(arr[0]);
vector<int> vec(n);
copy(arr, arr + n, vec.begin());
4.5 查找
find()
用来在容器中查找指定的元素,该算法返回一个迭代器,指向第一个匹配的元素,如果没有找到匹配的元素,则返回容器的end()
迭代器。
vector<int> vec = {1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12};
auto it = find(vec.begin(), vec.end(), 7);
4.6 遍历
for_each()
对容器内的每个元素执行一个操作,可以使用函数、函数对象或者Lambda
表达式来进行操作。
#include <iostream>
#include <algorithm>
#include <vector>
using namespace std;
// 一元函数对象
class Print
{
public:
Print()
{
count = 0;
}
void operator()(int &num)
{
cout << count << ":" << num << " ";
count++;
}
private:
int count;
};
// 普通函数
void print(int &num)
{
cout << num << " ";
}
int main()
{
vector<int> vec = {2, 4, 6, 8, 10};
cout << "传入Lambda表达式:" << endl;
for_each(vec.begin(), vec.end(), [](int a)
{ cout << a << " "; });
cout << endl;
for_each(vec.begin(), vec.end(), [](int &x)
{ x *= x; });
for (int i = 0; i < vec.size(); i++)
{
cout << vec[i] << " ";
}
cout << endl;
cout << "传入普通函数:" << endl;
for_each(vec.begin(), vec.end(), print);
cout << endl;
cout << "传入一元函数对象:" << endl;
for_each(vec.begin(), vec.end(), Print());
cout << endl;
return 0;
}
🆚🆚运行结果:
传入Lambda表达式:
2 4 6 8 10
4 16 36 64 100
传入普通函数:
4 16 36 64 100
传入一元函数对象:
0:4 1:16 2:36 3:64 4:100
4.7 随机数
srand()
用来初始化随机数种子,rand()
用来产生随机数。
4.7.1 srand()函数
随机数产生的初始值(种子值),
srand()
函数是随机数发生器的初始化函数,和rand()
配合使用产生伪随机序列。
srand((unsigned)time(NULL));
4.7.2 rand()函数
如果要生成一个指定范围内的随机整数,可以使用取模运算符
%
将rand()
函数的返回值对范围大小取模,rand()
函数左闭右开。
int number = rand() % max + 1;//生成1-max的随机整数
5. 代码演示
#include <iostream>
#include <algorithm>
#include <vector>
#include <cstdlib>
#include <ctime>
using namespace std;
int* getmax(vector<int> vec, int length) {
static int max[2] = { 0,0 };//加上static关键字,定义静态局部变量
for (int i = 0; i < length; i++) {
if (max[0] < vec[i]) {
max[1] = max[0];
max[0] = vec[i];
}
else if (max[1] < vec[i]) {
max[1] = vec[i];
}
}
//int max = *max_element(vec.begin(), vec.end());//vector容器查找最大值
return max;
}
int main() {
vector<int> vec;//创建动态数组
vec.resize(10);//用于设定容器的内存大小,且创建元素(对象)
vec.reserve(100);//用于预分配容器所需的内存空间,不会构造新的元素
vector<int>::iterator it;//创建迭代器,原来遍历或指向容器里面的元素
int length = (int)vec.size();//返回容器元素的个数
srand((unsigned)time(NULL));//srand()用来设置rand()产生随机数时的随机数种子
for (int i = 0; i < length; i++) {
vec[i] = rand() % 100 + 1;//生成1-100的随机数
for (int j = 0; j < i; j++) {//判断和前面的数是否重复
if (vec[i] == vec[j]) {//如果重复,重新产生随机数
i--;//退回上次外循环,终止本次内循环
break;
}
}
}
//vec.push_back(101);//在尾部加入元素
cout << "容器数据:" << endl;
for (it = vec.begin(); it != vec.end(); it++) {
cout << *it << " ";//使用迭代器访问元素
}
cout << endl;
//vec.shrink_to_fit();//成功退回未使用的内存空间
cout << "容器的数据个数为:" << vec.size() << ",容器的存储容量为:" << vec.capacity() << endl;
int* max = getmax(vec, length);
cout << "容器中最大的两个数:" << endl;
for (int i = 0; i < 2; i++) {
cout << "第" << i + 1 << "个数是" << *(max + i) << endl;
}
return 0;
}
🆚🆚运行结果:
容器数据:
44 88 21 38 19 59 50 7 64 75
容器的数据个数为:10,容器的存储容量为:100
容器中最大的两个数:
第1个数是88
第2个数是75
✨✨✨✨✨✨✨✨✨✨✨✨✨✨✨✨✨✨✨✨✨✨✨✨✨✨✨✨✨✨✨✨✨✨✨✨✨✨
——>以上内容是关于如何用C++语言找出vector容器中最大的两个数,希望对初学者或再次学习者有所帮助,基础打扎实,不怕风吹雨打! 如果以上内容有错误或者内容不全,望大家提出!我也会继续写好每一篇博文!
👍👍👍
待续未完
——文优
🙊🙊🙊
欢迎观看和提问!!!
👏👏👏