qt c++ 去除QVector<QString>中重复的QString
时间: 2024-10-23 07:04:22 浏览: 94
在Qt C++中,如果你想要从`QVector<QString>`中去除重复的字符串,可以使用`QSet<QString>`或者通过自定义迭代器来实现。这里提供两种方法:
1. 使用`QSet<QString>`:
```cpp
QVector<QString> vectorWithDuplicates;
// ... (填充vector)
QSet<QString> uniqueStrings(vectorWithDuplicates.begin(), vectorWithDuplicates.end());
QVector<QString> uniqueVector(uniqueStrings.begin(), uniqueStrings.end()); // 由于QSet不允许重复元素,所以uniqueStrings就是去重后的集合
```
2. 自定义迭代器并添加到新的QVector:
```cpp
QVector<QString> vectorWithDuplicates;
// ... (填充vector)
QVector<QString> uniqueVector;
for (auto it = vectorWithDuplicates.begin(); it != vectorWithDuplicates.end(); ++it) {
if (std::find(uniqueVector.begin(), uniqueVector.end(), *it) == uniqueVector.end()) {
uniqueVector.push_back(*it);
}
}
```
阅读全文
相关推荐
![pdf](https://img-home.csdnimg.cn/images/20241231044930.png)
![zip](https://img-home.csdnimg.cn/images/20241231045053.png)
![rar](https://img-home.csdnimg.cn/images/20241231044955.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://img-home.csdnimg.cn/images/20241231044930.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)
![zip](https://img-home.csdnimg.cn/images/20241231045053.png)