gpt4 book ai didi

c++ - 在函数中过滤 vector 并返回另一个包含过滤结果的 vector

转载 作者:行者123 更新时间:2023-11-28 06:25:37 24 4
gpt4 key购买 nike

我正在做一个类(class)项目,我被困在完成之前我需要做的最后一件事中。问题是我需要为我的自定义类创建一个函数(使用 vector 作为它的元素之一)过滤我的类的 vector 并返回另一个只包含 vector 的匹配项的 vector ,这件事必须用 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.抱歉英语不好(不是我的母语)

最佳答案

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

关于c++ - 在函数中过滤 vector 并返回另一个包含过滤结果的 vector ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28590447/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com