【问题标题】:How do you find a string/char inside another string that's in a vector in C++? [that works for me]如何在 C++ 的向量中的另一个字符串中找到一个字符串/字符? [这对我行得通]
【发布时间】:2022-01-01 11:49:44
【问题描述】:

我在多个论坛页面上查找了这一点,而不仅仅是堆栈溢出,并尝试了来自“检查字符串是否包含 C++ 中的字符串”帖子中的许多解决方案,以及其他解决方案,并尝试了几乎所有提出的解决方案,但没有其中似乎对我有用?我尝试了vector[i].find(std::string2)if(strstr(s1.c_str(),s2.c_str())) {cout << " S1 Contains S2";} 以及

 std::string in = "Lorem ipsum dolor sit amet, consectetur adipiscing elit,"
                     " sed do eiusmod tempor incididunt ut labore et dolore magna aliqua";
    std::string needle = "pisci";
    auto it = std::search(in.begin(), in.end(),
                   std::boyer_moore_searcher(
                       needle.begin(), needle.end()));
    if(it != in.end())
        std::cout << "The string " << needle << " found at offset "
                  << it - in.begin() << '\n';
    else
        std::cout << "The string " << needle << " not found\n";

还有更多解决方案(适用于我的代码),但没有一个有效。我唯一没有尝试过的是std::string.contain(),但那是因为 Visual Studios(2019 v.142 - 如果有帮助)[我正在使用的 C++ 标准语言,预览 - 最新 C++ 工作草案中的功能(std:c+ +latest),] 无法识别该函数 - 因为由于某种原因它无法识别更大的库。我什至尝试颠倒这两个变量,以防我将两者混合在一起,并在较小的变量中寻找较大的变量。

我是 C++ 的新手,所以我不擅长用这样的东西解决问题,所以如果你愿意,请原谅我的无知。 出现问题是因为我正在寻找的是向量吗?我创建了一个vector &lt;string&gt; names = {"Andrew John", "John Doe",};,其中包含名称,并试图“窥视”它并找到关键字,但同样,没有任何效果。在向量中查找某些东西时,是否有一个特殊的函数可以调用?任何帮助将不胜感激!

【问题讨论】:

  • 你将如何用你的眼睛、一张写在纸上的字符串列表和一支铅笔来标记找到的字符串?一个程序将非常相似。

标签: c++ visual-c++


【解决方案1】:

如何在 C++ 中的向量中的另一个字符串中找到一个字符串/字符?

如果std::vector&lt;std::string&gt; 没有排序,我会使用std::find_if

例子:

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

int main() {
    // A vector with some strings:
    std::vector<std::string> vec{
        {"Lorem ipsum dolor sit amet, consectetur adipiscing elit,"
         " sed do eiusmod tempor incididunt ut labore et dolore magna aliqua"},
        {"Foo bar"}};
                     
    std::string needle = "pisci";
    size_t pos;

    // find and save position in the found string
    auto it = std::find_if(vec.begin(), vec.end(),
                           [&needle,&pos](const std::string& str) {
                               return (pos = str.find(needle)) != std::string::npos;
                           });

    if(it != vec.end()) {
        std::cout << "Found needle at position " << pos
                  << " in string\n" << *it << '\n';
    }
}

Demo


让我们仔细看看std::find_if中使用的lambda expression

[&needle,&pos](const std::string& str) {
    return (pos = str.find(needle)) != std::string::npos;
}

lambda 是一个匿名类的实例,带有 operator(),这使得它可调用。您可以将其与下面的 struct 进行比较,该实例有一个名为 foo 的实例,可以调用:

struct { void operator()() { std::cout << "foo1\n"; } } foo1;   // oldschool

auto foo2 = [](){ std::cout << "foo2\n"; };                     // lambda version:

int main() {
    foo1();  // calling foo1() - prints foo1
    foo2();  // calling foo2() - prints foo2
}}

find_if 中使用的lambda 中还有[&amp;needle,&amp;pos]。这意味着构造的 Lamba 将包含对这两个变量的保持引用,以便能够在函数中使用它们。

与普通的struct比较

    std::string s1 = "S1";
    std::string s2 = "S2";

    struct { // struct capturing a string by reference
        void operator()() { std::cout << "foo1 " + s + '\n'; }
        std::string& s;
    } foo1{s1};

    // lamda capturing by reference
    auto foo2 = [&s2]() { std::cout << "foo2 " + s2 + '\n'; };

    foo1(); // prints "foo1 S1"
    foo2(); // prints "foo2 S2"

因此,正如您所见,lambda 函数和函数对象(如上面的匿名结构实例)有很多共同点。

答案中的 lambda 通过引用捕获两个变量,即“外部”变量 needlepos,并且无论何时调用它都会接受一个参数 (const std::string&amp; str)。

使用 str 它会尝试查找 needle 并将结果分配给 pos

pos = str.find(needle)

pos 然后与std::string::npos 进行比较。如果 不是 std::string::npos,则找到了针,lambda 将返回 true,这会阻止 std::find_if 函数进一步查找并返回一个迭代器到找到的 std::string。作为奖励,pos 仍将设置为其分配的最新值 - 这是在字符串中找到针的位置。

【讨论】:

  • 非常感谢,伙计,这成功了!部分代码让我有点不知所措,特别是 [&needle, &pos](const std::string& str) 部分,我不想问,但如果你能解释它是如何工作的,那么我可以在需要时实现它未来的项目。如果不是很好,我只是感谢你回答这个问题。
  • @Frank_Hamill 不客气 - 我添加了一个小解释。如果您希望我更详细地解释它,请在此处发表评论。
  • 小解释??不,伙计,这是一个非常详细的解释,我不能要求更多,你甚至引用了 cppreference 上的 lambda 表达式,我现在很好理解了,谢谢。
  • @Frank_Hamill 太好了,很高兴听到这个消息!
【解决方案2】:

您的示例代码与您的文字描述不符。您不是在搜索字符串向量,而是在搜索 a 字符串。其实好像是根据CPPreference中的例子来的。

在字符串的第 43 位找到针
Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua

当我尝试它时它会起作用。

这对您的搜索来说可能是多余的,因为您只搜索字符串一次。使用简单的find 可能会更好。

这根本没有显示的是你希望它在一个循环中,in 被字符串向量中的每个值替换。这是一个简单的for 循环:

for (const auto& in : myvector) { ...

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2013-06-12
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-07-01
    • 2021-06-15
    • 2022-11-30
    相关资源
    最近更新 更多