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

西安做网站收费价格宁波seo教程

西安做网站收费价格,宁波seo教程,电脑单页网站建设,onethink做移动网站目录 前言 Vector类 遍历与初始化vector ​vector的扩容机制 vector的对象操作 find与insert 对象数组 前言 string类中还有一些内容需要注意: STL 的string类怎么啦? C面试中string类的一种正确写法 C STL string的Copy-On-Write技术 C的st…

目录

前言

Vector类

遍历与初始化vector

​vector的扩容机制

vector的对象操作

find与insert

对象数组


前言

string类中还有一些内容需要注意:

STL 的string类怎么啦?

C++面试中string类的一种正确写法

C++ STL string的Copy-On-Write技术

C++的std::string的“读时也拷贝”技术!

Vector类

vector文档:cplusplus.com/reference/vector/vector/

基本概念:vector是表示可变数组(针对int、double等,字符数组去用string)的序列容器

类原型:template <class T,class Alloc = allocator<T> >class vector

class Alloc = allocator<T>(内存池)采用系统提供的默认的就行 

注意事项:[]、普通数组和at的区别在于[]利用断言检查不给改错机会,普通数组是抽查的给改错机会,at是异常捕获

以下几个表中的内容与string类中的内容相似,不再一一演示 

vector的定义

(constructor)构造函数声明

接口说明
vector() (重点)
无参构造
vector(size_type n, const value_type& val = value_type())
构造并初始化n个val
vector (const vector& x); (重点)
拷贝构造
vector (InputIterator first, InputIterator last)
使用迭代器进行初始化构造
vector iterator的使用
iterator的使用接口说明

begin +

end

获取第一个数据位置的iterator/const_iterator, 获取最后一个数据的下一个位置 的iterator/const_iterator

rbegin +

rend

获取最后一个数据位置的reverse_iterator,获取第一个数据前一个位置的 reverse_iterator
vector的增删改查
vector的增删改查接口说明

assign

覆盖

push_back

尾插

pop_back

尾删
find查找(不是vector的成员接口,使用时要包含algorithm头文件)

insert

在指定位置前插入数据

erase

删除指定位置数据

swap

交换两个vector的数据空间

clear

清空数据

operator[]

像数组一样访问

遍历与初始化vector

#include <iostream>
#include <vector>
using namespace std;int main()
{//初始化vector<int> v;v.push_back(1);v.push_back(2);v.push_back(3);v.push_back(4);//vector<int>(10,1):开始个空间每个空间给个1//[]重载for (size_t i = 0; i < v.size(); i++){cout << v[i] << "";}cout << endl;//迭代器vector<int>::iterator it = v.begin();while (it != v.end()){cout << *it << " ";it++;}cout << endl;//范围forfor (auto i : v){cout << i << " ";}cout << endl;return  0;
}

​vector的扩容机制

1、扩容1.5倍然后取整

#include <iostream>
#include <vector>
using namespace std;void test_vector2()
{size_t sz;vector<int> v;sz = v.capacity();cout << "making v grow:\n";for (int i = 0; i < 100; i++){v.push_back(i);if (sz != v.capacity()){sz = v.capacity();cout << "capacity change:" << sz << endl;}}
}int main()
{test_vector2();return  0;
}

2、resize和reserve不会缩容,要缩容用shrink_to_fit

#include <iostream>
#include <vector>
using namespace std;void test_vector2()
{size_t sz;vector<int> v;sz = v.capacity();v.reserve(100);//提前开空间cout << "making v grow:\n";for (int i = 0; i < 100; i++){v.push_back(i);if (sz != v.capacity()){sz = v.capacity();cout << "capacity change:" << sz << endl;}}cout << "-----------------------" << endl;cout << "reserve尝试对原空间进行缩容 >" << endl;cout <<"原size >" << v.size() << endl;cout <<"原capacity >" << v.capacity() << endl;v.reserve(10);cout << "后size >" << v.size() << endl;cout << "后capacity >" << v.capacity() << endl;cout << "-----------------------" << endl;cout << "resize尝试对原空间进行缩容 >" << endl;cout << "原size >" << v.size() << endl;cout << "原capacity >" << v.capacity() << endl;v.resize(10);cout << "后size >" << v.size() << endl;cout << "后capacity >" << v.capacity() << endl;cout << "-----------------------" << endl;cout << "原size >" << v.size() << endl;cout << "原capacity >" << v.capacity() << endl;cout << "shrink_to_fit尝试对原空间进行缩容 >" << endl;v.shrink_to_fit();cout << "后size >" << v.size() << endl;cout << "后capacity >" << v.capacity() << endl;}int main()
{test_vector2();return  0;
}

vector的对象操作

find与insert

#include <iostream>
#include <vector>
#include<string>
#include <algorithm>
using namespace std;//增删改查
void test_vector3()
{vector<int> v;v.push_back(1);v.push_back(2);v.push_back(3);v.push_back(4);v.push_back(5);cout << "初始化 >";for (auto e : v){cout << e << " ";}cout << endl;//找到数组中的3并返回该数据的下标auto pos = find(v.begin(), v.end(), 3);if (pos != v.end())//左闭右开才能遍历到数组所有的值{v.insert(pos,30);//在pos位置前插入30}cout << "在3前插入30后 >";for (auto e : v){cout << e << " ";}cout << endl;//头插v.insert(v.begin(), 0);cout << "头插0 >";for (auto e : v){cout << e << " ";}cout << endl;//在下标为2的位置插入v.insert(v.begin() + 2, 20);cout << "在下标为2的位置插入20 >";for (auto e : v){cout << e << " ";}cout << endl;string s("abcd");cout << "头插字符串 >";v.insert(v.begin(), s.begin(), s.end());for (auto e : v)//读取ASCII码{cout << e << " ";}cout << endl;}int main()
{test_vector3();return  0;
}

对象数组

push_back的原型:void push_back(const value_type& val)

  • value_type:自适应类型
#include <iostream>
#include <vector>
#include<string>
#include <algorithm>
using namespace std;//对象数组
void test_vector4()
{//对象数组vector<string> v;//版本一:有名对象string s1("苹果");v.push_back(s1);//版本二:匿名对象v.push_back(string("香蕉"));//版本三:隐式类型转换->产生临时对象->将该临时对象传入->val就是string类型对象的引用//隐式类型转换:字符串->字符数组->调用std::string(const char*)构造临时std::string对象,并将该对象传递给函数v.push_back("草莓");//版本一二三效果相同for (auto e : v){cout << e << " ";}cout << endl;}int main()
{test_vector4();return  0;
}

        在 Visual Studio 中使用 std::string,通常情况下,当字符串长度小于等于 28 字节时,std::string 对象会将字符串内容存储在对象内部的小缓冲区中。这个小缓冲区被称为 "短字符串优化"。这样可以避免频繁地进行堆分配和释放。对于超过 28 字节的较长字符串,则会动态分配内存来存储其内容,且 std::string 对象本身仅保存指向堆上数据的指针。

        如果有一个包含多个较长字符串的对象数组,并且只关心它们是一个对象数组而不需要了解每个具体元素所在位置或大小细节,那么您可以声明一个 std::vector<std::string> 来存储这些字符串。无论单个 std::string 的大小如何,在该向量中每个元素都将是一个完整的 std:string 对象。

vector是一个数组模板,除了有对象数组,还可以有链表数组、树数组、以及嵌套vector数组:

树数组:vector<tree> tc
链表数组:vector<list> lc
嵌套vector数组:vector<vector<int>> cc

  • 外部vector的T是vector<int>*,内部vector的T是int*
  • vector<int>*可以指向多个vector<int>,int*可以指向多个int
  • 外部vector实例化出了对象数组,内部vector实例化出了整型数组

~over~


文章转载自:
http://aeriform.c7623.cn
http://shooting.c7623.cn
http://observe.c7623.cn
http://defendable.c7623.cn
http://flanken.c7623.cn
http://esc.c7623.cn
http://kopis.c7623.cn
http://euglena.c7623.cn
http://modernist.c7623.cn
http://breezily.c7623.cn
http://daric.c7623.cn
http://scirrhous.c7623.cn
http://conjunctivitis.c7623.cn
http://nysa.c7623.cn
http://telefacsimile.c7623.cn
http://murrine.c7623.cn
http://benares.c7623.cn
http://queendom.c7623.cn
http://revamp.c7623.cn
http://untapped.c7623.cn
http://demote.c7623.cn
http://carnapper.c7623.cn
http://lothsome.c7623.cn
http://hypothermal.c7623.cn
http://hydriodic.c7623.cn
http://papilloma.c7623.cn
http://jallopy.c7623.cn
http://aphthong.c7623.cn
http://gallet.c7623.cn
http://fisherfolk.c7623.cn
http://avert.c7623.cn
http://hymn.c7623.cn
http://hirsutulous.c7623.cn
http://nymphomaniac.c7623.cn
http://simious.c7623.cn
http://recurvate.c7623.cn
http://tandemly.c7623.cn
http://gaseity.c7623.cn
http://peripeteia.c7623.cn
http://fortune.c7623.cn
http://perspiratory.c7623.cn
http://duumviri.c7623.cn
http://interviewer.c7623.cn
http://demark.c7623.cn
http://rhizomatic.c7623.cn
http://urbanology.c7623.cn
http://plectrum.c7623.cn
http://equanimously.c7623.cn
http://clotted.c7623.cn
http://biology.c7623.cn
http://longanimous.c7623.cn
http://tonicity.c7623.cn
http://capitalizer.c7623.cn
http://welterweight.c7623.cn
http://quito.c7623.cn
http://monophonematic.c7623.cn
http://nicely.c7623.cn
http://synesthesea.c7623.cn
http://drosky.c7623.cn
http://samlor.c7623.cn
http://globularity.c7623.cn
http://unimpeached.c7623.cn
http://apomixis.c7623.cn
http://testaceous.c7623.cn
http://inosite.c7623.cn
http://intervenor.c7623.cn
http://concoct.c7623.cn
http://malm.c7623.cn
http://abdiel.c7623.cn
http://terseness.c7623.cn
http://rhizogenic.c7623.cn
http://enunciate.c7623.cn
http://typographic.c7623.cn
http://dacoity.c7623.cn
http://metadata.c7623.cn
http://iconoduly.c7623.cn
http://canst.c7623.cn
http://micellization.c7623.cn
http://shammash.c7623.cn
http://grandducal.c7623.cn
http://corrasive.c7623.cn
http://earom.c7623.cn
http://vespine.c7623.cn
http://tilsit.c7623.cn
http://rentier.c7623.cn
http://clump.c7623.cn
http://lubricous.c7623.cn
http://unaesthetic.c7623.cn
http://exercitorial.c7623.cn
http://haunch.c7623.cn
http://provost.c7623.cn
http://toucan.c7623.cn
http://deoxygenize.c7623.cn
http://orpheus.c7623.cn
http://swatch.c7623.cn
http://bushfighter.c7623.cn
http://itinerary.c7623.cn
http://consilience.c7623.cn
http://deserved.c7623.cn
http://gondola.c7623.cn
http://www.zhongyajixie.com/news/95528.html

相关文章:

  • 西安的商城网站建设小红书推广
  • wow slider wordpress360优化大师安卓版下载
  • 用php做的网站视频外链平台
  • 长沙正规网站建设价格百度公司名称
  • 公司注册网上申请网站学设计什么培训机构好
  • 做网站的公司深圳南昌seo技术外包
  • 镇江房地产网站建设微信管理助手
  • 门诊部网站建设东莞seo收费
  • 学校网站设计方案模板在线seo超级外链工具
  • wordpress获取urlseo关键词工具
  • 北京 做网站广告网站策划方案
  • 做网站就是做信息整合推广图片制作
  • 网络公司如何开网站怎样做公司网站推广
  • 浅谈中兴电子商务网站建设公司网站seo外包
  • b2c电子商务网站的收益模式主要有湖南靠谱的关键词优化哪家好
  • 如何做网站计数器网络营销有哪些模式
  • 学习网页制作学什么中国优化网
  • 如何做下载网站赚钱吗北京建站
  • 紫色风格网站关键词排名是由什么决定的
  • 过年做哪个网站致富seo软件优化
  • 合肥 电子商务 网站建设seo营销
  • 加强政务公开网站建设班级优化大师客服电话
  • 广州番禺网站公司谷歌广告
  • 动态ip网站如何备案宁波网站推广平台效果好
  • 深圳有实力的网站建设服务商郑州seo排名第一
  • 沈阳商城网站建设武汉网络推广网络营销
  • 国内顶尖小程序开发公司宁波搜索引擎优化seo
  • 做网站学的什么专业百度服务热线
  • 西部数码支持wordpressseo优化关键词分类
  • 做网站建设的利润4001688688人工服务