- ubuntu12.04环境下使用kvm ioctl接口实现最简单的虚拟机
- Ubuntu 通过无线网络安装Ubuntu Server启动系统后连接无线网络的方法
- 在Ubuntu上搭建网桥的方法
- ubuntu 虚拟机上网方式及相关配置详解
CFSDN坚持开源创造价值,我们致力于搭建一个资源共享平台,让每一个IT人在这里找到属于你的精彩世界.
这篇CFSDN的博客文章通过代码实例解析c++ vector常用方法由作者收集整理,如果你对这篇文章有兴趣,记得点赞哟.
1. c++ vector 每个元素加上一个特定值 (c++ vector add a constant value for each element) 。
https://stackoverflow.com/questions/4461446/stl-way-to-add-a-constant-value-to-a-stdvector 。
1
2
3
4
5
|
vector<
int
> x = {0, 30, 80, 100, 120, 150, 190, 220, 250};
//transform可以将函数应用到序列的元素上,bind2nd通过绑定其中一个参数把二元函数转换成一元函数
transform(x.begin(), x.end(), x.begin(), bind2nd(plus<
int
>(), 1));
//显示x的值
copy(x.begin(), x.end(), ostream_iterator<
int
>(cout,
" "
));
|
结果: x = {1 31 81 101 121 151 191 221 251} 。
2. c++判断vector中是否存在某个元素(c++ judge whether an element exists in the vector) 。
https://www.techiedelight.com/check-vector-contains-given-element-cpp/ 。
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
|
#include <iostream>
#include <vector>
#include <algorithm>
int
main()
{
std::vector<
int
> v = { 4, 7, 5, 2, 6, 9 };
int
key = 6;
if
(std::count(v.begin(), v.end(), key))
std::cout <<
"Element found"
;
else
std::cout <<
"Element not found"
;
return
0;
}
|
结果显示:Element found 。
3. c++ vector<int> 生成指定个数的顺序列表 (c++ generate a sequential vector<int> of special numbers) 。
https://stackoverflow.com/questions/17694579/use-stdfill-to-populate-vector-with-increasing-numbers 。
std::vector<int> seq(10); // 定义在 numeric 头文件中的 iota() 函数模板会用连续的 T 类型值填充序列 std::iota(seq.begin(), seq.end(), 0),
结果: seq = {0, 1, 2, 3, 4, 5, 6, 7, 8, 9} 。
4. c++ 一条语句打印vector信息(c++ print out vector by one statement). 。
https://stackoverflow.com/questions/10750057/how-to-print-out-the-contents-of-a-vector 。
vector<int> x = {1, 2, 3, 4}; //istream_iterator用于从输入流中读取连续的元素 copy(x.begin(), x.end(), ostream_iterator<int>(cout, " ")),
结果显示: 1 2 3 4 。
5. c++ 得到vector<int>中元素的最大值和最小值以及最大值和最小值的索引位置 (c++ get the maximum and minimum values of the elements in vector<int> and the index positions ) 。
https://riptutorial.com/cplusplus/example/11151/find-max-and-min-element-and-respective-index-in-a-vector 。
1
2
3
4
5
6
7
8
9
10
11
12
13
|
vector<int> row_y = { 502, 263, 684, 324, 979 };
// 最大值索引和最大值
int row_y_max_index = max_element(row_y.begin(), row_y.end()) - row_y.begin();
cout <<
"row_y_max_index = "
<< row_y_max_index << endl;
int row_y_max_value = *max_element(row_y.begin(), row_y.end());
cout <<
"row_y_max_value = "
<< row_y_max_value << endl;
// 最小值索引和最小值
int row_y_min_index = min_element(row_y.begin(), row_y.end()) - row_y.begin();
cout <<
"row_y_min_index = "
<< row_y_min_index << endl;
int row_y_min_value = *min_element(row_y.begin(), row_y.end());
cout <<
"row_y_min_value = "
<< row_y_min_value << endl;
|
结果返回:
row_y_max_index = 4 row_y_max_value = 979 row_y_min_index = 1 row_y_min_value = 263 。
6. c++ vector 相加两个vector (c++ append a vector to vector) 。
https://stackoverflow.com/questions/2551775/appending-a-vector-to-a-vector 。
vector<int> x = {0, 30, 80, 100, 120, 150, 190, 220, 250}; vector<int> y = {100}; y.insert(y.end(), x.begin(), x.end()),
结果:y = {100, 0, 30, 80, 100, 120, 150, 190, 220, 250} 。
7. c++ 复制vector(c++ copy vector) 。
https://www.geeksforgeeks.org/ways-copy-vector-c/ 。
vector<int> x = {0, 30, 80, 100, 120, 150, 190, 220, 250}; vector<int> y; y.assign(x.begin(), x.end()),
结果:y = {0, 30, 80, 100, 120, 150, 190, 220, 250} 。
8. c++ vector 根据给定索引删除元素(c++ vector delete element based on a given index) 。
https://iq.opengenus.org/ways-to-remove-elements-from-vector-cpp/ 。
若想要删除第2个索引值和到第5个索引值,则可以使用下以语句:
vector<int> x = {0, 30, 80, 150, 120, 150, 30, 220, 80}; //remove(x.begin(), x.end(), 80); x.erase(x.begin() + 2, x.begin() + 5 + 1),
结果: x = {0, 30, 30, 220, 80} 。
9. c++ 删除vector所有指定元素(c++ delete all specified elements in the vector) 。
https://www.techiedelight.com/erase-elements-vector-cpp/ 。
vector<int> x = {0, 30, 150, 30, 220, 80}; //vector中的remove的作用是将等于value的元素放到vector的尾部,但并不减少vector的size //vector中erase的作用是删除掉某个位置position或一段区域(begin, end)中的元素,减少其size x.erase(remove(x.begin(), x.end(), 30), x.end()),
结果: x = {0 150 220 80} 。
10. c++ 统计 vector 某个元素出现的次数 (C++ count the number of occurrences of an element in vector) 。
https://www.geeksforgeeks.org/std-count-cpp-stl/ 。
vector<int> x = { 0, 3, 5, 6, 3, 2, 3 }; int n = count(x.begin(), x.end(), 3),
结果:n = 3 。
以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持我.
原文链接:https://www.cnblogs.com/ttweixiao-IT-program/p/12840921.html 。
最后此篇关于通过代码实例解析c++ vector常用方法的文章就讲到这里了,如果你想了解更多关于通过代码实例解析c++ vector常用方法的内容请搜索CFSDN的文章或继续浏览相关文章,希望大家以后支持我的博客! 。
我有这个析构函数,它在运行时产生错误“vector 迭代器不可取消引用”。 gridMatrix 是一个 std::vector * > * > * > * > 我添加了 typename 和 typ
我有一个 vector 的 vector ,比方说 std::vector > my2dArray; 现在我想要一个 vector ,其中包含 my2dArray 中 vector 的大小。手动这看起
假设我有一些 vector :v1、v2、v3 假设我还有一个 vector 来保存这些 vList = {v1, v2, v3} 如果我同步了 (vList),这是否意味着 v1、v2 和 v3 也
我正在创建一个 char 的二维 vector 数组作为类变量,但我在将 vector 添加到 vector 数组中时遇到了麻烦。 我正在使用 C++ 11 标准运行 gcc。 我尝试使用 vecto
如何修改 Vec基于 Vec 中某项的信息没有对向量的不可变和可变引用? 我已尝试创建一个最小示例来演示我的特定问题。在我的真实代码中,Builder struct 已经是其他答案提出的中间结构。具体
这个问题在这里已经有了答案: What is the idiomatic Rust way to copy/clone a vector in a parameterized function? (
在我的程序中,我有一个整数 vector 的 vector 。现在我想从 vector 的 vector 中取出一个 vector 并在另一个 vector 容器中对其进行操作,但是我得到了错误...
我得到一个vector>数据由 OpenCV 提供。由于某些原因(例如偏移/缩放),我需要转换数据 Point至Point2f 。我怎样才能做到这一点? 例如: std::vector > conto
我有一个函数,该函数应使用来自字符串类型的给定 vector vector 中的某些元素初始化来自字符串类型的空 vector vector 。我的语法看起来像这样 std::vector> extr
我得到一个vector>数据由 OpenCV 提供。由于某些原因(例如偏移/缩放),我需要转换数据 Point至Point2f 。我怎样才能做到这一点? 例如: std::vector > conto
这里有很多类似的问题,但我没有真正找到任何可以特别回答我的问题的问题。 我有一个 vector 的 vector 作为类的属性。另一个属性是 bucket_count。我想将 vector 的 vec
如果我像这样创建一个 vector 的 vector : std::vector> myVectorOfVectors; 然后用一些东西填充它: std::vector myVector1; myVe
我正在用 C++ 编写自定义 vector 类。我对这样的代码有疑问: vector vec; vec.push_back(one); vec.push_back(two);
这是我发布的问题 c++ program for reading an unknown size csv file (filled only with floats) with constant (b
vector> a; for (int i=0;i v(i+1); iota(v.begin(),v.end(),1); a.push_back(v); } a.erase(a.beg
也许已经晚了,但我不明白为什么我会得到一个超出此代码范围的 vector 下标: int m = 3; int n = 2; std::vector> path(m, std::vector(n, 0
这个问题真的很奇怪,我似乎找不到任何导致它的原因。 所以这里有一个赋值运算符重载函数,鸟类和哺乳动物都是 vector 。 (下面是类) const Register& Register::opera
我怎么去 std::vector> 只是 std::vector> ?有真正有效的方法吗? 最佳答案 我会做这样的事情: #include #include int main() { //
我正在尝试将这些 vector 中的一些数据写入文本文件。当我运行代码时,它返回运行时错误。 Category、Product、Cart、Customer和Address都是struct 包含每个 g
显然它会因您使用的编译器而异,但我很好奇执行 vector> 时的性能问题与 vector*> ,尤其是在 C++ 中。具体来说: 假设您的外部 vector 已满,您想要开始将元素插入到第一个内部
我是一名优秀的程序员,十分优秀!