数组中插入元素(C++)
来源:互联网 发布:知乎北京五中大厂分校 编辑:程序博客网 时间:2024/06/03 17:41
用静态数组太麻烦,动态的相对来说跟方便
int main(){ vector <int >num(6,9);//产生6个9 for (int i = 0; i < num.size(); i++) { cout << num[i] << " "; } cout << endl; vector<int>::iterator it; it = num.begin()+2; num.insert(it, 11); // {9,9,11,9,9,9,9} for (int i = 0; i < num.size(); i++) { cout << num[i] << " "; } return 0;}//insert有很多重载方法:#include <iostream>#include <vector>int foofun(int v) { return v;}int main (){ std::vector<int> foo1(3,9); std::vector<int> foo2(3,9); std::vector<int> foo3(3,9); std::vector<int> foo4(3,9); std::vector<int> foo5(3,9); std::vector<int>::iterator it; // single element (1) it = foo1.begin(); foo1.insert(it,11); // {11,9,9,9} // fill (2) it = foo2.begin(); foo2.insert(it,2,7); // {7,7,9,9,9} // range (3) int fooarray[] = {1,2,3,4,5}; it = foo3.begin(); foo3.insert(it,fooarray + 1,fooarray + 4); // {2,3,4,9,9,9} // move (4) it = foo4.begin(); foo4.insert(it,foofun(5)); // {5,9,9,9} // initializer list (5) it = foo5.begin(); foo5.insert(it,{1,2,3}); // {1,2,3,9,9,9} return 0;}
#python的话。。。。。。#人生苦短,就用pythonnum=[0,1,2,3,4]print(num)#[0,1,2,3,4]num.append(5)#末尾添加print(num)#[0,1,2,3,4,5]num.insert(1,20)#也可以把元素插入到指定的位置,比如索引号为1的位置print(num)#[0,20,1,2,3,4,5]num.pop()#删除list末尾的元素,用pop()方法print(num)#[0,20,1,2,3,4]num.pop(1)#删除指定位置的元素,用pop(i)方法,其中i是索引位置print(num)#[0,1,2,3,4]
0 0
- 数组中插入元素(C++)
- c语言在降序数组中插入元素依然降序
- 向数组中插入元素
- 向数组中插入元素
- js中数组插入、删除元素操作
- 在数组中插入新的元素
- 5、向数组中插入一个元素
- java向数组中插入元素
- 向数组中插入元素排序
- c语言心得-----数组中对元素的操作排序,查找,插入,和删除
- C 已排序的数组中一个元素,插入后排序不变
- 不使用数组扩容向数组中插入元素
- 13周(数组插入元素)
- 有序数组 插入元素
- 数组元素的插入
- 数组-插入元素
- 第13周项目1(5)向数组中插入某一元素
- MongoDB&C++开发 (六)使用bsoncxx::builder::stream和$push在数组中插入元素
- 事件驱动IO-udp
- vue-router使用
- POJ 1273 Drainage Ditche(最大流模板题)
- CefSharp 屏蔽右键菜单
- redhat中通过nano创建用户
- 数组中插入元素(C++)
- 自定义标签的初步入门
- 项目整体管理·六
- 堆排序算法java实现
- php命名规范
- 虚拟机类加载机制小结
- linux 表示当前目录下所有文件的命令
- 前置机
- 权限管理命令 chmod