- ubuntu12.04环境下使用kvm ioctl接口实现最简单的虚拟机
- Ubuntu 通过无线网络安装Ubuntu Server启动系统后连接无线网络的方法
- 在Ubuntu上搭建网桥的方法
- ubuntu 虚拟机上网方式及相关配置详解
CFSDN坚持开源创造价值,我们致力于搭建一个资源共享平台,让每一个IT人在这里找到属于你的精彩世界.
这篇CFSDN的博客文章C++标准模板库vector的常用操作由作者收集整理,如果你对这篇文章有兴趣,记得点赞哟.
一:介绍 。
vector是C++标准模板库,是一个容器,底层是数组,为连续内存.
命名空间为std,所属头文件为<vector> 注意:不是<vector.h> 。
vector存储数据时,会分配一个存储空间,如果继续存储,该分配的空间已满,就会分配一块更大的内存,把原来的数据复制过来,继续存储,这些性能也会一定程度上会有损耗 。
二:常用操作 。
容量:
修改:
迭代器:
访问元素:
三:存储 。
简单存储 。
1
2
3
4
5
6
7
8
9
10
11
12
|
//存储方式1
vector<
int
> v1(10);
for
(
int
i=0; i<10; i++)
{
v1[i] = i;
}
//存储方式2
vector<
int
> v2;
for
(
int
i=0; i<10; i++)
{
v2.push_back(i);
}
|
存储结构体和结构体指针 。
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
|
struct
Student
{
char
name[32];
int
age;
};
//存储结构体
vector<Student> vStu1;
for
(
int
i=0; i<10; i++)
{
Student stu;
strcpy
(stu.name,
"woniu201"
);
stu.age = 30 + i;
vStu1.push_back(stu);
}
//存储结构体指针
vector<Student*> vStu2;
for
(
int
i=0; i<10; i++)
{
Student* pStu = (Student*)
malloc
(
sizeof
(Student));
strcpy
(pStu->name,
"woniu201"
);
pStu->age = 30 + i;
vStu2.push_back(pStu);
}
|
四:vector遍历 。
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
|
vector<
int
> v;
for
(
int
i=0; i<100; i++)
{
v.push_back(i);
}
//遍历方式1
for
(
int
i=0; i<100; i++)
{
int
& a = v[i];
printf
(
"%d "
, a);
}
//遍历方式2
for
(vector<
int
>::iterator it = v.begin(); it != v.end(); it++)
{
int
&a = *it;
printf
(
"%d "
, a);
}
|
五:排序 。
对vector整形进行排序 。
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
|
#include "stdlib.h"
#include <vector>
#include <algorithm>
using
namespace
std;
//升序比较函数
int
compare1(
const
int
&a,
const
int
&b)
{
return
a < b;
}
//降序比较函数
int
compare2(
const
int
&a,
const
int
&b)
{
return
a > b;
}
int
main()
{
vector<
int
> v;
for
(
int
i=0; i<10; i++)
{
v.push_back(
rand
() % 10);
}
//遍历输出
printf
(
"排序前数据:"
);
for
(vector<
int
>::iterator it = v.begin(); it != v.end(); it++)
{
printf
(
"%d "
, *it);
}
//升序排序
sort(v.begin(), v.end(), compare1);
//遍历输出
printf
(
"\n升序后数据:"
);
for
(vector<
int
>::iterator it = v.begin(); it != v.end(); it++)
{
printf
(
"%d "
, *it);
}
//降序排序
sort(v.begin(), v.end(), greater<
int
>());
//遍历输出
printf
(
"\n降序后数据:"
);
for
(vector<
int
>::iterator it = v.begin(); it != v.end(); it++)
{
printf
(
"%d "
, *it);
}
getchar
();
return
1;
}
|
对存放类成员变量排序 。
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
|
#include <string>
#include <vector>
#include <algorithm>
using
namespace
std;
class
Student {
public
:
Student(string n,
int
c) :name(n), core(c) {}
string name;
int
core;
};
//升序比较函数
bool
compare1(
const
Student& s1,
const
Student& s2)
{
return
s1.core < s2.core;
}
//降序比较函数
bool
compare2(
const
Student& s1,
const
Student& s2)
{
return
s1.core > s2.core;
}
int
main()
{
vector<Student> v;
Student s1(
"aaaa"
, 97);
Student s2(
"bbbb"
, 99);
Student s3(
"cccc"
, 95);
v.push_back(s1);
v.push_back(s2);
v.push_back(s3);
printf
(
"排序前数据:\n"
);
for
(vector<Student>::iterator it = v.begin(); it != v.end(); it++)
{
printf
(
"%s; %d\n"
, ((*it).name).c_str(), (*it).core);
}
//升序排序
sort(v.begin(), v.end(), compare1);
printf
(
"\n升序后的数据:\n"
);
for
(vector<Student>::iterator it = v.begin(); it != v.end(); it++)
{
printf
(
"%s; %d\n"
, ((*it).name).c_str(), (*it).core);
}
//降序排序
sort(v.begin(), v.end(), compare2);
printf
(
"\n降序后的数据:\n"
);
for
(vector<Student>::iterator it = v.begin(); it != v.end(); it++)
{
printf
(
"%s; %d\n"
, ((*it).name).c_str(), (*it).core);
}
getchar
();
return
1;
}
|
六:查找 。
1
2
3
4
5
6
7
8
9
|
vector<
int
>::iterator it = find(v.begin(), v.end(), 5);
if
(it != v.end())
{
cout <<
"found"
;
}
else
{
cout <<
"not found"
;
}
|
七:删除 。
1
2
3
4
5
6
7
8
|
for
(vector<
int
>::iterator it=v.begin(); it != v.end(); it++)
{
if
(*it == 8)
{
it = v.erase(it);
//it会++一次
it--;
//删除完后需要--,否则最终循环越界
}
}
|
八:释放内存 。
存放整形vector释放 。
1
2
3
4
5
6
7
8
9
10
11
|
//存放整型
vector<
int
> v;
for
(
int
i=0; i<100; i++)
{
v.push_back(i);
}
//释放内存
{
vector<
int
> vEmpty;
v.swap(vEmpty);
}
|
存放结构体vector释放 。
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
|
//存储结构体
vector<Student> vStu1;
for
(
int
i=0; i<10; i++)
{
Student stu;
strcpy
(stu.name,
"woniu201"
);
stu.age = 30 + i;
vStu1.push_back(stu);
}
//释放内存
{
vector<Student>
}
vector<Student> vEmpty;
vStu1.swap(vEmpty);
|
存放结构体指针vector释放 。
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
|
//存储结构体指针
vector<Student*> vStu2;
for
(
int
i=0; i<10; i++)
{
Student* pStu = (Student*)
malloc
(
sizeof
(Student));
strcpy
(pStu->name,
"wangpengfei"
);
pStu->age = 30 + i;
vStu2.push_back(pStu);
}
//释放内存
for
(vector<Student*>::iterator it = vStu2.begin(); it != vStu2.end(); it++)
{
if
(NULL != *it)
{
delete
*it;
*it = NULL;
}
}
|
总结 。
以上就是这篇文章的全部内容了,希望本文的内容对大家的学习或者工作具有一定的参考学习价值,谢谢大家对我的支持。如果你想了解更多相关内容请查看下面相关链接 。
原文链接:https://blog.csdn.net/woniu211111/article/details/75530501 。
最后此篇关于C++标准模板库vector的常用操作的文章就讲到这里了,如果你想了解更多关于C++标准模板库vector的常用操作的内容请搜索CFSDN的文章或继续浏览相关文章,希望大家以后支持我的博客! 。
Java 库和 android 库有什么区别,各自有什么优点/缺点? 最佳答案 您可以在 Android 应用程序中包含标准 Java .jar 文件库。它们在 .apk 构建时被翻译成 Dalvik
所以,我现在的代码就像从 Java 层加载库(比如 liba.so),并在内部 liba.so 加载 libb.so。因此,如果我必须将所有库打包到 APK 中并将其安装在没有 root 访问权限的设
我想在我的系统中设置 LEDA 库。 我已经从以下链接下载了 LEDA 库 http://www.algorithmic-solutions.info/free/d5.php Instruct
我想用 autoconf 创建一个共享库。但是,我希望共享库具有“.so”扩展名,而不是以“lib”开头。基本上,我想制作一个加载 dlopen 的插件。 .是否有捷径可寻? 当我尝试使用 autoc
我需要在 Apps 脚本应用程序上修改 PDF。为此,我想使用 JS 库:PDF-LIB 我的代码: eval(UrlFetchApp.fetch("https://unpkg.com/pdf-lib
我正在构建一个使用以下 Boost header 的程序(我使用的是 Microsoft Visual C++ 10), #include #include #include #include
当我通过 cygwin 在 hadoop 上运行此命令时: $bin/hadoop jar hadoop-examples-*.jar grep input output 'dfs[a-z.]+' 我
我已经通过 vcpgk 成功安装了一个 C++ 库,名为:lmdb:x64-windows 我还安装了lmdb通过 Cabal 安装的 Haskell 绑定(bind)包 在尝试测试 lmdb 包时:
我该如何解决这个问题? 我刚刚将 javacv jar 文件复制到我的项目 Lib 文件夹下,但出现了这个错误! 我可以找到这个thread来自谷歌,但不幸的是,由于我国的谷歌限制政策,该页面无法打开
我有一个 Android 库项目 FooLib。 FooLib 引用 Android Context 之类的东西,但不需要任何资源文件(res/ 中的东西)所以我目前将其打包为供我的应用使用的 JAR
我正在开发一个 Android 应用程序(使用 Android Studio),它能够通过手势识别算法了解您正在进行的 Activity 。对于我使用 nickgillian ithub 帐户上可用的
关于从 .NET Framework 项目中引用 .NET Standard 类库的问题有很多类似的问题,其中 netstandard 库中的 NuGet 包依赖项不会流向 netframework
我已经从互联网上下载了 jna-4.2.2.jar,现在想将这个 jar 导入到我的项目中。但是当我试图将这个 jar 导入我的项目时,出现以下错误。 [2016-06-20 09:35:01 - F
我正在尝试通过编译在 Mac 上安装 rsync 3.2.3。但是,我想安装所有功能。为此,它需要一些库,此处 ( https://download.samba.org/pub/rsync/INSTA
进入 Web 开发有点困难。过去 5 年我一直致力于 winforms 工作。所以我正在努力从一种切换到另一种。前段时间,我使用过 JavaScript,但现在还没有大量的 JavaScript 库
很难说出这里要问什么。这个问题模棱两可、含糊不清、不完整、过于宽泛或夸夸其谈,无法以目前的形式得到合理的回答。如需帮助澄清此问题以便重新打开,visit the help center . 关闭 1
我正在寻找一个用Python编写的与logstash(ruby + java)类似的工具/库。 我的目标是: 从 syslog 中解析所有系统日志 解析应用程序特定日志(apache、django、m
就目前情况而言,这个问题不太适合我们的问答形式。我们希望答案得到事实、引用资料或专业知识的支持,但这个问题可能会引发辩论、争论、民意调查或扩展讨论。如果您觉得这个问题可以改进并可能重新开放,visit
我花了几天时间试图寻找用于 JavaPOS 实现的 .jar 库,但我找不到任何可以工作的东西。我找到了很多像这样的文档:http://jpos.1045706.n5.nabble.com/file/
这个问题在这里已经有了答案: Merge multiple .so shared libraries (2 个答案) 关闭 9 年前。 我有我在代码中使用的第三方库的源代码和对象。该库附带有关如何使
我是一名优秀的程序员,十分优秀!