如何从歌曲的专辑字段中删除标记 `[None]` 并用空格替换 None? [英] How to remove the token `[None]` from the album field of the songs and replace None with whitespaces?

查看:26
本文介绍了如何从歌曲的专辑字段中删除标记 `[None]` 并用空格替换 None?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

功能:

void cleanAlbum()

从没有有效专辑的歌曲的专辑字段中删除标记 [None].

removes the token [None] from the album field of the songs that do not have a valid album.

我有一个名为 collection 的 Songs 向量,并且有像

I have a vector of Songs called collection and there are strings like

字符串艺术家;

字符串专辑;

string song_name;

string song_name;

我需要将 [None] 替换为 ""(6 个空格)在此集合中用于名称为[无]"的专辑

我的代码不起作用:

     replace_if(collection.begin(), collection.end(), "[None]", "      ");

推荐答案

看起来您正在尝试将 Song 类与代码中的字符串进行比较.那行不通.如果我是你,我会在你的 Song 类中添加一个名为 isEmpty 的布尔变量,在你输入[None]"的情况下它会是真的.以及一个名为 empty() 的函数,如果歌曲为空则返回 true.

It looks like you are trying to compare, your Song class to a string in your code. That won't work. If I were you I'd add a boolean variable to your Song class called isEmpty which will be true in the case where your were entering "[None]" and a function called empty() which returns true if the song is empty.

使用此代码,您的代码应如下所示:

Using this your code should look like:

replace_if(collection.begin(), collection.end(), empty, "      ");

这是有关 replace_if 函数的文档的链接,http://www.cplusplus.com/reference/algorithm/replace_if/ empty() 可能不必是 Song 类的成员函数才能使其工作,在这种情况下,我建议给它一个不同的名称.

Here is a link to the documentation on the replace_if function, http://www.cplusplus.com/reference/algorithm/replace_if/ empty() might have to not be a member function of the Song class for this to work, in which case I'd advise giving it a different name.

这篇关于如何从歌曲的专辑字段中删除标记 `[None]` 并用空格替换 None?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆