如何从向量中删除某些指定值?
How to remove certain specified value from vectors?
mySongs
是一个向量,存储了用户输入的歌曲集合。在 if 语句中,程序将根据用户输入检查向量中的元素。如果匹配,它将从向量中删除指定的值。当我寻找解决方案时,我看到有人建议使用 remove/erase 成语:。但是当我在我的代码中实现时,它继续弹出这个错误 C2678 binary '==': no operator found which takes a left - hand operand of type 'Song' (or there is no acceptable conversion)
void deleteSong() {
string songTitle;
cout << "\n\t\tPlease enter the particular song name to remove: ";
cin >> songTitle;
if (songTitle != "") {
for (Song songs : mySongs) {
if (songs.title == songTitle) {
mySongs.erase(find(mySongs.begin, mySongs.end, songs)); //erase an element with value
break;
}
}
}
}
此错误是由于 class 歌曲没有 == 运算符。这可以通过以下两种方式之一解决
如果你能接触到Song的源代码那么
向其中添加以下功能。请注意 const 是必需的
bool operator == ( const Song& song)
{
//Do the comparison code here
}
如果您无权访问源代码。然后添加如下函数
bool operator == (const Song& song1, const Song& song2)
{
// Do the comparison code here
}
也就是说,您的代码还有一个小问题
擦除函数应该这样调用
mySongs.erase(find(mySongs.begin(), mySongs.end(), songs));
我决定添加一个最小示例来帮助您。在那个例子中,我假定了 Song 的某种实现,但该实现不一定是你所拥有的
#include <iostream>
#include<vector>
#include<algorithm>
#include<string>
using namespace std;
class Song
{
public:
std::string title;
/* bool operator ==(const Song& other)
{
return this->title == other.title;
}*/
};
bool operator ==(const Song& song1,const Song& other)
{
return song1.title == other.title;
}
std::vector<Song> mySongs={{"ali"},{"ahmed"},{"ali"}};;
void deleteSong() {
string songTitle;
cout << "\n\t\tPlease enter the particular song name to remove: ";
cin >> songTitle;
if (songTitle != "") {
for (const auto& songs : mySongs) {
if (songs.title == songTitle) {
mySongs.erase(find(mySongs.begin(), mySongs.end(), songs)); //erase an element with value
break;
}
}
}
}
int main() {
// your code goes here
return 0;
}
mySongs
是一个向量,存储了用户输入的歌曲集合。在 if 语句中,程序将根据用户输入检查向量中的元素。如果匹配,它将从向量中删除指定的值。当我寻找解决方案时,我看到有人建议使用 remove/erase 成语:。但是当我在我的代码中实现时,它继续弹出这个错误 C2678 binary '==': no operator found which takes a left - hand operand of type 'Song' (or there is no acceptable conversion)
void deleteSong() {
string songTitle;
cout << "\n\t\tPlease enter the particular song name to remove: ";
cin >> songTitle;
if (songTitle != "") {
for (Song songs : mySongs) {
if (songs.title == songTitle) {
mySongs.erase(find(mySongs.begin, mySongs.end, songs)); //erase an element with value
break;
}
}
}
}
此错误是由于 class 歌曲没有 == 运算符。这可以通过以下两种方式之一解决
如果你能接触到Song的源代码那么 向其中添加以下功能。请注意 const 是必需的
bool operator == ( const Song& song) { //Do the comparison code here }
如果您无权访问源代码。然后添加如下函数
bool operator == (const Song& song1, const Song& song2) { // Do the comparison code here }
也就是说,您的代码还有一个小问题
擦除函数应该这样调用
mySongs.erase(find(mySongs.begin(), mySongs.end(), songs));
我决定添加一个最小示例来帮助您。在那个例子中,我假定了 Song 的某种实现,但该实现不一定是你所拥有的
#include <iostream>
#include<vector>
#include<algorithm>
#include<string>
using namespace std;
class Song
{
public:
std::string title;
/* bool operator ==(const Song& other)
{
return this->title == other.title;
}*/
};
bool operator ==(const Song& song1,const Song& other)
{
return song1.title == other.title;
}
std::vector<Song> mySongs={{"ali"},{"ahmed"},{"ali"}};;
void deleteSong() {
string songTitle;
cout << "\n\t\tPlease enter the particular song name to remove: ";
cin >> songTitle;
if (songTitle != "") {
for (const auto& songs : mySongs) {
if (songs.title == songTitle) {
mySongs.erase(find(mySongs.begin(), mySongs.end(), songs)); //erase an element with value
break;
}
}
}
}
int main() {
// your code goes here
return 0;
}