C++ STL emplace

C++ STL   emplace

iterator container::emplace(const_iterator pos,args)

●在容器的iterator位置pos上安插新元素,以args为初值

●返回新元素的位置

●此操作会引起当前的iterator失效

●始自C++11

●提供者:vector、deque、list

 

#include <iostream>
#include <vector>

using namespace std;

void test1()
{
    vector<int> vector_1;
    vector<int>::const_iterator const_vec_iter;
    vector<int>::iterator vec_iter;

    for (int k = 1; k < 7; ++k)
    {
        vector_1.push_back(k);
    }

    for(const_vec_iter =vector_1.begin(); const_vec_iter !=vector_1.end();++const_vec_iter)
    {
        int index_value = *const_vec_iter;
        //cout << index_value << endl;

        if (index_value==3)
        {
            //vector_1.emplace(const_vec_iter, 10);
            vector_1.insert(const_vec_iter, 10);
            ///vector_1.emplace_back(10);
       break;
} } for (vec_iter = vector_1.begin(); vec_iter != vector_1.end(); ++vec_iter) { int index_value = *vec_iter; cout << index_value << endl; } } int main() { test1(); system("pause"); return 0; }

 

输出结果

1
2
10
3
4
5
6

 

posted @ 2020-10-03 20:47  西北逍遥  阅读(241)  评论(0编辑  收藏  举报