小编tar*_*ali的帖子

remove_if() 究竟是如何工作的?

我参考了许多资源,但无法理解有关它如何修改容器中元素的部分。我写了一些代码来试图理解,谁能解释一下发生了什么?

#include<bits/stdc++.h>
using namespace std;

bool test(char c)
{
    return (c=='a');
}

int isPalindrome(string A) 
{
    remove_if(A.begin(), A.end(), test);
    cout<<A<<'\n';   
 }

int main() 
{
    
    string A="aaaaabbbbb";
    isPalindrome(A); 

    return 0;
}
Run Code Online (Sandbox Code Playgroud)

根据我到目前为止的理解,它应该将所有 a 字符移到 b 的右侧,如果我们尝试打印它,我们应该得到

bbbbbaaaaa
Run Code Online (Sandbox Code Playgroud)

相反,我得到

bbbbbbbbbb
Run Code Online (Sandbox Code Playgroud)

c++ stl c++17

1
推荐指数
1
解决办法
146
查看次数

标签 统计

c++ ×1

c++17 ×1

stl ×1