insert函数
语法:
1 2 3 4 5 6 7 |
1.插入位置,插入值 iterator insert(iterator __position, const value_type& __x); 2.插入位置,插入数量,插入值 void insert(iterator __position, size_type __n, const value_type& __x); 3.插入位置,迭代器开始位,迭代器结束位 template<typename _InputIterator> void insert(iterator __position, _InputIterator __first, _InputIterator __last) |
insert() 插入元素到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 47 48 49 50 51 52 53 54 |
/** * @brief Inserts given value into %vector before specified iterator. * @param position An iterator into the %vector. * @param x Data to be inserted. * @return An iterator that points to the inserted data. * * This function will insert a copy of the given value before * the specified location. Note that this kind of operation * could be expensive for a %vector and if it is frequently * used the user should consider using std::list. */ iterator insert(iterator __position, const value_type& __x); /** * @brief Inserts a number of copies of given data into the %vector. * @param position An iterator into the %vector. * @param n Number of elements to be inserted. * @param x Data to be inserted. * * This function will insert a specified number of copies of * the given data before the location specified by @a position. * * Note that this kind of operation could be expensive for a * %vector and if it is frequently used the user should * consider using std::list. */ void insert(iterator __position, size_type __n, const value_type& __x) { _M_fill_insert(__position, __n, __x); } /** * @brief Inserts a range into the %vector. * @param position An iterator into the %vector. * @param first An input iterator. * @param last An input iterator. * * This function will insert copies of the data in the range * [first,last) into the %vector before the location specified * by @a pos. * * Note that this kind of operation could be expensive for a * %vector and if it is frequently used the user should * consider using std::list. */ template<typename _InputIterator> void insert(iterator __position, _InputIterator __first, _InputIterator __last) { // Check whether it's an integral type. If so, it's not an iterator. typedef typename _Is_integer<_InputIterator>::_Integral _Integral; _M_insert_dispatch(__position, __first, __last, _Integral()); } |
测试代码
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 |
// 梁笔记 // https://zouzhongliang.com #include <iostream> #include <vector> using namespace std; int main() { vector<int> v1; vector<int> v2(3, 2); v1.push_back(1); v1.push_back(2); v1.push_back(3); v1.push_back(4); v1.push_back(5); for(int i=0;i<v1.size();++i){ cout<<v1[i]<<","; } cout<<endl; //iterator insert(iterator __position, const value_type& __x); v1.insert(v1.begin(),10); for(int i=0;i<v1.size();++i){ cout<<v1[i]<<","; } cout<<endl; //void insert(iterator __position, size_type __n, const value_type& __x); v1.insert(v1.begin(),3,20); for(int i=0;i<v1.size();++i){ cout<<v1[i]<<","; } cout<<endl; //template<typename _InputIterator> //void insert(iterator __position, _InputIterator __first, _InputIterator __last) v1.insert(v1.begin(),v2.begin(),v2.end()); for(int i=0;i<v1.size();++i){ cout<<v1[i]<<","; } cout<<endl; } |
测试结果:
1 2 3 4 |
1,2,3,4,5, 10,1,2,3,4,5, 20,20,20,10,1,2,3,4,5, 2,2,2,20,20,20,10,1,2,3,4,5, |