【问题标题】:How to find a struct list item如何查找结构列表项
【发布时间】:2019-08-15 16:54:49
【问题描述】:

我有一个结构列表,如下所述:

struct Files
{
    string id;
    string path;
    string chksum;
};

还有一个包含此结构列表的变量,该结构描述了一个文件列表,其中包含字段idpathchksum

list<Files> myFiles;

我需要实现一个函数来搜索我的列表中是否存在确定的文件名。

我尝试使用find_if 算法,但遇到了非常奇怪的错误,我不确定如何有效地实现。

string filename = "mytext.txt";

auto match = std::find_if(myFiles.cbegin(), myFiles.cend(), [] (const Files& s) {
  return s.path == filename;
});

if (match != myFiles.cend()) 
{
    cout << "omg my file was found!!! :D";
}
else
{
    cout << "your file is not here :(";
}

【问题讨论】:

  • 我尝试使用find_if...您也可以添加该代码吗?
  • 当然...我编辑以包含我尝试过的代码
  • 为了将来参考,请尝试在您的问题中包含尽可能多的细节:您使用的是什么 IDE?您收到的错误文本是什么?等
  • 奇怪的错误不能很好地描述问题。

标签: c++ algorithm struct lambda find


【解决方案1】:

你需要将filename添加到lambda的捕获列表中:

string filename = "mytext.txt";

auto match = std::find_if(myFiles.cbegin(), myFiles.cend(), [filename] (const Files& s) {
    return s.path == filename;
});

if (match != myFiles.cend()) 
{
    cout << "omg my file was found!!! :D";
}
else
{
    cout << "your file is not here :(";
}

【讨论】:

  • 或者只是[=],虽然[&amp;](或[&amp;filename])可能更有意义,以避免复制字符串
  • 是的,工作正常,但请编辑帖子并将 simbols.cend() 替换为 myFiles.cend()
  • @RemyLebeau 我想通过引用提及捕获默认值或文件名,但决定不混淆这个问题;尤其是短字符串。
【解决方案2】:

似乎变量filename 是在块作用域中声明的。在这种情况下,您必须捕获它。最好通过引用来捕获它,例如

#include <iostream>
#include <string>
#include <list>
#include <iterator>
#include <algorithm>

struct Files
{
    std::string id;
    std::string path;
    std::string chksum;
};

int main()
{
    std::list<Files> myFiles = { { "10", "mytext.txt", "10" } };

    std::string filename = "mytext.txt";

    auto match = std::find_if( std::cbegin( myFiles ), std::cend( myFiles ), 
                               [&] ( const Files &s ) 
                               {
                                    return s.path == filename;
                               } );

    if ( match != std::cend( myFiles ) ) 
    {
        std::cout << "omg my file was found!!! :D";
    }
    else
    {
        std::cout << "your file is not here :(";
    }
}

否则,如果变量文件名在命名空间中声明,您可以按原样使用 lambda

#include <iostream>
#include <string>
#include <list>
#include <iterator>
#include <algorithm>

struct Files
{
    std::string id;
    std::string path;
    std::string chksum;
};

std::string filename = "mytext.txt";

int main()
{
    std::list<Files> myFiles = { { "10", "mytext.txt", "10" } };


    auto match = std::find_if( std::cbegin( myFiles ), std::cend( myFiles ), 
                               [] ( const Files &s ) 
                               {
                                    return s.path == filename;
                               } );

    if ( match != std::cend( myFiles ) ) 
    {
        std::cout << "omg my file was found!!! :D";
    }
    else
    {
        std::cout << "your file is not here :(";
    }
}

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2017-12-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2023-03-11
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多