我得到了以下任务:创建一个包含男人,女人和未知名称的文本文件。尝试实现一个过滤器,将那些名称与真实名称进行比较(因此,总共输入了3个文件:男性,女性,比如说房客)。过滤时,将匹配的名称放入其适当的容器中。在我看来,这很简单,所以我按照下面提供的方式进行了操作。

我的问题是:有没有一种方法可以优化此代码?

我尝试使用抽象类,并基于抽象实体创建4个不同的对象(男人,女人,已知,未知)。但是对于这样一个简单的任务,代码量仍然很大。另一个想法是使用lambda表达式,但是我仅限于C ++ 98。

我想我想得太多了...

#include <fstream>
#include <iostream>
#include <vector>
#include <string>

int main()
{
    std::ifstream men("resources/men_names.txt");
    std::ifstream women("resources/women_names.txt");
    std::ifstream renters("resources/renter_names.txt");

    std::vector<std::string> menNames;
    std::vector<std::string> womenNames;
    std::vector<std::string> renterNames;
    std::vector<std::string> knownRenters;
    std::vector<std::string> unknownRenters;

    std::string name;

    while (men >> name)
        menNames.push_back(name);

    men.close();

    while (women >> name)
        womenNames.push_back(name);

    women.close();

    while (renters >> name)
        renterNames.push_back(name);

    renters.close();

    std::vector<std::string>::iterator itMen;
    std::vector<std::string>::iterator itWomen;
    std::vector<std::string>::iterator itRenters;

    for (itRenters = renterNames.begin(); itRenters != renterNames.end(); itRenters++)
    {
        bool found = false;

        for (itMen = menNames.begin(); itMen != menNames.end(); itMen++)
        {
            if ((*itMen) == (*itRenters))
            {
                found = true;
                knownRenters.push_back((*itMen));
            }
        }
        if (!found)
        {
            for (itWomen = womenNames.begin(); itWomen != womenNames.end(); itWomen++)
            {
                if ((*itWomen) == (*itRenters))
                {
                    found = true;
                    knownRenters.push_back((*itWomen));
                }
            }
        }
        if (!found)
            unknownRenters.push_back((*itRenters));
    }

    std::cout << knownRenters.size() << '\n';
    std::cout << unknownRenters.size() << '\n';

    std::cin.get();

    return 0;
}

最佳答案

缩短现有代码。这应该都是C ++ 98

#include <fstream>
#include <iostream>
#include <vector>
#include <string>
#include <set>
#include <iterator>

int main()
{
    std::ifstream men("resources/men_names.txt");
    std::ifstream women("resources/women_names.txt");
    std::set<std::string> peopleNames;

    peopleNames.insert(std::istream_iterator<std::string>(men), std::istream_iterator<std::string>());
    peopleNames.insert(std::istream_iterator<std::string>(women), std::istream_iterator<std::string>());

    std::ifstream renters("resources/renter_names.txt");
    std::vector<std::string> knownRenters;
    std::vector<std::string> unknownRenters;

    for (std::string name; renters >> name; )
    {
        if (peopleNames.count(name))
            knownRenters.push_back(name);
        else
            unknownRenters.push_back(name);
    }

    std::cout << knownRenters.size() << '\n';
    std::cout << unknownRenters.size() << '\n';

    std::cin.get();

    return 0;
}

关于c++ - 在C++ 98中优化 vector 的过滤,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/50275670/

10-13 05:05