📜  STL集C++中的插入和删除

📅  最后修改于: 2021-05-30 08:44:22             🧑  作者: Mango

集合是使用STL中的C++语言实现的容器,其概念类似于数学中定义集合的方式。将集合与其他容器分开的事实是它仅包含不同的元素,并且可以按排序顺序遍历元素。在竞争性编程和解决算法问题中,拥有牢固的集合很有用。本文讨论了STL集中的插入和删除。

插入

    使用insert() :插入函数用于将元素插入集合中。插入后,将对元素进行重新排序,并对集合进行排序。此函数以3种方式实现。

  • insert(ele) :此函数将元素插入集合中。插入只会发生时传递的元素是不是已经在集。它返回一个指针对。指向已经存在或新插入的元素的第一个元素。返回布尔状态为“ true”或“ false”的第二个元素。
  • insert(hint,ele) :在此实现中,提示指针与要插入的元素一起发送。提示指针的使用是为了帮助insert()知道实际的插入位置。因此,尝试减少分配元素的时间。提示指针不会在特定位置强制插入。此函数将指针返回到插入元素的位置
  • insert(beg_ptr,end_ptr) :这种插入类型是将其他容器的元素插入集合中所必需的。如果源容器中存在重复的元素,则不会将其插入。
// C++ code to demonstrate the working of insert()
#include
#include // for set operations
using namespace std;
  
int main()
{
    // declaring set
    set st;
  
    // declaring iterators
    set::iterator it = st.begin();
    set::iterator it1, it2;
  
    // declaring pair for return value of set containing
    // set iterator and bool
    pair< set::iterator,bool> ptr;
  
    // using insert() to insert single element
    // inserting 20
    ptr = st.insert(20);
  
    // checking if the element was already present or newly inserted
    if (ptr.second)
        cout << "The element was newly inserted" ;
    else cout  << "The element was already present" ;
  
    // printing set elements after insertion
    cout << "\nThe set elements after 1st insertion are : ";
    for (it1 = st.begin(); it1!=st.end();  ++it1)
        cout << *it1 << " ";
  
    // inserting set elements using hint
    st.insert(it, 24);
  
    // printing set elements after insertion
    cout << "\nThe set elements after 2nd insertion are : ";
    for (it1 = st.begin(); it1!=st.end();  ++it1)
        cout << *it1 << " ";
  
    // inserting array elements in set
    // 24 is not inserted again
    int arr[3] = { 25, 24, 26 };
    st.insert(arr, arr+3);
  
    // printing set elements after insertion
    cout << "\nThe set elements after 3rd insertion are : ";
    for (it1 = st.begin(); it1!=st.end();  ++it1)
        cout << *it1 << " ";
  
}

输出:

The element was newly inserted
The set elements after 1st insertion are : 20 
The set elements after 2nd insertion are : 20 24 
The set elements after 3rd insertion are : 20 24 25 26 

    使用emplace :emplace也用于将元素插入Set中。此函数类似于上面讨论的“ insert()”,唯一的区别是元素的“就地”构造发生在元素插入的位置,而不是复制或电影现有对象的insert()。

  • emplace() :使用就地构造策略插入元素。将set的大小增加1。返回一个指针对。第一个元素是迭代器,它指向插入元素的位置。 2nd返回一个布尔变量,指示一个已经存在或新创建的元素
  • emplace_hint() :使用“ hint_iterator”来获得插入位置的提示,以可能减少插入所插入元素所需的时间。这不会影响插入位置。它发生在内部定义的地方。
// C++ code to demonstrate the working of emplace()
// and emplace_hint()
#include
#include // for set operations
using namespace std;
  
int main()
{
    // declaring set
    set st;
  
    // declaring iterators
    set::iterator it = st.begin();
    set::iterator it1, it2;
  
    // declaring pair for return value of set containing
    // set iterator and bool
    pair< set::iterator,bool> ptr;
  
    // using emplace() to insert single element
    // inserting 24
    ptr = st.emplace(24);
  
    // checking if the element was already present or
    // newly inserted returns true. newly inserted
    if (ptr.second)
        cout << "The element was newly inserted" ;
    else cout  << "The element was already present" ;
  
    // printing set elements after insertion
    cout << "\nThe set elements after 1st insertion are : ";
    for (it1 = st.begin(); it1!=st.end();  ++it1)
        cout << *it1 << " ";
  
    // using emplace() to insert single element
    // inserting 24 // not inserted this time
    ptr = st.emplace(24);
  
    // checking if the element was already present or
    // newly inserted returns false. already inserted
    if (ptr.second)
        cout << "\nThe element was newly inserted" ;
    else cout  << "\nThe element was already present" ;
  
    // printing set elements after insertion
    cout << "\nThe set elements after 2nd insertion are : ";
    for (it1 = st.begin(); it1!=st.end();  ++it1)
        cout << *it1 << " ";
  
    // inserting set elements using hint
    st.emplace_hint(it,25);
  
    // printing set elements after insertion
    cout << "\nThe set elements after 3rd insertion are : ";
    for (it1 = st.begin(); it1!=st.end();  ++it1)
        cout << *it1 << " ";
}

输出:

The element was newly inserted
The set elements after 1st insertion are : 24 
The element was already present
The set elements after 2nd insertion are : 24 
The set elements after 3rd insertion are : 24 25 
删除中

    使用擦除() :擦除()用于擦除参数中提到的集合中的元素,包括其位置,其值或数字范围。

  • delete(num) :擦除其参数中提到的。删除后对集重新排序。
  • delete(iter) :擦除其参数中提到的迭代器指向的位置的值。
  • delete(strt_iter,end_iter) :擦除从“ strt_iter”到“ end_iter”的元素范围
// C++ code to demonstrate the working of erase()
#include
#include // for set operations
using namespace std;
  
int main()
{
    // declaring set
    set st;
  
    // declaring iterators
    set::iterator it;
    set::iterator it1;
    set::iterator it2;
  
    // declaring pair for return value of set containing
    // set iterator and bool
    pair< set::iterator,bool> ptr;
  
    // inserting values in set
    for (int i=1; i<10; i++)
        st.insert(i*5);
  
    // printing initial set elements
    cout << "The set elements after insertion are : ";
    for (it1 = st.begin(); it1!=st.end();  ++it1)
        cout << *it1 << " ";
  
    it = st.begin();
  
    cout << endl;
  
    // erasing element using iterator
    // erases 2nd element i.e., 10
    ++it;
    st.erase(it);
  
    // printing set elements after deletion
    cout << "The set elements after 1st deletion are : ";
    for (it1 = st.begin(); it1!=st.end();  ++it1)
        cout << *it1 << " ";
  
    // erasing element using value
    st.erase(40);
  
    // printing set elements after deletion
    cout << "\nThe set elements after 2nd deletion are : ";
    for (it1 = st.begin(); it1!=st.end();  ++it1)
        cout << *it1 << " ";
  
    ++it;
    ++it;
    ++it;
    ++it;
  
    // erasing element using range iterator
    // deletes 25 - last(45)
    st.erase(it, st.end());
  
    // printing set elements 3rd deletion
    cout << "\nThe set elements after 3rd deletion are : ";
    for (it1 = st.begin(); it1!=st.end();  ++it1)
        cout << *it1 << " ";
  
    cout << endl;
}

输出:

The set elements after insertion are : 5 10 15 20 25 30 35 40 45 
The set elements after 1st deletion are : 5 15 20 25 30 35 40 45 
The set elements after 2nd deletion are : 5 15 20 25 30 35 45 
The set elements after 3rd deletion are : 5 15 20 
要从最佳影片策划和实践问题去学习,检查了C++基础课程为基础,以先进的C++和C++ STL课程基础加上STL。要完成从学习语言到DS Algo等的更多准备工作,请参阅“完整面试准备课程”