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

做施工的平台网站运营seo是什么意思

做施工的平台网站,运营seo是什么意思,crm客户管理软件,部门门户网站建设请示Vector详解 原文链接:【超详细】C vector 详解 例题,这一篇就够了-CSDN博客 向量(Vector)是一个封装了动态大小数组的顺序容器(Sequence Container)。跟任意其它类型容器一样,它能够存放各种…

Vector详解

原文链接:【超详细】C++ vector 详解 + 例题,这一篇就够了-CSDN博客

向量(Vector)是一个封装了动态大小数组的顺序容器(Sequence Container)。跟任意其它类型容器一样,它能够存放各种类型的对象。可以简单的认为,向量是一个能够存放任意类型的动态数组

容器特性:

  1. 顺序序列:顺序容器中的元素按照严格的线性序列排列。可以通过元素在序列中的位置访问对应的元素。
  2. 动态数组:支持对序列中的任意元素进行快速直接访问,甚至可以通过指针算述进行该操作。提供了在序列末尾相对快速地添加/删除元素的操作。
  3. 能够感知内存分配器的:容器使用一个内存分配器对象来动态地处理它的存储需求。

vector数组初始化

以下全部使用int举例

1.定义空向量

vector<int> a;  //相当于空数组

2.定义具有10个整型元素的向量

vector<int> a(10); //相当于a[10]

3.定义具有10个整型元素的向量,且赋予每个元素初值为1

vector<int> a(10,1); //相当于a[10] = {1}

4.定义与向量b具有相同值的向量a

vector<int> a(b); //将向量b赋值给向量a,即向量a等于向量b

5.将向量b中下标0-1(共三个)的元素赋值给a

//第一个数据是起始地址,第二个数据是结束地址(不包括),第二个数据就是你要截取的长度加1
vector<int> a(b.begin(), b.begin()+3); 

6.从数组中获得初值

int b[7] = {1,2,3,4,5,6,7}; //定义整形数组vector<int> a(b, b+7); //将数组b赋值给a,第一个数据是起始地址,第二个数据是结束地址(不包括)

7.二维数组初值化

vector<vector<int>> a;  //初始化为int型二维数组,相当于int a[][]

vector对象常用内置函数

首先定义两个vector向量,进行下面函数演示:

vector<int> a, b;

1. assign()函数:可对已定义好的vector向量进行赋值

//将b的下标为0-2的元素赋值给向量a
a.assign(b.begin(), b.begin()+3);//使向量a变为长度为4且值为2
a.assign(4,2);

测试用例:

int main(){a.assign(5, 3);for(int i = 0; i < a.size(); i++){cout << a[i] << " ";}cout << endl;a.assign(4, 2);for(int i = 0; i < a.size(); i++){cout << a[i] << " ";}return 0;
} //输出:
3 3 3 3 3
2 2 2 2

2. back()函数

a.back();  //返回a的最后一个元素

3. front()函数

a.front();  //返回a的第一个元素

4.取向量a中第i个数据

a[i]; //返回a的第i元素,当且仅当a存在

5.clear()函数

a.clear(); //清空a中的元素

6. empty()函数

a.empty(); //判断向量a是否为空,若为空空则返回true,非空则返回false

7. push_back()函数

a.push_back(5); //在向量a的最后插入一个元素,其值为5

8.pop_back()函数

a.pop_back(); //删除a向量的最后一个元素

测试用例

int main(){a.push_back(1);a.push_back(2);a.push_back(3);a.push_back(4);a.push_back(5);cout << "使用push_back压入函数后:";for(int i = 0; i < a.size(); i++){cout << a[i] << " ";}cout << endl << endl;cout << "使用pop_back删除函数后:";a.pop_back(); a.pop_back();for(int i = 0; i < a.size(); i++){cout << a[i] << " ";}return 0;
} //输出:
使用push_back压入函数后:1 2 3 4 5
使用pop_back删除函数后:1 2 3

9. erase()函数

erase可以删去容器中指定位置的元素,容器的size(大小)会改变,但是容器的容量不变。

//删除a向量中全部元素
a.erase(a.begin(), a.end());//删除a向量中下标0-2共三个元素
a.erase(a.begin(), a.begin()+3);

测试程序

int main(){for(int i = 1; i < 5; i++) a.push_back(i); //输入数据cout << "使用erase删除前:" ; for(int i = 0; i < a.size(); i++){cout << a[i] << " ";}cout << endl;a.erase(a.begin(), a.begin() + 3);cout << "使用erase删除后:";for(int i = 0; i < a.size(); i++){cout << a[i] << " ";}return 0;
} //输出:
使用erase删除前:1 2 3 4 5
使用erase删除后:4 5

insert()函数

//在a向量第二个元素(下标从0开始)后插入 8
a.insert(a.begin()+2, 8);//在a向量的第二个元素(下标从0开始)后插入3个数,其值都为5
a.insert(a.begin()+1, 3, 8);//b为数组,在a的第一个元素(从第0个元素算起)的位置插入b的第三个元素到第5个元素(不包括b+6)
a.insert(a.begin()+1, b+3, b+6);

测试用例

void fun(){a.clear(); //清空a for(int i = 1; i <= 5; i++) a.push_back(i); //对a输入数据b.assign(10, 6); //对b输入数据 
}int main(){fun(); cout << "使用insert插入前:" ; for(int i = 0; i < a.size(); i++){cout << a[i] << " ";}cout << endl;a.insert(a.begin() + 2, 8);cout << "使用insert插入后:" ;for(int i = 0; i < a.size(); i++) cout << a[i] << " ";cout << endl << endl;fun(); cout << "使用insert插入前:" ; for(int i = 0; i < a.size(); i++){cout << a[i] << " ";}cout << endl;a.insert(a.begin() + 2, 3, 8);cout << "使用insert插入后:" ;for(int i = 0; i < a.size(); i++) cout << a[i] << " ";cout << endl << endl;fun(); cout << "使用insert插入前:" ; for(int i = 0; i < a.size(); i++){cout << a[i] << " ";}cout << endl;a.insert(a.begin() + 2, b.begin()+3, b.begin()+6);cout << "使用insert插入后:" ;for(int i = 0; i < a.size(); i++) cout << a[i] << " ";cout << endl;return 0;
} //输出:
使用insert插入前:1 2 3 4 5
使用insert插入后:1 2 8 3 4 5使用insert插入前:1 2 3 4 5
使用insert插入后:1 2 8 8 8 3 4 5使用insert插入前:1 2 3 4 5
使用insert插入后:1 2 6 6 6 3 4 5

11. size()

a.size(); //返回向量a中元素的个数

12. capacity()函数

capacity是指在发生realloc(动态分配)前能允许的最大元素数,即预分配的内存空间。

a.capacity(); //返回a在内存中总共可以容纳的元素个数

测试用例:

int main(){for(int i = 1; i <= 5; i++) a.push_back(i); //对a输入数据cout << "a向量值:" ; for(int i = 0; i < a.size(); i++){cout << a[i] << " ";}cout << endl;cout << "a.size = " << a.size() << endl;cout << "a.capacity = " << a.capacity();return 0;
} //输出
a向量值:1 2 3 4 5
a.size = 5
a.capacity = 8

13. resize()函数

//将a的现有元素个数调整至10个,多则删,少则补,其值随机
a.resize(10);//将a的现有元素个数调整至10个,多则删,少则补,其值为2
a.resize(10, 2);

测试用例:

void fun(){a.clear(); //清空a for(int i = 1; i <= 5; i++) a.push_back(i); //对a输入数据b.assign(10, 6); //对b输入数据 
}int main(){fun();cout << "使用resize前:" ; for(int i = 0; i < a.size(); i++){cout << a[i] << " ";}cout << endl << endl; a.resize(10);cout << "使用a.resize(10)后:";for(int i = 0; i < a.size(); i++){cout << a[i] << " ";}cout << endl << endl;fun(); a.resize(10, 2);cout << "使用a.resize(10, 2)后:";for(int i = 0; i < a.size(); i++){cout << a[i] << " ";}return 0;
} //输出:
使用resize前:1 2 3 4 5使用a.resize(10)后:1 2 3 4 5 0 0 0 0 0使用a.resize(10, 2)后:1 2 3 4 5 2 2 2 2 2

14.reserve()函数

a.reserve(100); //将a的容量扩充至100

15.swap()函数

a.swap(b); //b为向量,将a中的元素和b中的元素整体交换

16.两个向量比较

a == b; //相等
a > b; //大于
a >= b; 大于等于
a < b; //小于
a <= b; //小于等于
a != b; //不等于

几个常用的操作函数

头文件

#include<algorithm>

1. sort()函数

sort(a.begin(), a.end()); //对向量a进行从小到大排序

测试用例

int main(){for(int i = 5; i >= 0; i--) a.push_back(i); //对a输入数据cout << "使用sort前:" ; for(int i = 0; i < a.size(); i++){cout << a[i] << " ";}cout << endl << endl; sort(a.begin(), a.end());cout << "使用sort后:";for(int i = 0; i < a.size(); i++){cout << a[i] << " ";}return 0;
} 输出:
使用sort前:5 4 3 2 1 0
使用sort后:0 1 2 3 4 5

2.reverse()函数

//对a中的元素从a.begin()(包括它)到a.end()(不包括它)的元素倒置,但不排列
reverse(a.begin(), a.end()); //如a中元素为1,3,2,4,倒置后为4,2,3,1

测试用例

int main(){for(int i = 1; i <= 5; i++) a.push_back(i); //对a输入数据cout << "使用reverse前:" ; for(int i = 0; i < a.size(); i++){cout << a[i] << " ";}cout << endl << endl; reverse(a.begin(), a.end());cout << "使用reverse后:";for(int i = 0; i < a.size(); i++){cout << a[i] << " ";}return 0;
} 输出:
使用reverse前:1 2 3 4 5
使用reverse后:5 4 3 2 1

3. copy()函数

//把a中的从a.begin()(包括它)到a.end()(不包括它)的元素复制到b中,从b.begin()+1的位置(包括它)开始复制,覆盖掉原有元素
copy(a.begin(), a.end(), b.begin()+1);

测试用例:

int main(){for(int i = 1; i <= 5; i++) a.push_back(i); //对a输入数据b.assign(10, 6); //对b输入数据 cout << "使用copy前向量b为:" ; for(int i = 0; i < b.size(); i++){cout << b[i] << " ";}cout << endl << endl; copy(a.begin(), a.end(), b.begin()+2);cout << "使用copy后向量b为:";for(int i = 0; i < b.size(); i++){cout << b[i] << " ";}return 0;
} //输出:
使用copy前向量b为:6 6 6 6 6 6 6 6 6 6使用copy后向量b为:6 6 1 2 3 4 5 6 6 6

4.find()函数

//在a中的从a.begin()(包括它)到a.end()(不包括它)的元素中查找10,
//若存在返回其在向量中的下标,不存在则返回end(),即向量最后一个元素下标加一
find(a.begin(), a.end(), 4);

测试用例

int main(){for(int i = 1; i <= 5; i++) a.push_back(i); //对a输入数据cout << "使用find前:" ; for(int i = 0; i < a.size(); i++){cout << a[i] << " ";}cout << endl << endl; cout << "使用find后返回值index = ";cout << find(a.begin(), a.end(), 4) - a.begin() << endl;return 0;
}//输出:
使用find前:1 2 3 4 5使用find后返回值index = 3

遍历向量


1. 使用下标进行遍历

int main(){for(int i = 1; i <= 5; i++) a.push_back(i); //对a输入数据cout << "遍历向量a:" ; for(int i = 0; i < a.size(); i++){cout << a[i] << " ";}return 0;
} //输出:
遍历向量a:1 2 3 4 5

 

2. 通过迭代器进行遍历

int main(){for(int i = 1; i <= 5; i++) a.push_back(i); //对a输入数据cout << "遍历向量a:" ; for(vector<int>::iterator it = a.begin(); it != a.end(); it++){cout << *it << " ";}return 0;
} //输出:
遍历向量a:1 2 3 4 5

 

对向量添加元素的几种方式


1. 使用push_back() 函数

int main(){a.push_back(1);a.push_back(2);a.push_back(3);a.push_back(4);a.push_back(5);cout << "使用push_back压入函数后:";for(int i = 0; i < a.size(); i++){cout << a[i] << " ";}return 0;
} //输出:
使用push_back压入函数后:1 2 3 4 5

 

2. 从数组或现有向量中添加

int main(){int num[6] = {1, 2, 3, 4, 5, 6};for(int i = 0; i < 6; i++) a.push_back(num[i]);for(int i = 0; i < a.size(); i++){cout << a[i] << " ";} return 0;
} //输出:
遍历向量a:1 2 3 4 5

 

在赋值时一个常犯的错误与注意

1、在使用内置函数时,一定要注意第二个参数范围不包括end()。

2、下标只能用来获取已经存在的元素,so下面使用方法是错误的。

for(int i=0; i<10; i++){a[i] = i;    //应使用a.push_back(i)
}

 

http://www.dt0577.cn/news/44888.html

相关文章:

  • 网站搜索不出来关键词优化排名用哪些软件比较好
  • 县网站建设方案拼多多代运营公司十大排名
  • 平阳县城乡规划建设局网站网店推广常用的方法
  • php网站开发前景腾讯朋友圈广告代理
  • 创立公司最低多少钱seo排名外包
  • 什么好的网站学做食品青岛seo排名公司
  • 做的比较唯美的网站有哪些广东seo网站推广
  • 怎样建立自己的视频号关键词seo排名优化如何
  • 广州做网络服装的网站建设竞价软件哪个好
  • 西安杰商网络网站建设淘宝联盟怎么推广
  • 做网站找谁好关键词优化软件
  • 在跨境网站贸易公司做怎么样竞价排名是按照什么来计费的
  • 哈尔滨网站开发联系薇深圳搜索优化排名
  • 中国品牌网站建设培训机构网站设计
  • 做图片带字的网站怎么在百度发布个人简介
  • 昆明网站建设-中国互联培训机构怎么找
  • 西安网站建设培训中心网络推广赚钱平台有哪些
  • 做的网站上传到服务器吗seo整站优化多少钱
  • 零售app开发公司新乡seo推广
  • 菠菜源码怎么做网站网络服务商在哪咨询
  • 做网站需要注册商标是几类网络营销策划师
  • 如何建设网站兴田德润可信赖产品软文是什么
  • 网站做弹窗拉新app推广接单平台
  • icp ip 网站备案百度小说搜索热度排行榜
  • 学做网站的书哪些好网络营销策划模板
  • 视差滚动 网站网络公司起名
  • wordpress 相关文章 分页seo软件安卓版
  • 做视频网站需要流量全网最低价24小时自助下单平台
  • 企业网站维护外包常见的搜索引擎有哪些
  • 旅游电子商务网站建设技术规范百度广告推广平台