C++ 通过名为id或name的属性从列表对象c++;

C++ 通过名为id或name的属性从列表对象c++;,c++,C++,我想获取/识别Id为“C003”的对象,例如,打印该对象的所有数据 我接收用cin键入的id,并将其存储在字符串变量中 我已经阅读了许多使用函数find()或find\u if()的网页和YouTube教程,但是这些示例只使用数字,所以第三个参数只是一个数字,如2 在我的例子中,我有一个包含对象的列表,我的条件是: if(iterador->getName()==citysearch.getName()){ cout一个问题是:只有在名称匹配时才增加迭代器。如果名称不匹配,迭代器将保持不变,并进

我想获取/识别Id为“C003”的对象,例如,打印该对象的所有数据

我接收用
cin
键入的id,并将其存储在
字符串
变量中

我已经阅读了许多使用函数
find()
find\u if()
的网页和YouTube教程,但是这些示例只使用数字,所以第三个参数只是一个数字,如2

在我的例子中,我有一个包含对象的列表,我的条件是:

if(iterador->getName()==citysearch.getName()){

cout一个问题是:只有在名称匹配时才增加迭代器。如果名称不匹配,迭代器将保持不变,并进入无限循环

尝试将迭代器移到
if
语句之外:

while(iterador != listcity.end()){

    if(iterador->getName() == citysearch.getName()){
        cout<<"Name: ";
        cout<< iterador->getId()<<endl;
        cout<<"Id: ";
        cout<< iterador->getName()<<endl;
        cout<<"------------------------------------------------------------"<<endl;
        //break;
    }
    iterador++;
}
while(iterador!=listcity.end()){
如果(iterador->getName()==citysearch.getName()){

cout这是您发布的代码的我的版本,运行正常。
对编译进行了更改。
见我上面的评论

//file City.h
#include <string>
#include <iostream>
#include <list>

using std::string;
using std::cout;
using std::endl;
using std::list;
using std::cin;

class City{
public:
City();
City(string id, string name);
string getId();
void setId(string id);
string getName();
void setName(string name);


private:
   string id;
   string name;
};

//file City.cpp

    //constructors
    City::City() {
    }
    City::City(string newId,string newName)
    {
        id=newId;
        name=newName;
    }
        //functions to get and set data
        string City::getId(){
            return id;
        };
        void City::setId(string newId){
            id = newId;
        };
        string City::getName(){
            return name;
        };
        void City::setName(string newName){
            name=newName;
        };
    ///
        
//main function
int main () {

City c1("C001","Barcelona");
City c2("C002","New York");
City c3("C003","San Andres");

list<City> listcity;
listcity.push_back(c1);
listcity.push_back(c2);
listcity.push_back(c3);


string namecity;
std::cout << "Enter city name: ";
std::getline(std::cin, namecity);

City citysearch;
citysearch.setName(namecity);

list<City>::iterator iterador=listcity.begin();

while(iterador != listcity.end())
{
    const std::string  list_name(iterador->getName());
    const std::string   search_name(citysearch.getName());
    std::cout << "City name from list: " << list_name << "\n";
    std::cout << "City name to search: " << search_name << "\n\n";
    if(list_name == search_name){
        cout<<"Name: ";
        cout<< iterador->getId()<<endl;
        cout<<"Id: ";
        cout<< iterador->getName()<<endl;
        cout<<"------------------------------------------------------------"<<endl;

        break;
    }
    iterador++;
    
}

return 0;
}
用例:找到城市 我强烈建议:

  • 使用调试器。一次执行一条语句
  • 如果有疑问,请在比较之前打印变量
  • 我已经阅读了许多使用函数
    find()
    find\u if()
    的网页和YouTube教程,但是这些示例只使用数字,所以第三个参数只是一个数字,如2

    std::find()
    的第三个参数和
    std::find_if()
    谓词的参数可以采用与正在搜索的迭代器范围引用的类型兼容的任何类型

    例如,要在
    City
    类中使用
    std::find()
    ,您需要为它定义一个
    操作符==()
    ,例如比较
    name
    s
    ,您需要一个lambda/functor,它将
    城市
    作为参数,并返回一个
    布尔
    ,指示该
    城市
    对象是否符合您的条件

    例如,尝试以下方法:

    城市h:

    #ifndef City_H
    #define City_H
    
    #include <iostream>
    #include <string>
    
    class City{
    public:
        City();
        City(const std::string &newId, const std::string &newName);
        std::string getId() const;
        void setId(const std::string &newId);
        std::string getName() const;
        void setName(const std::string &newName);
    
        bool operator==(const City &city) const;
    
    private:
        std::string id;
        std::string name;
    };
    
    std::ostream& operator<<(std::ostream &out, const City &city);
    
    #endif
    
    \ifndef City\u H
    #定义城市
    #包括
    #包括
    阶级城市{
    公众:
    城市();
    城市(const std::string和newId,const std::string和newName);
    std::string getId()常量;
    void setId(const std::string和newId);
    std::string getName()常量;
    void setName(const std::string和newName);
    布尔运算符==(常数城市和城市)常数;
    私人:
    std::字符串id;
    std::字符串名;
    };
    
    std::ostream&operator在代码中为citysearch.setName赋值的位置?此代码无法运行,因为
    City
    有两个名为
    setId
    的方法。在查找运行时错误时,提供兼容的代码符合您的最佳利益。有关如何构建良好代码示例的建议,请参阅。作为额外的奖励,MRE是一个极好的调试工具。很有可能你会在制作过程中发现一部分错误,你自己也会发现。嗯,你不应该在
    if
    语句中使用函数调用。设置临时变量,并在
    if
    语句之前分配它们,然后比较临时变量。这允许你在
    if
    语句中放置一个断点语句,并在比较之前查看值。使用调试器时,从函数调用返回的值更难查看或查看。您有
    列表
    ,但推送
    城市
    的变量。类型
    机器人
    与类型
    城市
    有何关系?您应该为
    列表指定一个常量迭代器y、 end()
    。这将防止编译器在每次迭代时调用
    listcity.end()
    。列表不会更改,因此结束值将保持不变;无需在每次迭代时调用它。(虽然有些编译器可能会在更高的优化级别上应用此优化,但IMHO,您始终会尝试帮助编译器。)这是一个错误,但是我移动了它,没有什么是完美的!!!非常感谢:'3!!!!!!!!!!!!!!!
    $ ./city.exe
    Enter city name: New York
    City name from list: Barcelona
    City name to search: New York
    
    City name from list: New York
    City name to search: New York
    
    Name: C002
    Id: New York
    ------------------------------------------------------------
    
    #ifndef City_H
    #define City_H
    
    #include <iostream>
    #include <string>
    
    class City{
    public:
        City();
        City(const std::string &newId, const std::string &newName);
        std::string getId() const;
        void setId(const std::string &newId);
        std::string getName() const;
        void setName(const std::string &newName);
    
        bool operator==(const City &city) const;
    
    private:
        std::string id;
        std::string name;
    };
    
    std::ostream& operator<<(std::ostream &out, const City &city);
    
    #endif