给出字符串值的列表
Give list of string value
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;
}
现场演示
相关文章:
- C++:如何使函数只返回作为列表一部分的字符串
- C++ 创建包含链表和字符串的对象的链接列表时出错
- 在 c++ 中多次更改字符串列表
- C++未知长度字符串的数组,其行为类似于 Python 字符串列表
- 如何将包含括号的字符串列表平展为单个列表?
- 无法在声明时使用初始值设定项列表初始化常量字符*/字符串数组的向量
- C++完全复制要字符串的字符列表,而不会忽略多个空格
- (C++) 字符串文本与参数列表不匹配
- C++使用旧编译器GCC 4.4.0在结构列表中查找字符串
- 如何存储字符串列表
- 如何在数组或链接列表中查找特定字符串
- C++ EnumWindows,将列表存储在字符串数组中
- 给定字符串和单词 S 的列表,检查列表中是否存在 S
- C 需要帮助的作业程序,该程序在文件列表中读取并在每个双重的双重列表中添加一个字符串
- 查找可变参数列表的字符串格式指定符
- 在链接列表中添加字符串的简单方法
- 嵌套列表(字符串向量的向量)初始化失败
- 通过单词列表将两个字符串移动以比较每个单词
- 链接的列表字符串分类问题
- 穿过链接列表以查找字符串