vector.erase()之后的size()
时间: 2023-11-11 15:06:48 浏览: 335
在使用 `vector.erase()` 删除元素后,`vector` 的 `size()` 会减小,因为元素已经被删除了。具体来说,如果删除单个元素,则 `size()` 会减 1;如果删除多个元素,则 `size()` 会减去删除元素的个数。
例如,如果有一个 `vector<int> v`,初始时包含 5 个元素,且要删除第 2 个元素,则可以使用以下代码:
```c++
vector<int>::iterator it = v.begin() + 1; // 获取第 2 个元素的迭代器
v.erase(it); // 删除第 2 个元素
cout << v.size() << endl; // 输出 4
```
在上述代码中,`v.size()` 在删除元素后为 4。
需要注意的是,如果在删除元素时使用了迭代器,则删除元素后迭代器会失效,因此需要重新获取迭代器。如果在删除元素后继续使用失效的迭代器,则会导致不可预期的行为。
相关问题
Imagine you are writing a program to manage a shopping list. Using a vector to hold the shopping list items, write a print function to print out the contents of a vector of strings. Test your print function with a main program that does the following: Create an empty vector. Print it. Append the items, "eggs," "milk," "sugar," "chocolate," and "flour" to the list. Print it. Remove the last element from the vector. Print it. Append the item, "coffee" to the vector. Print it. Write a loop that searches for the item, "sugar" and replace it with "honey." Print the vector. Write a loop that searches for the item, "milk," and then remove it from the vector. Print the vector. Search for the item, "chocolate," and insert the item "tea" before it. Print the vector one more time.
Here is a possible implementation of the program:
```c++
#include <iostream>
#include <vector>
#include <algorithm> // for std::replace
void print_vector(const std::vector<std::string>& v) {
std::cout << "[";
for (size_t i = 0; i < v.size(); ++i) {
std::cout << v[i];
if (i < v.size() - 1) {
std::cout << ", ";
}
}
std::cout << "]" << std::endl;
}
int main() {
std::vector<std::string> shopping_list;
std::cout << "Empty list: ";
print_vector(shopping_list);
shopping_list.push_back("eggs");
shopping_list.push_back("milk");
shopping_list.push_back("sugar");
shopping_list.push_back("chocolate");
shopping_list.push_back("flour");
std::cout << "Initial list: ";
print_vector(shopping_list);
shopping_list.pop_back();
std::cout << "List after removing last item: ";
print_vector(shopping_list);
shopping_list.push_back("coffee");
std::cout << "List after adding coffee: ";
print_vector(shopping_list);
for (auto& item : shopping_list) {
if (item == "sugar") {
item = "honey";
}
}
std::cout << "List after replacing sugar with honey: ";
print_vector(shopping_list);
shopping_list.erase(std::remove(shopping_list.begin(), shopping_list.end(), "milk"), shopping_list.end());
std::cout << "List after removing milk: ";
print_vector(shopping_list);
auto it = std::find(shopping_list.begin(), shopping_list.end(), "chocolate");
if (it != shopping_list.end()) {
shopping_list.insert(it, "tea");
}
std::cout << "List after inserting tea before chocolate: ";
print_vector(shopping_list);
return 0;
}
```
The `print_vector` function takes a reference to a const vector of strings and prints its contents between square brackets, separated by commas and spaces, followed by a newline.
The `main` function creates an empty vector of strings called `shopping_list`, prints it using `print_vector`, appends the items using `push_back`, prints the list again, removes the last item using `pop_back`, prints the list again, appends "coffee", prints the list again, replaces "sugar" with "honey" using a range-based for loop and `std::replace`, prints the list again, removes "milk" using `std::find` and `std::erase`, prints the list again, inserts "tea" before "chocolate" using `std::find` and `std::insert`, and prints the list one last time.
The output of the program should be:
```
Empty list: []
Initial list: [eggs, milk, sugar, chocolate, flour]
List after removing last item: [eggs, milk, sugar, chocolate]
List after adding coffee: [eggs, milk, sugar, chocolate, coffee]
List after replacing sugar with honey: [eggs, milk, honey, chocolate, coffee]
List after removing milk: [eggs, honey, chocolate, coffee]
List after inserting tea before chocolate: [eggs, honey, tea, chocolate, coffee]
```
vector<vector<Segment>> segmentGroups; while (selectedSegments.size() > 0) { vector<Segment> group; group.push_back(selectedSegments[0]); selectedSegments.erase(selectedSegments.begin()); bool flag = true; while (flag) { flag = false; for (int i = 0; i < selectedSegments.size(); i++) { if (group.back().end == selectedSegments[i].start) { group.push_back(selectedSegments[i]); selectedSegments.erase(selectedSegments.begin() + i); flag = true; break; } } } segmentGroups.push_back(group); }这段代码什么意思/
这段代码是一个将一组线段按照相邻关系分组的过程。首先,我们有一个二维向量 `segmentGroups`,用来存储分组后的线段。然后,通过一个 `while` 循环来遍历 `selectedSegments`,直到没有剩余的线段为止。
在每次循环中,我们创建一个新的空向量 `group`,并将 `selectedSegments` 中的第一个线段添加到 `group` 中。然后,将该线段从 `selectedSegments` 中移除。
接下来,通过一个内部的 `while` 循环和一个 `flag` 标志来查找与 `group` 中最后一个线段相邻的线段。在内部循环中,我们遍历 `selectedSegments` 中的线段,如果找到与 `group` 中最后一个线段相邻的线段,则将其添加到 `group` 中,并从 `selectedSegments` 中移除。同时,将 `flag` 设置为 `true`,表示找到了相邻的线段。
当内部循环结束时,说明没有找到与 `group` 中最后一个线段相邻的线段,此时将 `group` 添加到 `segmentGroups` 中。
最终,循环会一直执行直到 `selectedSegments` 中没有剩余的线段。这样就完成了将线段按照相邻关系分组的过程。
阅读全文