【问题标题】:how to compare a vector of pointers and string ?如何比较指针和字符串的向量?
【发布时间】:2018-11-18 06:47:23
【问题描述】:
struct compare{

string k;
compare(string a) : k(a){}
bool operator()(const product* t)const{
return (t->productdetails.getName()<k);
}

};

void cashier::calculate(string s,vector<product*> &a){

//this is lambda (tried to use it ,but doesn't work)
auto comp=[](const product* t, const string b)
{
  return (b < t->productdetail.getName());
};

if (std::binary_search ( a.begin(), a.end(),s, compare(s)))
    cout << "found!\n";
else
    std::cout << "not found.\n";
 }

我在这部分卡了很长时间。 向量保存产品(类)指针。 product 指针指向 productdetail,它有两个变量(名称和价格)

我需要在向量中查找(字符串s),如果字符串s在向量中(product->productdetail.getName()),我需要返回价格..

如何比较 proct* 和 string s ? 我的老师给了我建议,我需要做另一个字符串转换函数,因为比较类型需要相同..

(我尝试使用lambda,它不起作用并更改为比较功能)

【问题讨论】:

  • 请提供minimal reproducible example。此外,你为什么要使用 vector 的指针?为什么不简单地vector&lt;product&gt;?此外,您不希望比较函数同时采用对象和指针。在比较之前通过引用和取消引用

标签: c++


【解决方案1】:

我同意@Fureeish,在现代 C++ 中使用裸指针不是一个好的选择,除非你绝对必须这样做。

也就是说,为标准库编写谓词需要对您正在使用的库方法有很好的了解。查看std::binary_search 文档。一个简单的应用程序是搜索与您正在搜索的类型相同的类型。换句话说,你的针和你干草堆里的针是同一类型的。这是一个例子:

#include <iostream>
#include <vector>
#include <algorithm>

class product {
public:
    class product_details {
    public:
        std::string name;
        double price;
    };

    product_details productDetails{};

    product(std::string const &name, double price) : productDetails{ name, price} {}
};

struct product_compare {
    bool operator()(product const &lhs, product const &rhs) {
        return lhs.productDetails.name < rhs.productDetails.name;
    }
};

int main(int argc, char *argv[])
{
    std::vector<product> producList{
            { "toaster", 74.99 },
            { "blender", 103.99 },
            { "slow cooker", 142.99 }
    };

    product_compare productCompare{};
    std::sort(producList.begin(), producList.end(), productCompare);

    product searchProduct{"slow cooker", 142.99};
    if (std::binary_search(producList.begin(), producList.end(), searchProduct, productCompare))
        std::cout << "Found!\n";
    else
        std::cout << "Not found!\n";
}

但这缺乏优雅。你的针可以是不同的类型,答案实际上是in this SO question。这是利用这一点的重写。这个成语写起来有点复杂,但更贴近实际问题,因此更容易理解。最终你永远不知道比较的哪一边是针,哪一边是干草。所以你必须编写比较谓词来接受这两个比较。

#include <iostream>
#include <vector>
#include <algorithm>

class product {
public:
    class product_details {
    public:
        std::string name;
        double price;
    };

    product_details productDetails{};

    product(std::string const &name, double price) : productDetails{ name, price} {}
};

struct product_compare {
    bool operator()(std::string const &lhs, product const &rhs) {
        return lhs < rhs.productDetails.name;
    }

    bool operator()(product const &lhs, std::string const &rhs ) {
        return lhs.productDetails.name < rhs;
    }
};

// Used by std::sort
bool operator<(product const &lhs, product const &rhs) {
    return lhs.productDetails.name < rhs.productDetails.name;
}

int main(int argc, char *argv[])
{
    std::vector<product> producList{
            { "toaster", 74.99 },
            { "blender", 103.99 },
            { "slow cooker", 142.99 }
    };

    product_compare productCompare{};
    std::sort(producList.begin(), producList.end());

    if (std::binary_search(producList.begin(), producList.end(), std::string("slow cooker"), productCompare))
        std::cout << "Found!\n";
    else
        std::cout << "Not found!\n";
}

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-10-27
    • 1970-01-01
    • 1970-01-01
    • 2020-08-17
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多