📜  C++中的矢量擦除()和清除()

📅  最后修改于: 2021-05-30 02:43:18             🧑  作者: Mango

向量与动态数组相同,具有在插入或删除元素时自动调整自身大小的能力,并且容器自动处理其存储。

向量:: clear()

clear()函数用于删除向量容器的所有元素,从而使其大小为0。
句法 :

vectorname.clear()
Parameters :
No parameters are passed.
Result :
All the elements of the vector are
removed ( or destroyed )

例子:

Input  : myvector= {1, 2, 3, 4, 5};
         myvector.clear();
Output : myvector= {}

Input  : myvector= {};
         myvector.clear();
Output : myvector= {}

错误和异常
1.它没有异常抛出保证。
2.传递参数时显示错误。

CPP
// CPP program to illustrate
// Implementation of clear() function
#include 
#include 
using namespace std;
 
int main()
{
    vector myvector;
    myvector.push_back(1);
    myvector.push_back(2);
    myvector.push_back(3);
    myvector.push_back(4);
    myvector.push_back(5);
 
    // Vector becomes 1, 2, 3, 4, 5
 
    myvector.clear();
    // vector becomes empty
 
    // Printing the vector
    for (auto it = myvector.begin(); it != myvector.end(); ++it)
        cout << ' ' << *it;
    return 0;
}


CPP
// CPP program to illustrate
// working of erase() function
#include 
#include 
using namespace std;
 
int main()
{
    vector myvector{ 1, 2, 3, 4, 5 };
    vector::iterator it;
 
    it = myvector.begin();
    myvector.erase(it);
 
    // Printing the Vector
    for (auto it = myvector.begin(); it != myvector.end(); ++it)
        cout << ' ' << *it;
    return 0;
}


CPP
// CPP program to illustrate
// Implementation of erase() function
#include 
#include 
using namespace std;
 
int main()
{
    vector myvector{ 1, 2, 3, 4, 5 };
    vector::iterator it1, it2;
 
    it1 = myvector.begin();
    it2 = myvector.end();
    it2--;
    it2--;
 
    myvector.erase(it1, it2);
 
    // Printing the Vector
    for (auto it = myvector.begin(); it != myvector.end(); ++it)
        cout << ' ' << *it;
    return 0;
}


CPP
// CPP program to illustrate
// Application of erase() function
#include 
#include 
using namespace std;
 
int main()
{
    vector myvector{ 1, 2, 3, 4, 5, 6, 7, 8, 9 };
 
    for (auto i = myvector.begin(); i != myvector.end(); ++i) {
        if (*i % 2 == 0) {
            myvector.erase(i);
            i--;
        }
    }
 
    // Printing the vector
    for (auto it = myvector.begin(); it != myvector.end(); ++it)
        cout << ' ' << *it;
    return 0;
}


输出:

No Output

时间复杂度: O(N)
所有元素都被一一摧毁。

向量::: erase()

delete()函数用于从指定位置或范围中删除容器中的元素。
句法 :

1. vectorname.erase(position)
2. vectorname.erase(startingposition, endingposition)
Parameters :
Position of the element to be removed in the form of iterator.
or the range specified using start and end iterator.
Result :
Elements are removed from the specified
position of the container.

例子:

Input  : myvector= {1, 2, 3, 4, 5}, iterator= 2
         myvector.erase(iterator);
Output : 1, 2, 4, 5

Input  : myvector= {1, 2, 3, 4, 5, 6, 7, 8}, iterator1= 3, iterator2= 6
         myvector.erase(iterator1, iterator2);
Output : 1, 2, 3, 7, 8

错误和异常
1.如果该位置有效,则没有异常抛出保证。
2.否则显示未定义的行为。
特定位置删除元素

CPP

// CPP program to illustrate
// working of erase() function
#include 
#include 
using namespace std;
 
int main()
{
    vector myvector{ 1, 2, 3, 4, 5 };
    vector::iterator it;
 
    it = myvector.begin();
    myvector.erase(it);
 
    // Printing the Vector
    for (auto it = myvector.begin(); it != myvector.end(); ++it)
        cout << ' ' << *it;
    return 0;
}

输出:

2 3 4 5


删除范围内的元素:

CPP

// CPP program to illustrate
// Implementation of erase() function
#include 
#include 
using namespace std;
 
int main()
{
    vector myvector{ 1, 2, 3, 4, 5 };
    vector::iterator it1, it2;
 
    it1 = myvector.begin();
    it2 = myvector.end();
    it2--;
    it2--;
 
    myvector.erase(it1, it2);
 
    // Printing the Vector
    for (auto it = myvector.begin(); it != myvector.end(); ++it)
        cout << ' ' << *it;
    return 0;
}

输出:

4 5

应用
给定一个整数列表,从向量中删除所有偶数元素并打印该向量。

Input  :1, 2, 3, 4, 5, 6, 7, 8, 9
Output :1 3 5 7 9
Explanation - 2, 4, 6 and 8 which are even are erased from the vector

算法
1.运行一个循环,直到向量的大小。
2.检查每个位置的元素是否可被2整除,如果是,则删除该元素并递减迭代器。
3.打印最终向量。

CPP

// CPP program to illustrate
// Application of erase() function
#include 
#include 
using namespace std;
 
int main()
{
    vector myvector{ 1, 2, 3, 4, 5, 6, 7, 8, 9 };
 
    for (auto i = myvector.begin(); i != myvector.end(); ++i) {
        if (*i % 2 == 0) {
            myvector.erase(i);
            i--;
        }
    }
 
    // Printing the vector
    for (auto it = myvector.begin(); it != myvector.end(); ++it)
        cout << ' ' << *it;
    return 0;
}

输出:

1 3 5 7 9

时间复杂度:最坏的情况是O(N 2 ),因为擦除需要线性时间。

clear()vs delete(),什么时候使用什么?

clear()从向量容器中删除所有元素,从而使其大小为0。使用clear()函数删除向量中的所有元素。
另一方面, delete()函数用于从容器中删除特定元素或从容器中删除一系列元素,从而通过删除的元素数量来减小其大小。

要从最佳影片策划和实践问题去学习,检查了C++基础课程为基础,以先进的C++和C++ STL课程基础加上STL。要完成从学习语言到DS Algo等的更多准备工作,请参阅“完整面试准备课程”