给出字符串值的列表

Give list of string value

本文关键字:列表 字符串      更新时间:2023-10-16
std::string text;
std::getline(std::cin, text);

有了上面的设置,我如何识别一个字符串列表,它将以文本形式输入,等于一个值?

,

std::string text;
std::getline(std::cin, text);
std::string aux; //Added
text.find("word ", "thisword", "another", "floor") = aux; //Added
if(text.find("lutece labs" + aux) != std::string::npos); //Goal
{
...megh...
}

我觉得我糟蹋了上面的代码,但我希望它能解释我在寻找什么。所有字符串输入都来自文本。那么,我如何创建一个可以在文本中找到的单词列表,从而使新列表等于单个值呢?希望我问得清楚。谢谢你!

你可以像下面这样做,可能不是最好的方法:

#include <algorithm>
#include <string>
#include <iterator>
#include <sstream>
#include <iostream>
int main()
{
        std::string text = "This is a long text word 
                            this word another floor king 
                            queen ace";
    std::stringstream ss(text) ; 
    std::vector<std::string> vec{ // Form a vector of string
                std::istream_iterator<std::string>(ss),
                std::istream_iterator<std::string>() };
    // Get list of words to be searched for
    std::vector<std::string> to_find {"word ", "this", 
                          "word", "another", "floor"};                   
    std::string aux ="jack"; // the replace word
    std::replace_if(   vec.begin( ),vec.end( ), /* Loop over vec */
                    [to_find](const std::string& x)
                    {  /* Find if any from to_find is present */
                       return std::any_of( 
                              to_find.begin(), 
                              to_find.end(),
                              [x](const std::string& y)
                              { return x == y; }
                                         ) ;
                    },
                    aux );
    /* Now get your modified text */                
    text =std::accumulate( vec.begin()+1, vec.end( ), 
                           vec[0],
                         [](std::string s0, std::string const& s1) 
                         { return s0 += " " + s1; }
                        );
    std::cout << text ;
}

参见这里(只是一个简单的演示,您需要检查边界条件)

尝试如下:

#include <algorithm>
#include <string>
#include <iostream>
namespace detail
{
    template<int... Is>
    struct index_sequence { };
    template<int N, int... Is>
    struct make_index_sequence : make_index_sequence<N-1, N-1, Is...> { };
    template<int... Is>
    struct make_index_sequence<0, Is...> : index_sequence<Is...> { };
    void replace_all_with_t(std::string, std::string)
    {
    }
    template<class... Tail>
    void replace_all_with_t(std::string& c,
                            std::string value,
                            std::string head, Tail&&...tail)
    {
        while (c.find(head) != std::string::npos)
            c.replace(c.find(head), head.size(), value);
        replace_all_with_t(c, std::move(value), std::forward<Tail>(tail)...);
    }
    template<class Tuple, int... Is>
    void replace_all_with(std::string& c,
                          Tuple&& tokens,
                          std::string value,
                          detail::index_sequence<Is...>)
    {
        replace_all_with_t(c, std::move(value), std::get<Is>(std::forward<Tuple>(tokens))...);
    }
}
template<class Tuple>
void replace_all_with(std::string& c,
                      Tuple&& tokens,
                      std::string value)
{
    detail::replace_all_with(c, std::forward<Tuple>(tokens), std::move(value),
               detail::make_index_sequence<std::tuple_size<Tuple>::value>());
}
int main()
{
    std::string s = "abchellojsoncrazyabcworldabc";
    replace_all_with(s, std::make_tuple("abc", "json"), "*");
    std::cout << s;
}

现场演示