两个元素个数相同的容器 vector<string> delete_pathlists; vector<unsigned char> crosstypelists;想要移除crosstypelists中等于2的元素,同时移除delete_pathlists中相同位置的元素
时间: 2024-03-02 11:51:26 浏览: 12
可以使用 erase-remove idiom 来实现:
```
crosstypelists.erase(std::remove(crosstypelists.begin(), crosstypelists.end(), 2), crosstypelists.end());
delete_pathlists.erase(std::remove_if(delete_pathlists.begin(), delete_pathlists.end(),
[&, i = 0](const auto&) mutable { return crosstypelists.size() > i && crosstypelists[i++] == 2; }),
delete_pathlists.end());
```
这里使用了 lambda 表达式来捕获 `crosstypelists` 和 `&` 以传递引用。`i` 是一个可变的局部变量,用于跟踪 `crosstypelists` 的迭代器。`remove_if` 通过调用 lambda 表达式来判断是否需要移除对应位置的元素,如果需要,就将其移除。
需要注意的是,`erase` 和 `remove_if` 的顺序不能颠倒,否则会导致迭代器失效。
相关问题
两个元素个数相同的容器 vector<string> delete_pathlists; vector<unsigned char> crosstypelists;执行下面操作:移除delete_pathlists中所有等于string thepath_str的元素,和crosstypelists对应位置的元素
好的,针对你的问题,可以通过以下代码实现:
```
auto delete_it = delete_pathlists.begin();
auto crosstype_it = crosstypelists.begin();
while (delete_it != delete_pathlists.end()) {
if (*delete_it == "thepath_str") {
delete_it = delete_pathlists.erase(delete_it);
crosstype_it = crosstypelists.erase(crosstype_it);
} else {
++delete_it;
++crosstype_it;
}
}
```
这段代码会遍历 delete_pathlists 中的所有元素,如果当前元素等于 "thepath_str",就将其从 delete_pathlists 和 crosstypelists 中删除,并将对应的迭代器也更新。如果当前元素不等于 "thepath_str",就将两个迭代器都向前移动一位,继续检查下一个元素。
qt父窗口mainwindow有两个容器vector<string> delete_pathlists; vector<unsigned char> crosstypelists; 在子窗口deletewindow上显示出来,给出C++完整示例
好的,以下是一个简单的示例:
MainWindow.h:
```cpp
#ifndef MAINWINDOW_H
#define MAINWINDOW_H
#include <QMainWindow>
#include <vector>
#include <string>
namespace Ui {
class MainWindow;
}
class MainWindow : public QMainWindow
{
Q_OBJECT
public:
explicit MainWindow(QWidget *parent = nullptr);
~MainWindow();
private slots:
void on_deleteButton_clicked();
private:
Ui::MainWindow *ui;
std::vector<std::string> delete_pathlists;
std::vector<unsigned char> crosstypelists;
};
#endif // MAINWINDOW_H
```
MainWindow.cpp:
```cpp
#include "mainwindow.h"
#include "ui_mainwindow.h"
#include "deletewindow.h"
MainWindow::MainWindow(QWidget *parent) :
QMainWindow(parent),
ui(new Ui::MainWindow)
{
ui->setupUi(this);
// 添加一些示例数据
delete_pathlists.push_back("path1");
delete_pathlists.push_back("path2");
delete_pathlists.push_back("path3");
crosstypelists.push_back(1);
crosstypelists.push_back(2);
crosstypelists.push_back(3);
}
MainWindow::~MainWindow()
{
delete ui;
}
void MainWindow::on_deleteButton_clicked()
{
// 创建删除窗口并传递数据
DeleteWindow *deleteWindow = new DeleteWindow(this, delete_pathlists, crosstypelists);
deleteWindow->show();
}
```
DeleteWindow.h:
```cpp
#ifndef DELETEWINDOW_H
#define DELETEWINDOW_H
#include <QDialog>
#include <vector>
#include <string>
namespace Ui {
class DeleteWindow;
}
class DeleteWindow : public QDialog
{
Q_OBJECT
public:
explicit DeleteWindow(QWidget *parent = nullptr,
const std::vector<std::string>& delete_pathlists = std::vector<std::string>(),
const std::vector<unsigned char>& crosstypelists = std::vector<unsigned char>());
~DeleteWindow();
private:
Ui::DeleteWindow *ui;
};
#endif // DELETEWINDOW_H
```
DeleteWindow.cpp:
```cpp
#include "deletewindow.h"
#include "ui_deletewindow.h"
DeleteWindow::DeleteWindow(QWidget *parent,
const std::vector<std::string>& delete_pathlists,
const std::vector<unsigned char>& crosstypelists) :
QDialog(parent),
ui(new Ui::DeleteWindow)
{
ui->setupUi(this);
// 在窗口中显示数据
for (const auto& path : delete_pathlists) {
ui->pathList->addItem(QString::fromStdString(path));
}
for (const auto& type : crosstypelists) {
ui->typeList->addItem(QString::number(type));
}
}
DeleteWindow::~DeleteWindow()
{
delete ui;
}
```
在这个示例中,我们在 `MainWindow` 中创建了一个 `vector<string>` 和一个 `vector<unsigned char>`,然后在点击删除按钮时打开了一个 `DeleteWindow` 窗口并将这些数据传递给它,在 `DeleteWindow` 中我们可以将这些数据显示在两个列表中。