
- 动态数组优点:
和数组类似开辟一段连续的空间,并且支持随机访问,所以它的查找效率高其时间复杂度O(1)。 - 动态数组缺点:
由于开辟一段连续的空间,所以插入删除会需要对数据进行移动比较麻烦,时间复杂度O(n),另外当空间不足时还需要进行扩容。 - vector与list的比较:
vector拥有一段连续的内存空间,因此支持随机访问,如果需要高效的随即访问,而不在乎插入和删除的效率,使用vector。
list拥有一段不连续的内存空间,如果需要高效的插入和删除,而不关心随机访问,则应使用list。file.h文件:
/*****************************************************************************
* C++ 设计模式之原型模式 *
* Copyright (C) 2020 1158292387@qq.com *
* *
* @file file.h *
* @brief 对文件的简述 *
* Details. *
* *
* @author Leeme *
* @email 1158292387@qq.com *
* @version 1.0.0.1(版本号) *
* @date 2020.10.26 *
* @license GNU General Public License (GPL) *
* *
*----------------------------------------------------------------------------*
* Remark : Description *
*----------------------------------------------------------------------------*
* Change History : *
* <Date> | <Version> | <Author> | <Description> *
*----------------------------------------------------------------------------*
* 2020/10/26 | 1.0.0.1 | Leeme | Create file *
*----------------------------------------------------------------------------*
* *
*****************************************************************************/
#pragma once
#include <iostream>
#include <vector>
#include <set>
#include <algorithm>
/**
* @brief 简单概述
* @brief 获取vector最大值
*/
double GetVecMax(const std::vector<double> &vec)
{
if (vec.size() == 0) return 0;
return *std::max_element(vec.begin(), vec.end());
}
/**
* @brief 简单概述
* @brief 获取vector最小值
*/
double GetVecMin(const std::vector<double>& vec)
{
if (vec.size() == 0) return 0;
return *std::min_element(vec.begin(), vec.end());
}
/**
* @brief 简单概述
* @brief 获取vector最大值并返回索引
*/
double GetVecMaxIndex(const std::vector<double>& vec, int* index)
{
if (vec.size()>0)
{
auto itr = std::max_element(vec.begin(), vec.end());
*index = std::distance(std::begin(vec), itr);
return *itr;
}
else
{
*index = 0;
return 0;
}
}
/**
* @brief 简单概述
* @brief 获取vector最小值并返回索引
*/
double GetVecMinIndex(const std::vector<double>& vec, int* index)
{
if (vec.size() > 0)
{
auto itr = std::min_element(vec.begin(), vec.end());
*index = std::distance(std::begin(vec), itr);
return *itr;
}
else
{
*index = 0;
return 0;
}
}
/**
* @brief 简单概述
* @brief 删除vector中重复的元素
*/
int RemoveRepeatElement(std::vector<int>& nums)
{
int len = 0, i = 0, j = 0;
while (i < nums.size() && j < nums.size())
{
nums[len++] = nums[i];
while (j < nums.size() && nums[i] == nums[j])
{
j++;
}
i = j;
}
return len;
}
/**
* @brief 简单概述
* @brief 利用Set删除vector中重复的元素
*/
int RemoveRepeatElementWithSet(std::vector<int>& nums)
{
std::set<int> num;
for (int i = 0; i < nums.size(); i++)
{
num.insert(nums[i]);
}
nums.assign(num.begin(), num.end());
return nums.size();
}
/**
* @brief 简单概述
* @brief 利用Uique删除vector中重复的元素
*/
int RemoveRepeatElementWithuUique(std::vector<int>& nums)
{
std::sort(nums.begin(), nums.end());
nums.erase(std::unique(begin(nums), end(nums)), nums.end());
return nums.size();
}
/**
* @brief 简单概述
* @brief Point2D 类定义
*/
class Point2D
{
public:
Point2D(double px, double py)
{
x = px; y = py;
}
public:
double x;
double y;
};
/**
* @brief 简单概述
* @brief vector<Point2D>排序
*/
void SortPoint2DVec(std::vector<Point2D>& vec)
{
std::sort(begin(vec), end(vec), [](const Point2D& p0, const Point2D& p1)
{
return p0.x < p1.x;
});
}
main.cpp:
#include <iostream>
#include <vector>
#include "file.h"
int main(int argc, char* argv[])
{
std::vector<double> vec_db = { {3001.0},{1228.0},{897.0},{6573.2},{ 4300.0 },{ 3600.0 },{ 3800.0 },{ 3030.0 },{ 3007.0 },{ 3090.0 } };
std::vector<int> vec_int = { {200},{ 200 },{ 456 },{ 768 },{ 980 },{ 654 },{ 638 },{ 638 },{ 945 },{ 362 } };
std::cout << "* *** ***** ******* ***** *** * -- std::vector<double> vec_db for test -- * *** ***** ******* ***** *** *" << std::endl;
for (const auto itr : vec_db)
{
std::cout << itr << ",";
}
std::cout << std::endl;
std::cout << "length:" << vec_db.size() << std::endl;
std::cout << "* *** ***** ******* ***** *** * -- std::vector<double> vec_db for test -- * *** ***** ******* ***** *** *" << std::endl << std::endl;
std::cout << "* *** ***** ******* ***** *** * -- std::vector<int> vec_int for test -- * *** ***** ******* ***** *** ***" << std::endl;
for (const auto itr : vec_int)
{
std::cout << itr << ",";
}
std::cout << std::endl;
std::cout << "length:" << vec_int.size() << std::endl;
std::cout << "* *** ***** ******* ***** *** * -- std::vector<int> vec_int for test -- * *** ***** ******* ***** *** ***" << std::endl << std::endl;
std::cout << "Max value of vec_db:" << GetVecMax(vec_db) << std::endl;
std::cout << "Min value of vec_db:" << GetVecMin(vec_db) << std::endl;
int index= -1;
double MaxVal = GetVecMaxIndex(vec_db, &index);
std::cout << "Max value of vec_db:" << MaxVal << " Index:"<< index << std::endl;
double MinVal = GetVecMinIndex(vec_db, &index);
std::cout << "Min value of vec_db:" << MinVal << " Index:" << index << std::endl;
std::cout << "* *** ***** ******* ***** *** * -- RemoveRepeatElement -- * *** ***** ******* ***** *** *****************" << std::endl;
std::vector<int> temp = vec_int;
int length = -1;
length = RemoveRepeatElement(temp);
for (auto itr = temp.begin(); itr != temp.end(); itr++)
{
std::cout << *itr << ",";
}
std::cout << std::endl;
std::cout << "length:" << length << std::endl;
std::cout << "* *** ***** ******* ***** *** * -- RemoveRepeatElementWithSet -- * *** ***** ******* ***** *** **********" << std::endl;
temp = vec_int;
length = RemoveRepeatElementWithSet(temp);
for (auto itr = temp.begin(); itr != temp.end(); itr++)
{
std::cout << *itr << ",";
}
std::cout << std::endl;
std::cout << "length:" << length << std::endl;
std::cout << "* *** ***** ******* ***** *** * -- RemoveRepeatElementWithuUique -- * *** ***** ******* ***** *** *******" << std::endl;
temp = vec_int;
length = RemoveRepeatElementWithuUique(temp);
for (auto itr = temp.begin(); itr != temp.end(); itr++)
{
std::cout << *itr << ",";
}
std::cout << std::endl;
std::cout << "length:" << length << std::endl;
std::vector<Point2D> PointVec;
PointVec.push_back(Point2D(2, 3));
PointVec.push_back(Point2D(9, 3));
PointVec.push_back(Point2D(7, 3));
PointVec.push_back(Point2D(4, 3));
PointVec.push_back(Point2D(6, 3));
PointVec.push_back(Point2D(3, 3));
PointVec.push_back(Point2D(80, 3));
std::cout << "* *** ***** ******* ***** *** * -- std::vector<Point2D> before sort -- * *** ***** ******* ***** *** ****"<< std::endl;
for (const auto itr : PointVec)
{
std::cout << itr.x << ",";
}
std::cout << std::endl;
std::cout << "length of point vec:" << PointVec.size() << std::endl<< std::endl;
SortPoint2DVec(PointVec);
std::cout << "* *** ***** ******* ***** *** * -- std::vector<Point2D> after sort -- * *** ***** ******* ***** *** *****" << std::endl;
for (const auto itr : PointVec)
{
std::cout << itr.x << ",";
}
std::cout << std::endl;
std::cout << "length of point vec:" << PointVec.size() << std::endl;
std::cout << std::endl;
for (size_t i = 0; i < 1; i++)
{
std::cout << "* ** *** **** ***** ****** *******" << std::endl;
std::cout << "** *** **** ***** ****** ******* *" << std::endl;
std::cout << "*** **** ***** ****** ******* * **" << std::endl;
std::cout << "**** ***** ****** ******* * ** ***" << std::endl;
std::cout << "**** **** **** <==> **** **** ****" << std::endl;
std::cout << "***** ****** ******* * ** *** ****" << std::endl;
std::cout << "****** ******* * ** *** **** *****" << std::endl;
std::cout << "******* * ** *** **** ***** ******" << std::endl;
std::cout << "* ** *** **** ***** ****** *******" << std::endl;
}
std::cout << std::endl;
std::cout << "<<<<<<<<<<<<<<<<<<<<<<<<<<<<<== Hello! You are a good man! ==>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>" << std::endl;
getchar();
return 0;
}
