How to delete consecutive same words in a vector with C++

1 Answer

0 votes
#include <iostream>
#include <algorithm>
#include <vector>
   
void deleteConsecutiveDuplicates(std::vector<std::string> &vec) {
    vec.erase(std::unique(vec.begin(), vec.end()), vec.end());
}
    
int main() {
    std::vector<std::string> vec = {"c", "c", "c++", "c++", "java", "java", "java", "c++", "c", "c", "c"};
    
    deleteConsecutiveDuplicates(vec);

    for (const std::string &word : vec) {
        std::cout << word << " ";
    }
}
    
    
    
    
/*
run:
    
c c++ java c++ c 
     
*/

 



answered Aug 21, 2023 by avibootz
...