C++ 找到不同向量中重复元素的最有效方法

C++ 找到不同向量中重复元素的最有效方法,c++,vector,compare,C++,Vector,Compare,我有一个字符串向量,它包含5个元素,称为瓶状DNotes。我试图在一个名为products的向量类中找到,该类中有一个名为productNames的字符串向量,每个产品的productNames中都有哪些瓶装饮料元素 理想情况下,我想根据重复性来整理我的结果。也就是说,如果瓶装饮料重复5次,那么就把产品还给我。这是我的第一个方法: vector <Products> ofApp::returnProductsSuggestions(vector<Products> &am

我有一个字符串向量,它包含5个元素,称为瓶状DNotes。我试图在一个名为products的向量类中找到,该类中有一个名为productNames的字符串向量,每个产品的productNames中都有哪些瓶装饮料元素

理想情况下,我想根据重复性来整理我的结果。也就是说,如果瓶装饮料重复5次,那么就把产品还给我。这是我的第一个方法:

vector <Products> ofApp::returnProductsSuggestions(vector<Products> &_notesProducts, vector<string> &_bottledNotes){

vector<Products> newProducts;

for( int i = 0; i < _notesProducts.size(); i++){
//        cout << "We made it into to the products" << endl;
 for( int j = 0; j < _notesProducts[i].productNotes.size(); j++){
//        cout << "We made it into to the productNotes" << endl;
    for ( int k = 0; k < _bottledNotes.size(); k++){

        if( five of the bottleNotes are repeated in the productNotes){

           newProducts.push_back(_notesProducts[i]);
        }

        if( four of the bottleNotes are repeated in the productNotes){

           newProducts.push_back(_notesProducts[i]);
        }

           //etc...

        if ( _notesProducts[i].productNotes[j] == _bottledNotes[k] && i == 0){// we chec in the first set of products
//               cout << "These are the notes that are included in the first product: " << _bottledNotes[k] << endl;                    
             break;                                 }               
        }            
    }        
}

 return _notesProducts;
}
应用程序的向量::返回产品建议(向量和注释产品、向量和瓶装注释){ 向量新积; 对于(int i=0;i<_notesProducts.size();i++){
//无法调查
std::set_intersection
std::map
不是更适合这个吗?字符串是名称,
int
将是计数。@PaulMkKenzie我得到了set_intersection的概念,我只是很难多次使用它。我发现的所有示例都显示了它wo矢量。还有,为什么映射容器会更好?注意ellaborate。谢谢!从概念上考虑。您希望存储字符串的多个副本,然后计算有多少个副本。适当的(或更好的)容器是一个
std::map
。原因是您只有一个字符串,并且附加到该字符串上,该字符串出现的次数计数。所有这些副本的向量都很笨重,可能不是最优的。@PaulMcKenzie非常感谢!现在这很有意义。我实际上能够找到我的答案了!