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

时间:2015-02-18 17:53:00

标签: c++ vector filter stl

我正在从事一个班级项目,并且在我完成之前,我仍然坚持要做的最后一件事。问题是我需要为我创建一个函数自定义类(使用向量作为其元素之一)过滤我的类的向量并返回另一个仅包含向量的匹配项的向量,这个东西必须用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;
}

我完全卡住了,任何帮助都会非常感激p.s.抱歉英语不好(不是我的母语)

1 个答案:

答案 0 :(得分:1)

您可以在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);
}