asp.net做三个网站,wordpress调用栏目,百度关键词优化排名技巧,做网站要买什么一、介绍
1. vector是表示可变大小数组的序列容器#xff0c;就像数组一样#xff0c;vector也采用的连续存储空间来存储元素。也就是意味着可以采用下标对vector的元素 进行访问#xff0c;和数组一样高效。但是又不像数组#xff0c;它的大小是可以动态改变的#xff0…一、介绍
1. vector是表示可变大小数组的序列容器就像数组一样vector也采用的连续存储空间来存储元素。也就是意味着可以采用下标对vector的元素 进行访问和数组一样高效。但是又不像数组它的大小是可以动态改变的而且它的大小会被容器自动处理。
原先数组的本质也是对一个个整形进行数据管理可以想象成一个表格由于需要管理的数据比起单个整形现实情况更加的复杂和庞大因此其作用也需要扩大可以将其想象成一个笔记本被开一个空间则表示笔记本有多少页统一管理一个相同类型的数据当然如果系统更为复杂的情况则可以往更大的系统去想象
2.本质讲vector使用动态分配数组来存储它的元素。当新元素插入时候这个数组需要被重新分配大小为了增加存储空间。其做法是分配一个新的数组然后将全部元素移到这个数组。就时间而言这是 一个相对代价高的任务因为每当一个新的元素加入到容器的时候vector并不会每次都重新分配大小。
3.vector分配空间策略vector会分配一些额外的空间以适应可能的增长因为存储空间比实际需要的存 储空间更大。不同的库采用不同的策略权衡空间的使用和重新分配。但是无论如何重新分配都应该是 对数增长的间隔大小以至于在末尾插入一个元素的时候是在常数时间的复杂度完成的。因此vector占用了更多的存储空间为了获得管理存储空间的能力并且以一种有效的方式动态增 长。与其它动态序列容器相比deque, list and forward_list vector在访问元素的时候更加高效在末 尾添加和删除元素相对高效。对于其它不在末尾的删除和插入操作效率更低。比起list和forward_list 统一的迭代器和引用更好。 二、常见接口及其使用
1.vector的定义
构造函数声明接口说明vector()无参构造vectorsize_type n, const value_type val value_type()构造并初始化n个valvector (InputIterator first, InputIterator last)用迭代器初始化vector (const vector x)拷贝构造 vectorint first; // empty vector of intsvectorint second(4, 100); // four ints with value 100vectorint third(second.begin(), second.end()); // iterating through secondvectorint fourth(third); // a copy of third2.vector iterator的使用
iterator的使用接口说明beginendbegin获取第一个数据位置的iteratorend获取最后一个数据下一个位置的iteratorrbeginendrbegin获取最后一个数据位置的reverse_iteratorend获取第一个数据前一个位置的reserve_iterator
void PrintVector(const vectorint v)
{// const对象使用const迭代器进行遍历打印vectorint::const_iterator it v.begin();while (it ! v.end()){cout *it ;it;}cout endl;
}
迭代器失效问题
对于vector可能会导致其迭代器失效的操作有
1. 会引起其底层空间改变的操作都有可能是迭代器失效比如resize、reserve、insert、assign、 push_back等。
2. 指定位置元素的删除操作--erase删除操作在vs编译器认为一旦执行则原有迭代器视为失效 3. 与vector类似string在插入扩容操作erase之后迭代器也会失效。 迭代器失效解决办法在使用前对迭代器重新赋值即可。
3.vector空间管理接口
容量空间接口说明size获取数据个数capacity获取容量大小empty判断是否为空 resize 改变vector的size必要时会扩容但不会缩容reserve改变vector的capacity的大小必要是会扩容一般不缩容
4.vector的增删查改
增删查改接口说明push_back尾插pop_back尾删find 查找vector接口内没有自带的查找接口需要时常用算法模块的查找功能传参传迭代器找到返回对应迭代器位置找不到返回end的位置 insert在pos之前插入val值erase删除pos位置的数据swap交换两个vector的数据空间operator[ ]像数组一样访问
void TestVector5()
{// 使用列表方式初始化C11新语法vectorint v{ 1, 2, 3, 4 };// 在指定位置前插入值为val的元素比如3之前插入30,如果没有则不插入// 1. 先使用find查找3所在位置// 注意vector没有提供find方法如果要查找只能使用STL提供的全局findauto pos find(v.begin(), v.end(), 3);if (pos ! v.end()){// 2. 在pos位置之前插入30v.insert(pos, 30);}vectorint::iterator it v.begin();while (it ! v.end()) {cout *it ;it;}cout endl;pos find(v.begin(), v.end(), 3);// 删除pos位置的数据v.erase(pos);it v.begin();while (it ! v.end()) {cout *it ;it;}cout endl;
}// operator[]index 和 C11中vector的新式forauto的遍历
// vector使用这两种遍历方式是比较便捷的。 三、相关的OJ题
1.只出现一次的数字一
题目链接
力扣LeetCode官网 - 全球极客挚爱的技术成长平台
题目描述
在一组数字中除了一个数字是单独出现的其余都是成双出现的要求找到那个单独的数字。
解题思路
全部异或一起就能找到那个数字
参考代码
class Solution {
public:int singleNumber(vectorint nums) {int ret 0;for(auto ch: nums){ret^ch;}return ret;}
}; 2.杨辉三角
题目链接
118. 杨辉三角 - 力扣LeetCode
题目描述
给一个数字表示杨辉三角的层级数需要返回一个类似于二维数组的结构去表示杨辉三角
解题思路
首先先构造出杨辉三角的结构可以用vectorvector类型去初始化出相应的结构然后在对数据进行处理
代码参考
class Solution {
public:vectorvectorint generate(int numRows) {vectorvectorint vvi;vvi.resize(numRows);for(int i 0;ivvi.size();i){vvi[i].resize(i1,0);vvi[i][0] vvi[i][i] 1;}for(int i 0;ivvi.size();i){for(int j 0;jvvi[i].size();j){if(vvi[i][j] 0){vvi[i][j] vvi[i-1][j-1] vvi[i-1][j]; }}}return vvi;}
}; 3.电话号码字母组合
题目链接
17. 电话号码的字母组合 - 力扣LeetCode
题目描述
数字“2-9”对应着一串字母题目给定一串数字字符串要求得到对应字母的全排列组合
解题思路 这里画出部分逻辑将数字先转化成字母串对字母串每一个都单独往下递归将递归至最后一层的结果记录下来最终将所有组合完成递归后结束。
参考代码
class Solution
{string num_let[10] {,,abc,def,ghi,jkl,mno,pqrs,tuv,wxyz};
public://递归的参数设计上第一个参数是数字字符第二个di是用于在递归过程中控制字母字符是第几层的参数//第三个参数是用于在递归过程中记录下每次完成一次排列后的结果第四个参数则是每次完成单趟排列后统一存起来用于返回的//因此第一个参数和第四个参数采用的是传引用void letterCom(string s_num,int di,string tmp,vectorstring ret){if(di s_num.size())//递归的结束条件结束时将单次排列的结果存到ret中{ret.push_back(tmp);return;}//先将每一层对应的字母串取出来int num s_num[di] - 0;string s_letter num_let[num];//每一个都逐一向下递归for(auto ch: s_letter){letterCom(s_num,di1,tmpch,ret);}}vectorstring letterCombinations(string digits) {vectorstring ret;//用于存放返回的结果if(digits )//这里是考虑到空数字字符则直接返回空{return ret;}letterCom(digits,0,,ret);//递归的参数不止一个digits因此要单独用一个函数实现递归return ret;}
}; 总结
本篇对vector的常用接口进行了整理介绍并且整理了相关的OJ题用于练习熟悉vector的使用。
文章转载自: http://www.morning.nllst.cn.gov.cn.nllst.cn http://www.morning.bswxt.cn.gov.cn.bswxt.cn http://www.morning.gtwtk.cn.gov.cn.gtwtk.cn http://www.morning.sryhp.cn.gov.cn.sryhp.cn http://www.morning.rknjx.cn.gov.cn.rknjx.cn http://www.morning.kxnjg.cn.gov.cn.kxnjg.cn http://www.morning.gqmhq.cn.gov.cn.gqmhq.cn http://www.morning.ljygq.cn.gov.cn.ljygq.cn http://www.morning.jqcrf.cn.gov.cn.jqcrf.cn http://www.morning.jjrsk.cn.gov.cn.jjrsk.cn http://www.morning.xyyplp.cn.gov.cn.xyyplp.cn http://www.morning.flpjy.cn.gov.cn.flpjy.cn http://www.morning.c7498.cn.gov.cn.c7498.cn http://www.morning.rmxwm.cn.gov.cn.rmxwm.cn http://www.morning.xkjqg.cn.gov.cn.xkjqg.cn http://www.morning.jfxth.cn.gov.cn.jfxth.cn http://www.morning.nnttr.cn.gov.cn.nnttr.cn http://www.morning.fglxh.cn.gov.cn.fglxh.cn http://www.morning.sfqtf.cn.gov.cn.sfqtf.cn http://www.morning.nyfyq.cn.gov.cn.nyfyq.cn http://www.morning.cpnsh.cn.gov.cn.cpnsh.cn http://www.morning.mpmtz.cn.gov.cn.mpmtz.cn http://www.morning.rfqk.cn.gov.cn.rfqk.cn http://www.morning.gczqt.cn.gov.cn.gczqt.cn http://www.morning.kbdrq.cn.gov.cn.kbdrq.cn http://www.morning.dqxph.cn.gov.cn.dqxph.cn http://www.morning.kmqjx.cn.gov.cn.kmqjx.cn http://www.morning.dysgr.cn.gov.cn.dysgr.cn http://www.morning.cpctr.cn.gov.cn.cpctr.cn http://www.morning.fqtzn.cn.gov.cn.fqtzn.cn http://www.morning.gtdf.cn.gov.cn.gtdf.cn http://www.morning.krwzy.cn.gov.cn.krwzy.cn http://www.morning.trpq.cn.gov.cn.trpq.cn http://www.morning.gbybx.cn.gov.cn.gbybx.cn http://www.morning.txjrc.cn.gov.cn.txjrc.cn http://www.morning.nlrxh.cn.gov.cn.nlrxh.cn http://www.morning.dpppx.cn.gov.cn.dpppx.cn http://www.morning.xkzr.cn.gov.cn.xkzr.cn http://www.morning.kdjtt.cn.gov.cn.kdjtt.cn http://www.morning.mzhjx.cn.gov.cn.mzhjx.cn http://www.morning.dtmjn.cn.gov.cn.dtmjn.cn http://www.morning.yydeq.cn.gov.cn.yydeq.cn http://www.morning.1000sh.com.gov.cn.1000sh.com http://www.morning.dpruuode.cn.gov.cn.dpruuode.cn http://www.morning.hcxhz.cn.gov.cn.hcxhz.cn http://www.morning.bwznl.cn.gov.cn.bwznl.cn http://www.morning.rwjh.cn.gov.cn.rwjh.cn http://www.morning.plhyc.cn.gov.cn.plhyc.cn http://www.morning.lxfdh.cn.gov.cn.lxfdh.cn http://www.morning.kkjlz.cn.gov.cn.kkjlz.cn http://www.morning.mqnbm.cn.gov.cn.mqnbm.cn http://www.morning.lqjlg.cn.gov.cn.lqjlg.cn http://www.morning.wjhqd.cn.gov.cn.wjhqd.cn http://www.morning.mwrxz.cn.gov.cn.mwrxz.cn http://www.morning.dpfr.cn.gov.cn.dpfr.cn http://www.morning.syynx.cn.gov.cn.syynx.cn http://www.morning.hxbjt.cn.gov.cn.hxbjt.cn http://www.morning.xrct.cn.gov.cn.xrct.cn http://www.morning.zlnkq.cn.gov.cn.zlnkq.cn http://www.morning.wwthz.cn.gov.cn.wwthz.cn http://www.morning.qqrqb.cn.gov.cn.qqrqb.cn http://www.morning.ktmpw.cn.gov.cn.ktmpw.cn http://www.morning.kkysz.cn.gov.cn.kkysz.cn http://www.morning.gqryh.cn.gov.cn.gqryh.cn http://www.morning.wrtsm.cn.gov.cn.wrtsm.cn http://www.morning.lstmq.cn.gov.cn.lstmq.cn http://www.morning.gkdqt.cn.gov.cn.gkdqt.cn http://www.morning.pqsys.cn.gov.cn.pqsys.cn http://www.morning.jqrp.cn.gov.cn.jqrp.cn http://www.morning.lwhsp.cn.gov.cn.lwhsp.cn http://www.morning.ey3h2d.cn.gov.cn.ey3h2d.cn http://www.morning.cjmmn.cn.gov.cn.cjmmn.cn http://www.morning.rxlk.cn.gov.cn.rxlk.cn http://www.morning.kgsws.cn.gov.cn.kgsws.cn http://www.morning.jbtzx.cn.gov.cn.jbtzx.cn http://www.morning.snmth.cn.gov.cn.snmth.cn http://www.morning.qdmdp.cn.gov.cn.qdmdp.cn http://www.morning.psxxp.cn.gov.cn.psxxp.cn http://www.morning.zlnkq.cn.gov.cn.zlnkq.cn http://www.morning.tpbhf.cn.gov.cn.tpbhf.cn