去掉vector中指定的元素
时间: 2023-12-06 22:04:27 浏览: 39
可以使用C++ STL中的erase函数来去掉vector中指定的元素。具体操作如下:
假设要去掉vector中的第i个元素,可以使用以下代码:
```
vec.erase(vec.begin() + i);
```
其中,vec为vector变量名,i为要删除元素的下标。注意,vector的下标从0开始。
如果要删除多个元素,可以使用循环来实现:
```
for(int i = 0; i < index.size(); i++){
vec.erase(vec.begin() + index[i]);
}
```
其中,index为一个存储要删除元素下标的vector。循环中的代码会依次删除vector中存储在index中的元素。
相关问题
C++ vector去重
C++中对vector进行去重的方法有多种。其中一种方法是使用C++自带的remove函数。首先,需要包含头文件iostream、vector、algorithm和functional。然后,创建一个vector,并使用for循环将元素添加到vector中。接下来,使用remove函数将指定元素从vector中移除,再使用erase函数将移除的元素从vector中擦除。最后,使用for循环遍历vector并打印每个元素。
另一种去重方法是先对vector进行排序,然后使用unique函数去除重复元素,最后使用erase函数删除重复的元素。首先,需要包含头文件iostream、algorithm和vector。然后,创建一个int数组并将其转换为vector。接下来,使用sort函数对vector进行排序,然后使用unique函数去除重复元素,再使用erase函数删除重复的元素。最后,使用for循环遍历vector并打印每个元素。
还有一种方法是使用set的特性。首先,需要包含头文件iostream、vector和set。然后,创建一个int数组并将其转换为vector。接下来,使用set将vector中的元素去重,并将去重后的元素重新赋值给vector。最后,使用for循环遍历vector并打印每个元素。
但是没有去掉重复的元素
对于去重,我们需要使用unique函数,并且需要指定一个比较函数来判断两个元素是否相等。在这里,我们需要判断成绩相同的科目是否重复,如果重复则只保留一个。以下是修改后的代码:
```
bool compare_scores(const pair<string, string>& a, const pair<string, string>& b) {
double score_a = stod(a.second, nullptr);
double score_b = stod(b.second, nullptr);
if (isnan(score_a)) {
score_a = 0;
}
if (isnan(score_b)) {
score_b = 0;
}
return score_a > score_b;
}
bool compare_courses(const pair<string, string>& a, const pair<string, string>& b) {
return a.first == b.first && a.second == b.second;
}
void management::print_scores_sorted(const vector<course>& courses_for_student) {
vector<pair<string, string>> scores_vec;
for (const auto& c : courses_for_student) {
scores_vec.push_back(make_pair(c.m_coursename, c.m_score));
}
sort(scores_vec.begin(), scores_vec.end(), compare_scores);
scores_vec.erase(unique(scores_vec.begin(), scores_vec.end(), compare_courses), scores_vec.end());
for (const auto& s : scores_vec) {
cout << s.first << "\t" << s.second << endl;
}
}
```
在上面的代码中,我们定义了一个新的比较函数`compare_courses`,用于判断成绩相同的科目是否重复。然后在调用`unique`函数时,我们将这个比较函数作为第三个参数传递进去,以此来去除重复的元素。
相关推荐
![doc](https://img-home.csdnimg.cn/images/20210720083327.png)
![docx](https://img-home.csdnimg.cn/images/20210720083331.png)
![docx](https://img-home.csdnimg.cn/images/20210720083331.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)
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![application/msword](https://img-home.csdnimg.cn/images/20210720083327.png)