container.erase(container.begin() + i);详细解释这段代码里每个单词的意义及用法
时间: 2023-06-25 22:03:49 浏览: 49
这行代码的含义是从容器中删除指向第i个元素的迭代器。下面是每个单词的详细解释:
- container:表示待操作的容器,可以是vector、list、map等STL容器。
- erase:表示删除元素的操作。
- container.begin():返回指向容器起始位置的迭代器,即指向第一个元素的迭代器。
- i:表示待删除元素的位置,通常是一个整数。
- container.begin() + i:表示返回一个新的迭代器,指向第i个元素的位置。
- +:表示迭代器之间的加法,将一个迭代器加上一个整数,得到一个新的迭代器。
- ():表示函数调用的符号。
综上所述,这行代码的作用就是删除容器中第i个元素。
相关问题
container.erase(it, container.end()); container 是容器类的对象,it是迭代器,如果it包含了container.end(),container.end()会被删掉吗
使用 `erase` 函数删除 `[it, container.end())` 范围内的元素时,如果 `it` 恰好指向 `container.end()`,则它不指向任何元素,因此不会删掉 `container.end()`。
例如,如果要删除向量 `vec` 中从第三个元素开始到末尾的所有元素,可以这样写:
```c++
std::vector<int> vec = {1, 2, 3, 4, 5};
auto it = vec.begin() + 2; // 获取指向第三个元素的迭代器
vec.erase(it, vec.end()); // 删除第三个元素到末尾的所有元素
```
注意,这里使用 `begin()` 函数获取迭代器,然后通过加上偏移量 2 获取指向第三个元素的迭代器,再传入 `erase` 函数中删除第三个元素到末尾的所有元素。由于 `it` 不包含 `vec.end()`,因此 `vec.end()` 不会被删除。
#include <iostream> #include <string> #include <vector> using namespace std; struct RoutingEntry { string ip; string nextHop; }; class RoutingTable { public: void add (string ip, string nextHop) { RoutingEntry entry = { ip, nextHop }; container.push_back(entry); } void remove (string ip) { for (size_t i = 0; i < container.size(); i++) { if (ip == container[i].ip) { container.erase(container.begin() + i); break; } } } void modify (string ip, string nextHop) { for (size_t i = 0; i < container.size(); i++) { if (ip == container[i].ip) { container[i].nextHop = nextHop; break; } } } string search(string ip) { for (size_t i = 0; i < container.size(); i++) { if (ip == container[i].ip) { return container[i].nextHop; } } return "Not Found"; } private: vector <RoutingEntry> container; }; int main() { RoutingTable table; table.add("223.1.1.1", "200.23.16.1"); table.add("223.1.1.2", "200.23.16.2"); table.add("223.1.1.3", "200.23.16.3"); // 循环查找 while (true) { // 显示菜单 cout << "1. Lookup route" << endl; cout << "2. Add route" << endl; cout << "3. Remove route" << endl; cout << "4. Modify route" << endl; cout << "5. Quit" << endl; int choice; cout << "Please enter your choice: "; cin >> choice; if (choice == 1) { string ip; cout << "Please enter destination IP address: "; cin >> ip; string nextHop = table.search(ip); cout << "Next Hop: " << nextHop << endl; return 0; } else if (choice == 2) { string ip, nextHop; cout << "Please enter destination IP address to add: "; cin >> ip; cout << "Please enter next route address to add: "; cin >> nextHop; add(ip, nextHop); } else if (choice == 3) { string ip; cout << "Please enter destination IP address to remove: "; cin >> ip; remove(ip); } else if (choice == 4) { string ip, nextHop; cout << "Please enter destination IP address to modify: "; cin >> ip; cout << "Please enter next route address to modify: "; cin >> nextHop; modify(ip, nextHop); } else if (choice == 5) { break; } else { cout << "Invalid input, please re-enter" << endl; } } return 0; } 修改代码解决未定义标识符add,remove,modify的问题并作出解释
在主函数中,调用了add、remove、modify三个函数,但是这三个函数在RoutingTable类中定义,因此需要在调用它们之前加上"table."前缀,即使用table.add、table.remove、table.modify来调用这三个函数。
修改后的代码如下:
```
#include <iostream>
#include <string>
#include <vector>
using namespace std;
struct RoutingEntry {
string ip;
string nextHop;
};
class RoutingTable {
public:
void add(string ip, string nextHop) {
RoutingEntry entry = { ip, nextHop };
container.push_back(entry);
}
void remove(string ip) {
for (size_t i = 0; i < container.size(); i++) {
if (ip == container[i].ip) {
container.erase(container.begin() + i);
break;
}
}
}
void modify(string ip, string nextHop) {
for (size_t i = 0; i < container.size(); i++) {
if (ip == container[i].ip) {
container[i].nextHop = nextHop;
break;
}
}
}
string search(string ip) {
for (size_t i = 0; i < container.size(); i++) {
if (ip == container[i].ip) {
return container[i].nextHop;
}
}
return "Not Found";
}
private:
vector<RoutingEntry> container;
};
int main() {
RoutingTable table;
table.add("223.1.1.1", "200.23.16.1");
table.add("223.1.1.2", "200.23.16.2");
table.add("223.1.1.3", "200.23.16.3");
// 循环查找
while (true) {
// 显示菜单
cout << "1. Lookup route" << endl;
cout << "2. Add route" << endl;
cout << "3. Remove route" << endl;
cout << "4. Modify route" << endl;
cout << "5. Quit" << endl;
int choice;
cout << "Please enter your choice: ";
cin >> choice;
if (choice == 1) {
string ip;
cout << "Please enter destination IP address: ";
cin >> ip;
string nextHop = table.search(ip);
cout << "Next Hop: " << nextHop << endl;
}
else if (choice == 2) {
string ip, nextHop;
cout << "Please enter destination IP address to add: ";
cin >> ip;
cout << "Please enter next route address to add: ";
cin >> nextHop;
table.add(ip, nextHop); // 修改这里
}
else if (choice == 3) {
string ip;
cout << "Please enter destination IP address to remove: ";
cin >> ip;
table.remove(ip); // 修改这里
}
else if (choice == 4) {
string ip, nextHop;
cout << "Please enter destination IP address to modify: ";
cin >> ip;
cout << "Please enter next route address to modify: ";
cin >> nextHop;
table.modify(ip, nextHop); // 修改这里
}
else if (choice == 5) {
break;
}
else {
cout << "Invalid input, please re-enter" << endl;
}
}
return 0;
}
```
修改后,程序正常运行,可以进行路由表的增删改查操作。
相关推荐
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![doc](https://img-home.csdnimg.cn/images/20210720083327.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)