通过代码实例解析c++ vector常用方法

 更新时间:2020年07月24日 09:14:57   作者:ttweixiao9999  
这篇文章主要介绍了通过代码实例解析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

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/

#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

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

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持脚本之家。

相关文章

  • C语言数组元素的循环移位方法

    C语言数组元素的循环移位方法

    今天小编就为大家分享一篇C语言数组元素的循环移位方法,具有很好的参考价值,希望对大家有所帮助。一起跟随小编过来看看吧
    2018-07-07
  • 一文让你不再害怕指针之C指针详解(经典,非常详细)

    一文让你不再害怕指针之C指针详解(经典,非常详细)

    这篇文章主要给大家介绍了C指针的相关资料,文中介绍的很经典,非常详细,文中通过示例代码介绍的非常详细,对大家学习或者使用C指针具有一定的参考学习价值,需要的朋友们下面来一起学习学习吧
    2019-08-08
  • C/C++实现发送与接收HTTP/S请求的示例代码

    C/C++实现发送与接收HTTP/S请求的示例代码

    HTTP(Hypertext Transfer Protocol)是一种用于传输超文本的协议,它是一种无状态的、应用层的协议,用于在计算机之间传输超文本文档,通常在 Web 浏览器和 Web 服务器之间进行数据通信,本文给大家介绍了C/C++发送与接收HTTP/S请求,需要的朋友可以参考下
    2023-11-11
  • Qt实现编辑框失去焦点隐藏功能

    Qt实现编辑框失去焦点隐藏功能

    这篇文章主要为大家详细介绍了Qt实现的一个简单的编辑框操作——主窗口失去焦点隐藏功能,文中的示例代码讲解详细,感兴趣的小伙伴可以了解一下
    2022-10-10
  • C++中的throw关键字详解

    C++中的throw关键字详解

    throw关键字是在C语言中用来抛出异常的关键字,它通常与try和catch一起使用,用于在程序中发生错误时进行异常处理,当遇到无法处理的错误情况时,我们可以使用throw关键字主动抛出异常,所以本文给大家详细的介绍一下C++中的throw关键字,需要的朋友可以参考下
    2023-09-09
  • C++基础知识实例解析(一)

    C++基础知识实例解析(一)

    这篇文章主要对C++基础知识实例解析,通过四个简短的案例,巩固大家的基础知识,需要的朋友可以参考下
    2015-08-08
  • C++中简单的文本文件输入/输出示例详解

    C++中简单的文本文件输入/输出示例详解

    C++程序把输入和输出看作字节流,输入时程序从输入流中抽取字节,输出时程序将字节插入到输出流中,下面这篇文章主要给大家介绍了关于C++中简单的文本文件输入/输出的相关资料,需要的朋友可以参考下
    2021-12-12
  • VisualStudio2019配置OpenCV4.5.0的方法示例

    VisualStudio2019配置OpenCV4.5.0的方法示例

    这篇文章主要介绍了VisualStudio2019配置OpenCV4.5.0的方法示例,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一起学习学习吧
    2021-03-03
  • C++实现简单扫雷游戏

    C++实现简单扫雷游戏

    这篇文章主要为大家详细介绍了C++实现简单扫雷游戏,文中示例代码介绍的非常详细,具有一定的参考价值,感兴趣的小伙伴们可以参考一下
    2020-03-03
  • 使用VSCode和VS2017编译调试STM32程序的实现

    使用VSCode和VS2017编译调试STM32程序的实现

    这篇文章主要介绍了使用VSCode和VS2017编译调试STM32程序的实现,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一起学习学习吧
    2020-05-05

最新评论