如何在字符串 C++ 编程中找到子字符串的所有位置
how do i find all the positions of substring in a string c++ programming
我有字符串(C ++编程)表示按键事件:X,Y,DEL如果收到事件 X,我必须打印"X",但对于两个事件,没有 Y 事件之前或之后它。
例如:
-
"DEL DEL X DEL Y " => 输出 "X"
"DEL DEL X DEL Y DEL" => 无输出
"Y DEL X DEL" => 无输出
"X X X X X X X "=>输出"XXXXXX"
我应该怎么做?我很难解析和搜索字符串谢谢
一个简单的
解析器:
#include<sstream>
void parse(std::string input, std::vector<std::string> &keys)
{
std::stringstream stream(input); // put input string into a stream for easy parsing
std::string key;
while (stream >> key) // reads from the stream up to the first whitespace
// or end of string
{
keys.push_back(key);
}
}
示例用法:
int main (int argc, char ** argsv)
{
std::vector<std::string> keys; // allocate a container for the keys in the input
// get input from unspecified source
parse(input, keys); // call above function to fill the keys container with input
// operate on keys
}
operate on keys
是困难的部分:使用键列表,您需要弄清楚要输出的内容。回来提供一些代码,我们可以帮助您。
相关文章:
- 如何将元素添加到向量的字符串位置
- 我的目标是编写一个程序来计算和存储字符串在字符数组中出现的位置
- 为什么字符串的 move() 会改变内存中底层数据的位置?
- 返回员工和工资(给定位置子字符串)
- 将文件复制到自定义位置,存在字符串转换问题
- 替换字符串位置 X 中的单词C++
- 在特定位置为字符串添加空格
- 计算在同一位置至少包含一个常用字符的不同字符串对
- 如何在另一个字符串的x位置插入一个字符串?
- 我如何知道字符串中字符相对于英文字母的位置值?
- C++ - 重置字符串流不会重置获取位置或清除标志吗?
- 反转字符串中单词的位置,而不更改 O(1) 空格限制中特殊字符的顺序
- 如何从字符串向量中选择第 n 个位置?
- 如何从输入字符串中提取特定位置的字符?
- C++字符串_Xout_of_range("无效的字符串位置");
- 将字符数复制到新字符串位置的字符串操作,例如 S[0]
- 如何从字符串位置构造CSTRING/STD :: String
- 如何在 boost::regex 中找到子匹配的字符串位置
- 空字符串位置返回为*
- 如果我将一个值赋给一个目前不使用的字符串位置,究竟会发生什么?