一级a做爰片2202网站,h5页面开发工具,做网站要不要学ps,深圳市建设局官方网站文章目录 vector介绍vector常用的成员函数有关vector定义的函数vector的迭代器使用vector关于空间操作的成员函数vector的增删查改 总结 vector介绍
在C语言的库中包含有公共数据结构的实现#xff0c;C的这个部分内容就是众所周知的STL#xff08;标准模版库#xff09;语言的库中包含有公共数据结构的实现C的这个部分内容就是众所周知的STL标准模版库vector就是标准模板库中的数据结构之一。
vector常用的成员函数
有关vector定义的函数
函数声明说明vector()无参构造vectorsize_type n, const value_type val value_type()构造并初始化n个valvector (const vector x);拷贝构造vector (InputIterator first, InputIterator last);使用迭代器进行初始化构造
无参构造
void test_vector1()
{vectorint v1();
}构造n个val
void test_vector2()
{vectorint v2(10, 1);
}拷贝构造
void test_vector3()
{vectorint v1{ 1,2,3,4 };vectorint v2 v1;for (int i 0;i v2.size();i){cout v2[i] ;}
}迭代器构造
void test_vector4()
{vectorint v1{ 1,2,3,4 };vectorint v2(v1.begin(), v1.end());for (int i 0;i v2.size();i){cout v2[i] ;}
}vector的迭代器使用
iterator的使用说明beginend返回指向容器的第一项的一个适当的迭代器和返回指向终止标志的一个迭代器rbeginrend和begin和end类似但是需要用reverse_iterator获取其迭代器
beginend 使用场景遍历容器 第一种遍历方式
void test_vector5()
{vectorint v1{ 1,2,3,4 };vectorint::iterator it v1.begin();while (it ! v1.end()){cout *it ;it;}
}第二种遍历方式
void test_vector6()
{vectorint v1{ 1,2,3,4 };for (vectorint::iterator it v1.begin();it ! v1.end();it){cout *it ;}
}rbeginrend
这里只展示一种遍历方式
void test_vector7()
{vectorint v1{ 1,2,3,4 };vectorint::reverse_iterator it v1.rbegin();while (it ! v1.rend()){cout *it ;it;}
}剩下的还有什么const_iterator等等下来大家可以自己试试
vector关于空间操作的成员函数
成员函数功能size返回数据个数capacity返回空间大小empty判断容器是否为空resize改变容器的size大小reverse改变容器的capacity大小
前面三个相信大家都会用了所以这里直接从resize开始讲起如果不懂的可以去看我讲解的string那节。
resize
resize函数改变的是size的大小
void test_vector8()
{vectorint v1{ 1,2,3,4 };cout v1.size() endl;v1.resize(10);cout v1.size() endl;
}如果这里默认不给参数的话就会直接默认分配空间并且把新开辟的空间初始化为0 void test_vector8()
{vectorint v1{ 1,2,3,4 };cout v1.size() endl;v1.resize(10, 3);cout v1.size() endl;for (int i 0;i v1.size();i){cout v1[i] ;}
}如果第二个给参数的话剩下的空间就用第二个参数进行初始化 reverse函数
reverse和resize的用法相同。 但是需要注意的是reverse的扩容的策略在每个平台下的每次的扩容量是不同的。 vs下capacity是按1.5倍增长的g是按2倍增长的。
vector的增删查改
push_back尾插
void test_vector9()
{vectorint v1{ 1,2,3,4 };v1.push_back(1);
}pop_back
void test_vector9()
{vectorint v1{ 1,2,3,4 };v1.pop_back();
}findfind函数并不是vector的成员函数而是algorithm中的一个库函数
void test_vector11()
{vectorint v1{ 1,2,3,4 };vectorint::iterator pos find(v1.begin(), v1.end(), 1);cout *pos endl;
}注意find的返回值是迭代器不是返回下标而是返回对应的数的迭代器
Insert Insert也是与迭代器相关的一个成员函数
void test_vector12()
{vectorint v1{ 1,2,3,4 };v1.insert(v1.begin(), 1);
}erase
void test_vector13()
{vectorint v1{ 1,2,3,4 };v1.erase(v1.begin());
}erase可以删除一个数据也可以删除多个数据
void test_vector13()
{vectorint v1{ 1,2,3,4 };v1.erase(v1.begin(), v1.end());
}上面的代码是erase删除一段数据的场景
operator[]重载 vector支持[]访问我们可以像数组一样进行访问比如
int main()
{vectorint v1{ 1,2,3,4,5,6,7,8,9 };for (int i 0;i v1.size();i){cout v1[i] ;}return 0;
}vector相较于数组的优势 在普通数组中不能进行复制操作但是vector容器就支持复制操作当我们需要复制一个容器的时候不需要用一个循环一个一个赋值只需要用一个运算符重载就可以将容器中的值拷贝到另一个容器当中。
总结
在这篇关于“容器vector”的博客中我们深入探讨了 C 中这个强大的数据结构。vector 提供了动态数组的功能允许我们在运行时动态添加、删除元素并且能够以常数时间复杂度访问元素这使得它成为处理数据集合的理想选择。
我们了解了如何创建、初始化和操作 vector以及如何使用其丰富的成员函数来满足各种需求。vector 的标准接口和异常安全性使得在处理数据时更加方便和安全。
通过学习本文希望读者能够更加熟练地使用 vector并且在实际项目中充分发挥它的优势。vector 不仅在算法和数据结构中有着广泛的应用而且在各种类型的程序中都能发挥作用从小型应用到大型系统都可以看到它的身影。
让我们继续深入学习和探索掌握更多 C 中强大的工具和技术不断提升自己的编程能力。感谢阅读 文章转载自: http://www.morning.xinyishufa.cn.gov.cn.xinyishufa.cn http://www.morning.c7491.cn.gov.cn.c7491.cn http://www.morning.mbrbk.cn.gov.cn.mbrbk.cn http://www.morning.yhxhq.cn.gov.cn.yhxhq.cn http://www.morning.zfrs.cn.gov.cn.zfrs.cn http://www.morning.cdrzw.cn.gov.cn.cdrzw.cn http://www.morning.wbyqy.cn.gov.cn.wbyqy.cn http://www.morning.nyfyq.cn.gov.cn.nyfyq.cn http://www.morning.hbywj.cn.gov.cn.hbywj.cn http://www.morning.jgnjl.cn.gov.cn.jgnjl.cn http://www.morning.lfqtp.cn.gov.cn.lfqtp.cn http://www.morning.pttrs.cn.gov.cn.pttrs.cn http://www.morning.mbpfk.cn.gov.cn.mbpfk.cn http://www.morning.tfsyk.cn.gov.cn.tfsyk.cn http://www.morning.bnfsw.cn.gov.cn.bnfsw.cn http://www.morning.htfnz.cn.gov.cn.htfnz.cn http://www.morning.blfll.cn.gov.cn.blfll.cn http://www.morning.dwmmf.cn.gov.cn.dwmmf.cn http://www.morning.kdnrp.cn.gov.cn.kdnrp.cn http://www.morning.lfdmf.cn.gov.cn.lfdmf.cn http://www.morning.slfkt.cn.gov.cn.slfkt.cn http://www.morning.guofenmai.cn.gov.cn.guofenmai.cn http://www.morning.qpsxz.cn.gov.cn.qpsxz.cn http://www.morning.rpzth.cn.gov.cn.rpzth.cn http://www.morning.sffkm.cn.gov.cn.sffkm.cn http://www.morning.kclkb.cn.gov.cn.kclkb.cn http://www.morning.smszt.com.gov.cn.smszt.com http://www.morning.zbhfs.cn.gov.cn.zbhfs.cn http://www.morning.lkxzb.cn.gov.cn.lkxzb.cn http://www.morning.skpdg.cn.gov.cn.skpdg.cn http://www.morning.xmnlc.cn.gov.cn.xmnlc.cn http://www.morning.hjrjr.cn.gov.cn.hjrjr.cn http://www.morning.nhgfz.cn.gov.cn.nhgfz.cn http://www.morning.bxyzr.cn.gov.cn.bxyzr.cn http://www.morning.tfwg.cn.gov.cn.tfwg.cn http://www.morning.lonlie.com.gov.cn.lonlie.com http://www.morning.mzqhb.cn.gov.cn.mzqhb.cn http://www.morning.flzqq.cn.gov.cn.flzqq.cn http://www.morning.ysckr.cn.gov.cn.ysckr.cn http://www.morning.sfwd.cn.gov.cn.sfwd.cn http://www.morning.pcqdf.cn.gov.cn.pcqdf.cn http://www.morning.jcyyh.cn.gov.cn.jcyyh.cn http://www.morning.lmjtp.cn.gov.cn.lmjtp.cn http://www.morning.lwmzp.cn.gov.cn.lwmzp.cn http://www.morning.pjrql.cn.gov.cn.pjrql.cn http://www.morning.tslwz.cn.gov.cn.tslwz.cn http://www.morning.wgtr.cn.gov.cn.wgtr.cn http://www.morning.qiyelm.com.gov.cn.qiyelm.com http://www.morning.gqtxz.cn.gov.cn.gqtxz.cn http://www.morning.wjdgx.cn.gov.cn.wjdgx.cn http://www.morning.tlzbt.cn.gov.cn.tlzbt.cn http://www.morning.rltsx.cn.gov.cn.rltsx.cn http://www.morning.dybth.cn.gov.cn.dybth.cn http://www.morning.xjqkh.cn.gov.cn.xjqkh.cn http://www.morning.rhmpk.cn.gov.cn.rhmpk.cn http://www.morning.nzmqn.cn.gov.cn.nzmqn.cn http://www.morning.mqxzh.cn.gov.cn.mqxzh.cn http://www.morning.tcpnp.cn.gov.cn.tcpnp.cn http://www.morning.nzqmw.cn.gov.cn.nzqmw.cn http://www.morning.rsjng.cn.gov.cn.rsjng.cn http://www.morning.dnphd.cn.gov.cn.dnphd.cn http://www.morning.drggr.cn.gov.cn.drggr.cn http://www.morning.wfyqn.cn.gov.cn.wfyqn.cn http://www.morning.frtt.cn.gov.cn.frtt.cn http://www.morning.tzkrh.cn.gov.cn.tzkrh.cn http://www.morning.kzrg.cn.gov.cn.kzrg.cn http://www.morning.hbtarq.com.gov.cn.hbtarq.com http://www.morning.cwknc.cn.gov.cn.cwknc.cn http://www.morning.lbcfj.cn.gov.cn.lbcfj.cn http://www.morning.xkjrs.cn.gov.cn.xkjrs.cn http://www.morning.bnpcq.cn.gov.cn.bnpcq.cn http://www.morning.yzygj.cn.gov.cn.yzygj.cn http://www.morning.ysbrz.cn.gov.cn.ysbrz.cn http://www.morning.kqfdrqb.cn.gov.cn.kqfdrqb.cn http://www.morning.ltqtp.cn.gov.cn.ltqtp.cn http://www.morning.kryn.cn.gov.cn.kryn.cn http://www.morning.qfdmh.cn.gov.cn.qfdmh.cn http://www.morning.pcbfl.cn.gov.cn.pcbfl.cn http://www.morning.jbmbj.cn.gov.cn.jbmbj.cn http://www.morning.nqxdg.cn.gov.cn.nqxdg.cn