C ++遍历向量并删除匹配的字符串
C++ Iterate through a vector and delete a matching string
我正在尝试对字符串列表进行编码。用户可以添加到列表或从列表中删除以及显示当前列表。
显示列表和添加到列表工作正常,但我不知道如何通过遍历列表以找到匹配项来删除用户的字符串。
我该如何更改我的代码来解决这个问题?
看看 if(answer == 3)
// InClassAssignment-FavouriteGameList.cpp : Defines the entry point for the console application.
//
#include "stdafx.h"
#include <string>
#include <cstdlib>
#include <iostream>
#include <vector>
#include <algorithm>
#include <ctime>
#include <cctype>
using namespace std;
int _tmain(int argc, _TCHAR* argv[])
{
vector <string> gameList;
int answer = 0;
bool cont = true;
int size = 0;
vector<string>::iterator iter;
string addToList;
string removeFromList;
cout << "\tGame List" << endl;
while (cont)
{
cout << "--------------------------------------------" << endl;
cout << "\nWhat do you want to do?";
cout << "\n1 - Display List";
cout << "\n2 - Add to List";
cout << "\n3 - Remove from List";
cout << "\n4 - End program" << endl << "Selection: ";
cin >> answer;
cout << endl;
if (answer == 1)
{
cout << "List: ";
for (iter = gameList.begin(); iter != gameList.end(); ++iter)
{
if (iter != gameList.end() - 1)
cout << *iter << ", ";
else
cout << *iter << endl;
}
}
else if (answer == 2)
{
cout << "Type in a game to add: ";
cin >> addToList;
gameList.push_back(addToList);
cout << "\nAdded (" << addToList << ") to your list." << endl;
}
else if (answer == 3)
{
//display list
cout << "List: ";
for (iter = gameList.begin(); iter != gameList.end(); ++iter)
{
if (iter != gameList.end() - 1)
cout << *iter << ", ";
else
cout << *iter << "\n" << endl;
}
//ask which one to remove
cout << "Which game should be removed?: ";
cin >> removeFromList;
//loop/iterate through the list to find a match and erase it
for (iter = gameList.begin(); iter != gameList.end(); ++iter)
{
if ()
cout << "\nRemoved (" << removeFromList << ")" << endl;
else
cout << "\nGame not found" << endl;
}
}
else
{
cont = false;
}
}
cout << "\nThanks for using the program!\n" << endl;
return 0;
}
您可以使用 std::find
获取匹配您要删除的项目的迭代器,然后调用 vector::erase(iter)
auto iter = std::find(gameList.begin(), gameList.end(), removeFromList);
if (iter != gameList.end())
{
gameList.erase(iter);
}
看看remove
。它移动序列末尾的所有匹配元素,然后您可以截断它:
input.erase(
remove(input.begin(), input.end(), s)
, input.end());
编辑:合并了 Fred 评论中的建议。
我正在尝试对字符串列表进行编码。用户可以添加到列表或从列表中删除以及显示当前列表。
显示列表和添加到列表工作正常,但我不知道如何通过遍历列表以找到匹配项来删除用户的字符串。
我该如何更改我的代码来解决这个问题? 看看 if(answer == 3)
// InClassAssignment-FavouriteGameList.cpp : Defines the entry point for the console application.
//
#include "stdafx.h"
#include <string>
#include <cstdlib>
#include <iostream>
#include <vector>
#include <algorithm>
#include <ctime>
#include <cctype>
using namespace std;
int _tmain(int argc, _TCHAR* argv[])
{
vector <string> gameList;
int answer = 0;
bool cont = true;
int size = 0;
vector<string>::iterator iter;
string addToList;
string removeFromList;
cout << "\tGame List" << endl;
while (cont)
{
cout << "--------------------------------------------" << endl;
cout << "\nWhat do you want to do?";
cout << "\n1 - Display List";
cout << "\n2 - Add to List";
cout << "\n3 - Remove from List";
cout << "\n4 - End program" << endl << "Selection: ";
cin >> answer;
cout << endl;
if (answer == 1)
{
cout << "List: ";
for (iter = gameList.begin(); iter != gameList.end(); ++iter)
{
if (iter != gameList.end() - 1)
cout << *iter << ", ";
else
cout << *iter << endl;
}
}
else if (answer == 2)
{
cout << "Type in a game to add: ";
cin >> addToList;
gameList.push_back(addToList);
cout << "\nAdded (" << addToList << ") to your list." << endl;
}
else if (answer == 3)
{
//display list
cout << "List: ";
for (iter = gameList.begin(); iter != gameList.end(); ++iter)
{
if (iter != gameList.end() - 1)
cout << *iter << ", ";
else
cout << *iter << "\n" << endl;
}
//ask which one to remove
cout << "Which game should be removed?: ";
cin >> removeFromList;
//loop/iterate through the list to find a match and erase it
for (iter = gameList.begin(); iter != gameList.end(); ++iter)
{
if ()
cout << "\nRemoved (" << removeFromList << ")" << endl;
else
cout << "\nGame not found" << endl;
}
}
else
{
cont = false;
}
}
cout << "\nThanks for using the program!\n" << endl;
return 0;
}
您可以使用 std::find
获取匹配您要删除的项目的迭代器,然后调用 vector::erase(iter)
auto iter = std::find(gameList.begin(), gameList.end(), removeFromList);
if (iter != gameList.end())
{
gameList.erase(iter);
}
看看remove
。它移动序列末尾的所有匹配元素,然后您可以截断它:
input.erase(
remove(input.begin(), input.end(), s)
, input.end());
编辑:合并了 Fred 评论中的建议。