删除在两个字符串的比较中不常见的字符
Removing characters that are uncommon in the comparison of two strings
string word1 = "misisipi";
string word2 = "mississippi";
我想"比较"这些字符串,并能够"扔掉"不常见的字母。例如,word2将被削减为"misisipi"
,确保保持'S'的顺序,并且word1
不会改变,因为它的所有字符都在word2
。 我知道如何删除字符串中的元素,但这次我想保持顺序。 例如,如果我不维护订单,经过比较,word2
将是"missipi"
,这不是我想要的。
尝试在
循环访问word1
的每个元素时将有效的word2
字符复制到word1
中。
std::string temp;
std::string::iterator w2 = word2.begin();
for(std::string::iterator w1 = word1.begin(); w1 != word1.end(); ++w1)
{
for(; w2 != word2.end(); ++w2) // Move through word2...
{
if(*w1 == *w2) // Copy the character into temp when found.
{
temp += *w2;
break;
}
}
}
std::cout << temp << std::endl;
如果您想要速度,可能需要事先分配temp
。
相关文章:
- std::less是否应该允许在编译时比较不相关的指针?
- 当两个相等的双精度的相对比较不起作用时,我该怎么办?
- 在 MSVC 中,从 std::string_view 派生的对象比较不明确
- 在 c++ 中比较不相等数组或字符串的方法
- 为什么我没有警告枚举比较不匹配
- 字符串比较不正确
- 为什么INT(char)的比较不起作用
- 为什么数组之间的==相等比较不起作用
- 为什么字符数组之间的这种比较不起作用?
- 字符串在相等时比较不相等
- 我的自定义比较不起作用
- C(++) 中双精度的排序/比较不稳定
- 内置C++函数,使两个字符串的比较不区分大小写
- 我的C++代码出了什么问题?最不常见的祖先
- C++中一个模板函数中使用的类的不常见函数
- GNU使ifeq比较不起作用
- 查找整数C++中最不常见的数字
- 删除在两个字符串的比较中不常见的字符
- 矢量元素和 txt 文件行之间的字符串比较不起作用
- malloc的不常见使用