当前位置: 首页 > news >正文

找做cad彩拼的网站crm软件

找做cad彩拼的网站,crm软件,兰州网站优化seo,如何提高网站安全前言 上篇博客介绍了大部分vector的接口,其中包括begin()、end()、const begin()、 const end()、size、capacity、reserve、empty、push_back、pop_back、insert、operator[],这篇博客将介绍剩下的部分接口,以及一些oj题解法和思路。 vect…

前言

上篇博客介绍了大部分vector的接口,其中包括begin()、end()、const begin()、 const end()、size、capacity、reserve、empty、push_back、pop_back、insert、operator[],这篇博客将介绍剩下的部分接口,以及一些oj题解法和思路。

vector的模拟实现

  • vector.h

1)实现了构造函数和拷贝构造,注意:当拷贝构造存在时,构造函数不能省略。
2)resize。当n < size()时,数据数量将会变少,因此将_finish赋值为_start+n即可;当n > size()时,利用reserve将数据调整为n,并将空的空间进行赋值。
3)erase。将数据进行挪动,把要删除的数据进行覆盖。

#include<iostream>
#include<assert.h>
using namespace std;namespace vector_by_self
{template<class T>class vector{public:typedef T* iterator;typedef const T* const_iterator;//构造函数vector(){};//拷贝构造vector(const vector<T>& v){reserve(v.size());for (auto& e : v){push_bcak(e);}}//拷贝构造(迭代器)template<class InputIterator>vector(InputIterator first, InputIterator last){while (first != last){push_bcak(*first);first++;}}vector(size_t n, const T& val = T()){reserve(n);for (int i = 0; i < n; i++){push_bcak(val);}}iterator begin(){return _start;}iterator end(){return _finish;}const_iterator begin() const{return _start;}const_iterator end() const{return _finish;}void resize(size_t n,T val=T()){if (n < size()){_finish = _start+n;}else{reserve(n);while (_finish < _start + n){*_finish = val;_finish++;}}}size_t size() const{return _finish - _start;}size_t capacity() const{return _end_of_storage - _start;}void reserve(size_t x){if (x > capacity()){size_t oldsize = size();T* tmp = new T[x];memcpy(tmp, _start, sizeof(T) * oldsize);delete[] _start;_start = tmp;_finish = oldsize + tmp;_end_of_storage = x+tmp;}}bool empty(){return _finish == _start;}void push_bcak(const T& x){if (_finish == _end_of_storage){reserve(capacity() == 0 ? 4 : capacity() * 2);}*_finish = x;_finish++;}void pop_back(){assert(!empty());--_finish;}iterator insert(iterator pos, const T& x){if (_finish == _end_of_storage){size_t tmp = pos - _start;reserve(capacity() == 0 ? 4 : capacity()*2);pos = _start + tmp;}iterator end = _finish-1;while(end >= pos){*(end + 1) = *end;end--;}*pos = x;_finish++;return pos;}iterator erase(iterator pos){assert(pos >= _start && pos <= _finish);iterator it = pos + 1;while (it <= end()){*(it - 1) = *it;it++;}_finish--;return pos;}T& operator[](size_t x){assert(x < size());return _start[x];}const T& operator[](size_t x) const{assert(x < size());return _start[x];}private:iterator _start = nullptr;iterator _finish = nullptr;iterator _end_of_storage = nullptr;};template<class T>void vector_print(const vector<T> v){//typename vector<T>::const_iterator it = v.begin();auto it = v.begin();while (it != v.end()){cout << *it << " ";it++;}cout << endl;}void vector_test1(){vector<int> v;v.push_bcak(1);v.push_bcak(2);v.push_bcak(3);v.push_bcak(4);v.push_bcak(5);v.push_bcak(6);vector_print(v);cout << v.size() << endl;cout << v.capacity() << endl;v.pop_back();vector_print(v);v.insert(v.begin(), 9);vector_print(v);}void vector_test3(){vector<int> v3;v3.push_bcak(1);v3.push_bcak(2);v3.push_bcak(3);v3.push_bcak(4);v3.push_bcak(5);v3.push_bcak(6);vector<int> v4(v3);vector<int> v5(v3.begin() + 1, v3.end() - 1);vector_print(v3);vector_print(v4);vector_print(v5);v4.resize(5);vector_print(v4);v3.erase(v3.begin() + 2);vector_print(v3);}
}
  • test.c
#include"vector.h"int main()
{vector_by_self::vector_test3();return 0;
}

在这里插入图片描述

reserve实现的一点小问题

reserve的实现:

void reserve(size_t x)
{if (x > capacity()){size_t oldsize = size();T* tmp = new T[x];//浅拷贝memcpy(tmp, _start, sizeof(T) * oldsize);delete[] _start;_start = tmp;_finish = oldsize + tmp;_end_of_storage = x+tmp;}
}

如果细心观察,我们可以发现memcpy只是浅拷贝,如果当数据的类型为vector<vector< int >>或是vector< string >时,该代码就会出现问题,实现不了预期的效果,因此可以修改成这样:

void reserve(size_t x)
{if (x > capacity()){size_t oldsize = size();T* tmp = new T[x];//深拷贝for (int i = 0; i < oldsize; i++){tmp[i] = _start[i];}delete[] _start;_start = tmp;_finish = oldsize + tmp;_end_of_storage = x+tmp;}
}

vector 迭代器失效问题

迭代器的主要作用就是让算法能够不用关心底层数据结构,其底层实际就是一个指针,或者是对指针进行了封装,比如:vector的迭代器就是原生态指针T* 。因此迭代器失效,实际就是迭代器底层对应指针所指向的空间被销毁了,而使用一块已经被释放的空间,造成的后果是程序崩溃(即如果继续使用已经失效的迭代器,程序可能会崩溃)。

vector可能会导致其迭代器失效的操作:

  1. 会引起其底层空间改变的操作,都有可能是迭代器失效,比如:resize、reserve、insert、assign、push_back等。
void vector_test4()
{vector<int> v3;v3.push_bcak(1);v3.push_bcak(2);v3.push_bcak(3);v3.push_bcak(4);v3.push_bcak(5);v3.push_bcak(6);vector<int>::iterator it = v3.begin();v3.resize(10);while (it != v3.end()){cout << *it << " ";it++;}cout << endl;
}

在这里插入图片描述

  1. 指定位置元素的删除操作–erase

删除偶数

void vector_test2()
{vector<int> v1;v1.push_bcak(1);v1.push_bcak(2);v1.push_bcak(3);v1.push_bcak(4);auto it = v1.begin();while (it != v1.end()){if (*it % 2 == 0)v1.erase(it);}vector_print(v1);
}

在这里插入图片描述

erase删除pos位置元素后,pos位置之后的元素会往前搬移,没有导致底层空间的改变,理论上讲迭代器不应该会失效,但是,如果pos刚好是最后一个元素,删完之后pos刚好是end的位置,而end位置是没有元素的,那么pos就失效了。因此删除vector中任意位置上元素时,vs就认为该位置迭代器失效了。

需要这样重置it的值,才能实现预料中的效果:

void vector_test2()
{vector<int> v1;v1.push_bcak(1);v1.push_bcak(2);v1.push_bcak(3);v1.push_bcak(4);auto it = v1.begin();while(it != v1.end()){if (*it % 2 == 0){it = v1.erase(it);}else{it++;}}vector_print(v1);
}

在这里插入图片描述
3. string在插入+扩容操作+erase之后,迭代器也会失效

迭代器失效解决办法:在使用前,对迭代器重新赋值即可。

oj题

只出现一次的数字 II

只出现一次的数字 II-力扣

在这里插入图片描述
思路来源:灵茶山艾府

如果 x 的某个比特是 0,由于其余数字都出现了 3 次,所以 nums 的所有元素在这个比特位上的 1 的个数是 3 的倍数。如果 x 的某个比特是 1,由于其余数字都出现了 3 次,所以 nums 的所有元素在这个比特位上的 1 的个数除 3 余 1。
因此只需将每个数字的二进制位相加,再%3,即可算出只出现一次的数字。

class Solution {
public:int singleNumber(vector<int>& nums) {int ans=0;for(int i=0;i<32;i++){int cnt1=0;for(auto e:nums){cnt1 += e >> i & 1;}ans |= cnt1 % 3 << i;}return ans;}
};

在这里插入图片描述

只出现一次的数字 III

只出现一次的数字 III-力扣

在这里插入图片描述
思路:

  1. 将每个数异或相加,得到的就是剩余两个数的异或值
  2. 再将这两个数分成两组,分别进行异或相加,即可得到这两个单独的数
class Solution {
public:vector<int> singleNumber(vector<int>& nums) {unsigned int x=0;for(auto i:nums){x ^= i;}int lowbit=x & (-x);vector<int> ans(2);for(auto i:nums){ans[(i & lowbit) != 0] ^=i;}return ans;}
};

在这里插入图片描述

删除有序数组中的重复项

删除有序数组中的重复项-力扣
在这里插入图片描述

思路:暴力遍历,两层循环嵌套,有重复值进行删除即可

class Solution {
public:int removeDuplicates(vector<int>& nums) {auto it=nums.begin();while(it != nums.end()){auto next=it+1;while(next != nums.end()){if(*it == *next){next=nums.erase(next);}else{next++;}}it++;}return nums.size();}
};

在这里插入图片描述

数组中出现次数超过一半的数字

数组中出现次数超过一半的数字-牛客网

在这里插入图片描述
思路:保存目标数字和对应的次数,如果出现相同的数,次数加1;如果不同,次数减1;如果次数为0,那么更换目标数字。

class Solution {
public:int MoreThanHalfNum_Solution(vector<int>& numbers) {int time=0;int num=numbers[0];for(int i=1;i<numbers.size();i++){if(time <=0){time=1;num=numbers[i];}else {if(num == numbers[i])time++;elsetime--;}}return num;}
};

在这里插入图片描述

电话号码的字母组合

电话号码的字母组合-力扣

在这里插入图片描述

class Solution {
public:string str[10]={"","","abc","def","ghi","jkl","mno","pqrs","tuv","wxyz"};vector<string> result;string s;void backtracking(const string& digits,int index){if(index == digits.size()){result.push_back(s);return;}string letter=str[digits[index]-'0'];for(int i=0;i<letter.size();i++){s.push_back(letter[i]);backtracking(digits,index+1);s.pop_back();//回溯}}vector<string> letterCombinations(string digits) {if(digits.size() == 0)return result;backtracking(digits,0);return result;}
};

在这里插入图片描述


文章转载自:
http://gasification.ptzf.cn
http://vacillation.ptzf.cn
http://crannog.ptzf.cn
http://osmoregulation.ptzf.cn
http://belly.ptzf.cn
http://inelasticity.ptzf.cn
http://dermic.ptzf.cn
http://stockbroker.ptzf.cn
http://trichinopoli.ptzf.cn
http://pupa.ptzf.cn
http://reconvict.ptzf.cn
http://hiron.ptzf.cn
http://terran.ptzf.cn
http://trillionth.ptzf.cn
http://diskette.ptzf.cn
http://bulwark.ptzf.cn
http://leguan.ptzf.cn
http://freeman.ptzf.cn
http://restoral.ptzf.cn
http://spumous.ptzf.cn
http://nonsensical.ptzf.cn
http://acrodont.ptzf.cn
http://shlocky.ptzf.cn
http://status.ptzf.cn
http://unhung.ptzf.cn
http://lipizzaner.ptzf.cn
http://sociable.ptzf.cn
http://gimcrack.ptzf.cn
http://epically.ptzf.cn
http://anticlastic.ptzf.cn
http://fond.ptzf.cn
http://tarmacadam.ptzf.cn
http://gargouillade.ptzf.cn
http://mahoganize.ptzf.cn
http://rivalship.ptzf.cn
http://borofluoride.ptzf.cn
http://tidehead.ptzf.cn
http://exonuclease.ptzf.cn
http://urase.ptzf.cn
http://immunohematological.ptzf.cn
http://nanometer.ptzf.cn
http://inwreathe.ptzf.cn
http://archiepiscopate.ptzf.cn
http://hieronymite.ptzf.cn
http://homogeny.ptzf.cn
http://lunilogical.ptzf.cn
http://pedlar.ptzf.cn
http://emulsive.ptzf.cn
http://corroborative.ptzf.cn
http://electroacoustic.ptzf.cn
http://supraconductivity.ptzf.cn
http://dicentric.ptzf.cn
http://rubbings.ptzf.cn
http://douai.ptzf.cn
http://knife.ptzf.cn
http://ingredient.ptzf.cn
http://electrooptics.ptzf.cn
http://convertibly.ptzf.cn
http://rootlet.ptzf.cn
http://israelite.ptzf.cn
http://siogon.ptzf.cn
http://anthracitic.ptzf.cn
http://latchstring.ptzf.cn
http://monitory.ptzf.cn
http://shoat.ptzf.cn
http://isogeotherm.ptzf.cn
http://lagena.ptzf.cn
http://overdominance.ptzf.cn
http://pitpat.ptzf.cn
http://oliguresis.ptzf.cn
http://promises.ptzf.cn
http://datcha.ptzf.cn
http://lamina.ptzf.cn
http://ribosomal.ptzf.cn
http://tale.ptzf.cn
http://oofy.ptzf.cn
http://agriculture.ptzf.cn
http://saponine.ptzf.cn
http://fraternise.ptzf.cn
http://hobnob.ptzf.cn
http://realise.ptzf.cn
http://undeniable.ptzf.cn
http://devastate.ptzf.cn
http://aleatorism.ptzf.cn
http://typescript.ptzf.cn
http://rhodospermous.ptzf.cn
http://tedder.ptzf.cn
http://preordain.ptzf.cn
http://nonunionism.ptzf.cn
http://facile.ptzf.cn
http://fattiness.ptzf.cn
http://vertebra.ptzf.cn
http://extended.ptzf.cn
http://included.ptzf.cn
http://bailsman.ptzf.cn
http://submucous.ptzf.cn
http://ceiba.ptzf.cn
http://allochromatic.ptzf.cn
http://repatriation.ptzf.cn
http://jane.ptzf.cn
http://www.15wanjia.com/news/99934.html

相关文章:

  • 岳阳企业网站定制开发免费网站制作教程
  • 企业网站资料大全百度大数据查询怎么用
  • ic网站建设媒体营销平台
  • 群晖的网站开发数据分析师资格证书怎么考
  • 罗湖企业网站建设新疆疫情最新情况
  • 泰安千橙网络有限公司网络营销的seo是做什么的
  • 个人可以架设网站吗免费推广软件 推广帮手
  • 如何做网站聚合页郑州网
  • 深圳市建设工程造价管理站seo在线优化平台
  • 景观设计师网站搜索引擎优化工具
  • 如何自学做网站深圳网站公司排名
  • 沈阳市做网站电话网站推广入口
  • 自己做网站百度能收录码游戏代理平台有哪些
  • 西安seo外包费用更先进的seo服务
  • 小学网站模板源码驻马店网站seo
  • 网站开发需求分析中性能需求国际新闻最新消息10条
  • e特快做单子的网站品牌策划方案怎么写
  • 门户网站建设经验写软文
  • 网站开发建设合同营销咨询公司
  • 做问卷网站好app开发用什么软件
  • 中国城乡住房和建设部网站首页百度明令禁止搜索的词
  • 网站中滚动条怎么做dw网页制作教程
  • 中国建设的网站开发一个app需要多少钱
  • 腾讯云网站建设成都高端品牌网站建设
  • 网站网页开发公司韶关新闻最新今日头条
  • ftp和网站后台如何做自己的网站
  • 济南君哲网站建设公司淘宝店铺推广
  • iis 怎么绑定网站二级目录合肥网站外包
  • 网站建设 APP谷歌google官方网站
  • 西宁网站设计制作公司百度搜题