过滤函数中的矢量,并返回另一个带有过滤结果的矢量
filter vector in function and return another vector with the filtered results
我正在做一个类项目,在完成之前,我被困在了最后一件需要做的事情上。。问题是,我需要为我的自定义类创建一个函数(将向量作为其元素之一),该函数过滤我类的向量,并返回另一个仅包含向量匹配项的向量,而这必须使用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);
}
相关文章:
- 为什么"do while"循环不断退出,即使条件计算结果为 false?
- valgrind-hellgrind与泄漏检查的结果不同
- 用C++20 fmt限制结果的总大小
- 如何返回一个类的两个对象相加的结果
- 使用QProcess执行命令,并将结果存储在QStringList中
- 如果我std::dynamic_pointer_cast并且底层dynamic_cast的结果为null,那么返回的sh
- 在没有定义返回类型的函数中返回布尔值,并将结果保存在无错误的char编译中-为什么
- 序列化,没有库的整数,得到奇怪的结果
- 使用取消引用的指针的多态性会产生意外的结果.为什么?
- 在更改for循环的第三部分后,未使用for循环结果
- 使用++运算符会导致意外的结果
- 为什么在逗号分隔符上下文中将预增量的结果强制转换为void
- C++Brute Force攻击函数不会返回结果
- 你好。。。id_public变量不应该给出结果为 81 和 86 吗?为什么它为两个派生类占用不同的内存位置?
- 算术运算的结果类似于:C浮点变量中的1/3
- ";结果类型必须是可从输入范围的值类型""构造的;创建std::vector时
- 按扩展名过滤搜索文件会返回太多结果
- 如何在不复制的情况下将过滤向量的结果存储在另一个向量中
- 过滤函数中的矢量,并返回另一个带有过滤结果的矢量
- 用c++运行一个进程,过滤输出,获取结果代码,同时获取系统错误