我有一个使用这段代码的作业,但我需要它才能开始工作。除了removeFollowers函数之外,一切都按预期工作。它最终会删除正确的跟随器,但是当再次打印数组时,它会造成混乱并重复现有的追随者。我知道问题在于removeFollowers函数的逻辑,但我似乎无法让它正常工作。
以下是头文件、主文件、当前输出和预期输出:
#ifndef TWITTERPROJECT_H_INCLUDED
#define TWITTERPROJECT_H_INCLUDED
#include<iostream>
using namespace std;
template <class T>
class Twitter {
private:
string name;
T followers[5];
int numFollowers;
public:
Twitter(string x);
void addFollower(T follower); //takes in follower to add
bool removeFollower(T follower); //takes follower to remove
void printFollowers(); //doesn't take any parameters
};
template <class T>
Twitter<T>::Twitter(string x) { //constructor
name = x;
numFollowers = 0; //starts out with zero followers
}
template <class T>
void Twitter<T>::addFollower(T follower) {
if (numFollowers < 5) {
followers[numFollowers] = follower;
numFollowers++;
}
else{
cout<<"Error, this user already has maximum followers" << endl; }
}
template <class T>
bool Twitter<T>::removeFollower(T follower) {
for(int i=0;i<numFollowers;i++){
if(followers[i] == follower){
for(int j = i + 1; j < numFollowers; j++){
followers[j - 1] = followers[j];
numFollowers--;
return true;
}
return false;
}
}
cout << endl;
}
template <class T>
void Twitter<T>::printFollowers() {
cout << "Followers for " << name << ": " << endl;
for(int i = 0; i < numFollowers; i++)
cout << followers[i] << endl;}
#endif // TWITTERPROJECT_H_INCLUDED#include <iostream>
#include "TwitterProject.h"
using namespace std;
struct Profile {
string userName;
int age;
string state;
};
ostream& operator << (ostream & output, const Profile & p) {
output << p.userName;
return output;
} //overloading the insertion operator <<
bool operator == (const Profile &p1, const Profile &p2)
{
return p1.userName == p2.userName;
} //overloading the == operator for the removeFollower method
int main()
{
Twitter<string> e1("Sara");
Twitter<Profile> e2("Ty");
//adding followers of type string to user Sara (also string)
cout << "Adding followers to Sara" << endl;
e1.addFollower("Rapunzel");
e1.addFollower("Flynn");
e1.addFollower("Cinderella");
e1.addFollower("Prince Charming");
//printing Sara's followers
e1.printFollowers();
cout << endl;
//creating profiles of type username
Profile p1 = {"Rue", 18, "Michigan"};
Profile p2 = {"Jules", 22, "California"};
Profile p3 = {"Fez", 30, "Alaska"};
Profile p4 = {"Maddie", 21, "Colorado"};
Profile p5 = {"Cassie", 24, "Maine"};
//adding profile followers to Ty user (profile)
cout << "Adding follower profiles to Sara" << endl;
e2.addFollower(p1);
e2.addFollower(p2);
e2.addFollower(p3);
e2.addFollower(p4);
e2.addFollower(p5);
//printing Ty's followers
e2.printFollowers();
cout << endl;
//demonstrating removing followers from both users
cout << "Removing follower Flynn from Sara" << endl;
e1.removeFollower("Flynn");
cout << "Removing follower Prince Charming from Sara" << endl;
e1.removeFollower("Prince Charming");
e1.printFollowers();
cout << endl;
cout << "Removing Jule's profile from Ty" << endl;
e2.removeFollower(p2);
e2.printFollowers();
cout << endl;
return 0;
}Adding followers to Sara
Followers for Sara:
Rapunzel
Flynn
Cinderella
Prince Charming
Adding follower profiles to Sara
Followers for Ty:
Rue
Jules
Fez
Maddie
Cassie
Removing follower Flynn from Sara
Removing follower Prince Charming from Sara
Followers for Sara:
Rapunzel
Cinderella
Cinderella
Removing Jule's profile from Ty
Followers for Ty:
Rue
Fez
Fez
Maddie--预期产出-预期产出
Adding followers to Sara
Followers for Sara:
Rapunzel
Flynn
Cinderella
Prince Charming
Adding follower profiles to Sara
Followers for Ty:
Rue
Jules
Fez
Maddie
Cassie
Removing follower Flynn from Sara
Removing follower Prince Charming from Sara
Followers for Sara:
Rapunzel
Cinderella
Removing Jule's profile from Ty
Followers for Ty:
Rue
Fez
Maddie
Cassie任何帮助、暗示或线索都是非常感谢的。(非常感谢:)
发布于 2022-02-21 20:04:59
我不太理解你在移除器函数中的逻辑,但我认为你在尝试做什么,或者沿着这些思路做些什么:找到跟随者去删除,将数组从索引后移到一个空格,减少追随者的数量?
我认为您可能会采取一种更简单的方法,只需用数组的最后一个元素交换找到的元素,并减少关注者的数量。
所以你的功能应该是这样的?
template <class T>
bool Twitter<T>::removeFollower(T follower) {
for(int i=0;i<numFollowers;i++){
if(followers[i] == follower){
T tmp = followers[i];
followers[i] = followers[numFollowers-1];
followers[numFollowers-1] = tmp;
--numFollowers;
return true;
}
}
return false;
}就个人而言,我不建议在这种类型的事情上使用数组。向量是删除和添加元素的一个更好的选择。
邮递员还没对这个做过测试,如果有打字的话.
发布于 2022-02-21 20:37:09
在函数removeFollower的内循环中
for(int j = i + 1; j < numFollowers; j++){
followers[j - 1] = followers[j];
numFollowers--;
return true;
}一旦一个元素被复制到数组的前一个元素中,函数就会退出。因此,如果不是数组的最后一个元素,相同的元素实际上是重复的。
可以声明该函数(我更改了函数参数的类型)并定义了以下方式
template <class T>
bool Twitter<T>::removeFollower( const T &follower )
{
int i = 0;
while ( not ( i == numFollowers ) && not ( followers[i] == follower ) ) i++;
bool success = i != numFollowers;
if ( success )
{
while ( ++i != numFollowers )
{
followers[i - 1] = followers[i];
}
--numFollowers;
}
return success;
}该功能保证在其执行后,追随者的顺序不会改变,这看起来很自然。
https://stackoverflow.com/questions/71212147
复制相似问题