使用迭代器从向量中查找子字符串

Find a substring from a vector using iterators

本文关键字:查找 字符串 向量 迭代器      更新时间:2023-10-16

我正在尝试在我的应用程序中创建一个搜索功能。如果用户输入子字符串(或完整字符串),我想知道该子字符串是否与存储在我的向量中的任何字符串或部分字符串匹配。

到目前为止编写了以下代码:

cout << "Input word to search for: ";
cin >> searchString;

for (multimap <string, vector<string> >::const_iterator it = contactInformationMultimap.cbegin();       it != contactInformationMultimap.cend(); ++it)
{
    for (vector<string>::const_iterator iter = it->second.cbegin(); iter != it->second.cend(); ++iter)
    {
        if (*iter.find(searchString))
            ^^^^^^^^^^^^^^^^^^^^^^^   this does not work, if i cout *iter it is the correct word                     stored in the vector. The problem is that i can not use the find function.
    }                                 
}

有人有什么建议吗?

一元运算符的优先级低于后缀运算符。在您的 if 语句中,您需要在成员访问运算符之前计算一元运算符 *。所以你必须写

if ( ( *iter ).find(searchString) != std::string::npos )

或者你可以写

if ( iter->find(searchString) != std::string::npos )

考虑到此记录

if ( ( *iter ).find(searchString) )

毫无意义。

你也可以写

for (multimap <string, vector<string> >::const_iterator it = contactInformationMultimap.cbegin();       it != contactInformationMultimap.cend(); ++it)
{
    for ( const std::string &s : it->second )
    {
        if ( s.find(searchString ) != std::string::npos ) /*...*/;
    }                                 
}

注释显示了如何更正语法以便您的代码可以编译,但结果是我仍然(至少个人)宁愿避免的代码。迭代器允许在泛型算法中使用它们的主要原因。在这种情况下,通用算法可以很好地完成这项工作。例如,假设您要打印出与该键关联的值包含searchString中的任何值的每条记录的键。为此,您可以像这样编写代码:

std::copy_if(data.begin(), data.end(),    // The source "range"
    std::ostream_iterator<T>(std::cout, "n"), // the destination "range"
    [&](T const &v) {
        return std::any_of(v.second.begin(), v.second.end(),
            [&](std::string const &s) {
                return s.find(searchString) != std::string::npos;
            }
        );
    }
);

这取决于正确类型的operator<<,如下所示:

typedef std::pair < std::string, std::vector<std::string>> T;
namespace std {
    std::ostream &operator<<(std::ostream &os, T const &t) {
        return os << t.first;
    }
}

一个完整的测试程序可能如下所示:

#include <map>
#include <algorithm>
#include <iostream>
#include <iterator>
#include <vector>
#include <string>
typedef std::pair < std::string, std::vector<std::string>> T;
namespace std {
    std::ostream &operator<<(std::ostream &os, T const &t) {
        return os << t.first;
    }
}
int main() { 
    std::multimap<std::string, std::vector<std::string>> data{
        { "key1", { "Able", "Bend", "Cell" } },
        { "key2", { "Are", "Dead" } },
        { "key3", { "Bad", "Call" } }
    };
    std::string searchString = "a";
    std::copy_if(data.begin(), data.end(),
        std::ostream_iterator<T>(std::cout, "n"),
        [&](T const &v) {
            return std::any_of(v.second.begin(), v.second.end(),
                [&](std::string const &s) {
                    return s.find(searchString) != std::string::npos;
                }
            );
        }
    );
}

结果:

key2
key3
您可以使用

if ( *iter.find(searchString) != string::npos )