STD::FrcToMin插入器C++

标准:前插入器 构造一个前端插入迭代器,在应用它的容器前端插入新元素。它在头文件中定义 .

null

前插入迭代器是一种特殊类型的 输出迭代器 设计用于允许通常覆盖元素(例如复制)的算法在容器的开头自动插入新元素。这与 背部插入器。

语法:

std::front_inserter (Container& x);

x: Container in which new elements will 
be inserted at the beginning.

Returns: A front_insert_iterator that inserts 
elements at the beginning of container x.

// C++ program to demonstrate std::front_inserter
#include <iostream>
#include <iterator>
#include <deque>
#include <algorithm>
using namespace std;
int main()
{
// Declaring first container
deque< int > v1 = { 1, 2, 3 };
// Declaring second container for
// copying values
deque< int > v2 = { 4, 5, 6 };
// Using std::front_inserter inside std::copy
std::copy(v1.begin(), v1.end(), std::front_inserter(v2));
// v2 now contains 3 2 1 4 5 6
// Displaying v1 and v2
cout << "v1 = " ;
int i;
for (i = 0; i < 3; ++i) {
cout << v1[i] << " " ;
}
cout << "v2 = " ;
for (i = 0; i < 6; ++i) {
cout << v2[i] << " " ;
}
return 0;
}


输出:

v1 = 1 2 3
v2 = 3 2 1 4 5 6 

它有什么帮助?

  • 反转容器: 现在,因为std::front_inserter在容器的开头插入新元素,所以我们可以执行 在copy()的帮助下反向复制()的任务 ,这样我们将创建另一个容器,其中包含与当前容器相反的内容。

    // C++ program to demonstrate std::front_inserter
    #include <iostream>
    #include <iterator>
    #include <deque>
    #include <algorithm>
    using namespace std;
    int main()
    {
    // Declaring first container
    deque< int > v1 = { 1, 2, 3 };
    // Declaring second container
    // for storing the reverse
    deque< int > v2;
    // Using std::front_inserter inside std::copy
    std::copy(v1.begin(), v1.end(), std::front_inserter(v2));
    // v2 now contains 3 2 1
    // Displaying v1 and v2
    cout << "v1 = " ;
    int i;
    for (i = 0; i < 3; ++i) {
    cout << v1[i] << " " ;
    }
    cout << "v2 = " ;
    for (i = 0; i < 3; ++i) {
    cout << v2[i] << " " ;
    }
    return 0;
    }

    
    

    输出:

    v1 = 1 2 3
    v2 = 3 2 1
    

    说明: 在这里,我们开始将v1复制到v2,但从一开始,每当一个新元素出现时,它就被插入到开头,这样,插入的最后一个元素就成为新容器中的第一个元素,这样我们就能够反转容器中的内容。

要记住的要点:

  1. std::front_inserter的一个缺陷是它可以 仅用于那些将push_front作为其方法之一的容器 比如list和deque,而且 不能与向量一起使用。
  2. 推前()与前插入器() 现在,你可能会认为push_front()和front_inserter是相似的,但事实并非如此。当你必须在算法中传递一个迭代器时,你应该使用前面的插入器,就像上面的例子一样,而对于通常在容器开头插入值,可以使用push_front()。
  3. 代替使用std::front_插入器,我们可以 创建前插入迭代器 然后使用它,因为std::front_inserter最终只返回一个front_insert_迭代器。

    // C++ program to demonstrate front_insert_iterator
    #include <iostream>
    #include <iterator>
    #include <deque>
    #include <algorithm>
    using namespace std;
    int main()
    {
    // Declaring first container
    deque< int > v1 = { 1, 2, 3 };
    // Declaring second container for
    // copying values
    deque< int > v2 = { 4, 5, 6 };
    // Declaring a front_insert_iterator
    std::front_insert_iterator<std::deque< int > > front_i1(v2);
    // Using the iterator in the copy()
    std::copy(v1.begin(), v1.end(), front_i1);
    // v2 now contains 3 2 1 4 5 6
    // Displaying v1 and v2
    cout << "v1 = " ;
    int i;
    for (i = 0; i < 3; ++i) {
    cout << v1[i] << " " ;
    }
    cout << "v2 = " ;
    for (i = 0; i < 6; ++i) {
    cout << v2[i] << " " ;
    }
    return 0;
    }

    
    

    输出:

    v1 = 1 2 3
    v2 = 3 2 1 4 5 6
    

本文由 辛格先生 .如果你喜欢GeekSforgek,并想贡献自己的力量,你也可以使用 贡献极客。组织 或者把你的文章寄到contribute@geeksforgeeks.org.看到你的文章出现在Geeksforgeks主页上,并帮助其他极客。

如果您发现任何不正确的地方,或者您想分享有关上述主题的更多信息,请写下评论。

© 版权声明
THE END
喜欢就支持一下吧
点赞12 分享