过滤函数中的矢量,并返回另一个带有过滤结果的矢量

filter vector in function and return another vector with the filtered results

本文关键字:过滤 结果 另一个 函数 返回      更新时间:2023-10-16

我正在做一个类项目,在完成之前,我被困在了最后一件需要做的事情上。。问题是,我需要为我的自定义类创建一个函数(将向量作为其元素之一),该函数过滤我类的向量,并返回另一个仅包含向量匹配项的向量,而这必须使用STL算法来完成,我知道我应该使用remove_if/copy_if,但似乎无法计算出我需要输入到STL过滤算法中的函数。这是我的代码:

#include <iostream>
#include <conio.h>
#include <cmath>
#include <string>
#include <vector>
#include <fstream>
#include <algorithm>
using namespace std;
class cityPhone {
private: 
    string cityName;
    string cityCode;
public:
 void setCode(string code){
    cityCode=code;
}
 void setName(string name){
    cityName=name;
}
 cityPhone(){
    cityName="Varna";
    cityCode="0888123123";
}
 cityPhone(string name, string code){
    cityName=name;
    cityCode=code;
}
 string getCity(){
return cityName;    
}
 string getCode(){
    return cityCode;
}
};
bool cmpCity(cityPhone a, cityPhone b)
        {   
            if (a.getCity().compare(b.getCity())>0)return false;
    return true;
        }   
bool cmpCode(cityPhone a, cityPhone b)
        {   
           if (a.getCode().compare(b.getCode())>0)return false;
    return true;
}
 class phoneDirectory {
 private :
     vector<cityPhone> data;
 public:
     phoneDirectory (string path){
        read(path); 
    }
     phoneDirectory (){
        data=vector<cityPhone>();   
    }
   void read(string path){
        cout<<endl;
        try {
string line;
  ifstream myfile (path);
  cityPhone bla = cityPhone();
  if (myfile.is_open())
  {
    while ( getline (myfile,line) )
    {
        try{
      bla = cityPhone(line.substr(0,line.find_first_of(" ")),line.substr(line.find_first_of(" ")+1));
      data.push_back(bla);
        }
        catch(exception){ }
    }
    myfile.close();
  }
  else cout << "Unable to open file"; 
        } catch (exception) {}
}
   void addCityPhone(string city,string phone){
       try{
   data.push_back(cityPhone(city,phone)); 
       }
       catch(exception){
       cout<<"Error adding item "<<endl;
       }
   }
   void delCityPhone(int index){
       try{
   vector<cityPhone>::iterator p=data.begin();
   p+=index;
   data.erase(p);
       } 
       catch(exception){
       cout<<"Error deleting item with index "+index<<endl;
       }
   }
      cityPhone getCityPhone(unsigned index){
       try{
   vector<cityPhone>::iterator p=data.begin();
   p+=index;
   return *p;
       }
       catch(exception){
       cout<<"Error deleting item with index "+index<<endl;
       return cityPhone();
       }
   }
vector<cityPhone>& getData(){
   return data;
}
   void phoneChange(string city, string newPhone){
   try{
       int i=0;
       vector<cityPhone>::iterator p=data.begin();
       for(p=data.begin();p<data.end();p++,i++){
       if (getCityPhone(i).getCity().compare(city)==0){
           string oldPhone = getCityPhone(i).getCode();
           getCityPhone(i).setCode(newPhone);
           cout<<"Phone of city "+city + " was changed from "+oldPhone + " to " + newPhone<<endl;
           return;
       }
       cout<<"No such city exists!n";
       }
   }
   catch(exception){
   cout<<"Error changing phone"<<endl;
        }
   }
    friend istream& operator>>(ostream& out,phoneDirectory a);
    friend ostream& operator<<(ostream& out,phoneDirectory a);
 };
istream& operator>>(istream& in,phoneDirectory& a){
    string city,phone;
in >> city >> phone;
a.addCityPhone(city,phone);
return in;
}
ostream& operator<<(ostream &out, cityPhone a){
return out << a.getCity()  <<" " << a.getCode() <<endl;
}
void sortByCity(phoneDirectory &a){
        std::sort(a.getData().begin(),a.getData().end(),cmpCity);
        for(unsigned i=0;i<a.getData().size();i++)
      cout<<a.getCityPhone(i);
       }
void sortByCode(phoneDirectory &a){
        std::sort(a.getData().begin(),a.getData().end(),cmpCode);
        for(unsigned i=0;i<a.getData().size();i++)
      cout<<a.getCityPhone(i);
       }
int main()
{
    phoneDirectory test("C:\t.txt");
    cin>>test;
    sortByCity(test);
    cout<<endl<<endl;
    sortByCode(test);
    system("pause");
    return 0;
}

我完全被卡住了,任何帮助都将不胜感激。抱歉英语不好(不是我的母语)

您可以在c++11:中这样过滤您的电话号码

std::vector<cityPhone> contacts;
add_contacts(contacts); //add some data
std::vector<cityPhone> varna_contacts;
std::copy_if(contacts.begin(), contacts.end(), std::back_inserter(varna_contacts), 
  [] (const cityPhone& contact) { return contact.getCity() == "Varna"; });

或不带lambda(c++03):

class phoneDirectory {
  static void predicate(const cityPhone& cp) {
    return contact.getCity() == "Varna";
  }
}
int main() {
  std::vector<cityPhone> contacts;
  add_contacts(contacts); //add some data
  std::vector<cityPhone> varna_contacts;
  std::copy_if(contacts.begin(), contacts.end(), 
    std::back_inserter(varna_contacts), phoneDirectory::predicate);
}