如何使用获取用户索引和 returns his/her 名字的访问器方法。

How to use an accessor method that takes the user index and returns his/her first name.

3.) string getFirstName(int ind) const:获取用户索引和 returns his/her 名字的访问器方法。

这是我正在为我的程序解决的问题,正如上面所说,我应该使用一种访问器方法,该方法采用用户索引和 returns 他/她的名字???

到目前为止,这是我的代码,但我很困惑如何使用访问器方法

string RatingDB::getFirstName(int ind) const
{class getFirstName;

{
   public:


   return "";
}
}

目前为止我的程序代码供参考

// TODO: implement all these method stubs!
void RatingDB::loadFile(string filename)
{
 ifstream OpenFile;
    OpenFile.open (filename);

    // if this file does not exist then end the program
    if (!OpenFile.is_open())
    {
        cout << "can't open this file!" << endl;
        exit(EXIT_FAILURE);
    }

    int i = 0;


    char c;
    while(! OpenFile.eof())
    {

        do {
            c=OpenFile.get();
            if(c!= ' ')
                m_firstNames[i].push_back(c);
        } while(c!= ' ');

        do { 
                c=OpenFile.get();
            if(c!= ' ')
                m_lastNames[i].push_back(c);
        }while(c!= ' ');

        do { 
                c=OpenFile.get();
            if(c!= '/n' && OpenFile.eof())
                m_Ratings[i].push_back(c);

        } while(c!= '/n' && OpenFile.eof());
            i++;
    }   










}

string RatingDB::getFirstName(int ind) const
{class getFirstName;

{
    public:


    return "";
}
}
string RatingDB::getLastName(int ind) const
{
    return "";
}

vector<int> RatingDB::getRatingVector(int ind) const
{   
    return vector<int>();
}

int RatingDB::getNumberOfUsers() const
{
    return -1;
}

void RatingDB::insertRating(string firstName, string lastName, string strRatings)
{

}

void RatingDB::displayRatings() const
{

}

int RatingDB::compareRatings(const vector<int>& RatingVector1, const vector<int>& RatingVector2) const
{
    return -1;
}

void RatingDB::findSimUsers(const vector<int>& ratingVector, vector<int>& friendVector, vector<int>& scoreVector)
{

}

vector<int> RatingDB::parseVectorString(string strRatings)
{
    return vector<int>(1);
}

//************************************************************************************************************************
void RatingDB::findTenBestMatches(const vector<int>& RatingVector, vector<int>& returnVector)
{
    vector<int> sortVector(getNumberOfUsers());  //create a vector of integers representing each index in your vector of structures
    bool swapped;  //a boolean to indicate if any swaps have been made during this pass of the loop.

    for(int i = 0; i < getNumberOfUsers(); i++)  //initialize the array so that we have an integer indicating the location in 
        sortVector[i] = i;                       //your structure vector for every user that is read in.

    do{ //keep doing the following if we swap at least once during the pass
        swapped = false;                                    //set the swapped variable for this pass
        for(int i = 0; i < getNumberOfUsers() - 1; i++)     //we are going to look at each element in the vector and see if it has a ranking lower
        {                                                   //than the next element.  If so, the two need to be swapped.
            if(compareRatings(RatingVector, getRatingVector(sortVector[i])) < compareRatings(RatingVector, getRatingVector(sortVector[i + 1])))
            { //we compare the two rankings and swap the two in the array if the first is lower than the second  
              //(we want the best rankings first)
                int temp;
                temp = sortVector[i];   //swap this one and the next one
                sortVector[i] = sortVector[i + 1];
                sortVector[i + 1] = temp;
                swapped = true;         //if we moved anything, then another pass is necessary
            }   
        }
    }while(swapped);

    for(int i = 0; i < 10; i++)     //copy the top ten indexes to the returnVector
        if(i < getNumberOfUsers())  //if there are at least 10 users
            returnVector.push_back(sortVector[i]);  //copy the index
}

你想改变你的观点。您应该将名字和姓氏放入一个结构中,并使用该结构的向量,而不是使用一个数组来存储名字和另一个数组来存储姓氏。

class Person
{
  std::string m_first_name;
  std::string m_last_name;
  public:
    Person(const std::string& first_name,
           const std::string& last_name)
    : m_first_name(first_name),
      m_last_name(last_name)
    { }
};

typedef std::vector<Person> Person_Container;

int main(void)
{
  Person jack("Jack", "Frost");
  Person alton("Alton", "Brown");
  Person bobby("Bobby", "Flay");

  Person_Container people;
  people.push_back(jack);
  people.push_back(alton);
  people.push_back(bobby);

  return EXIT_SUCCESS;
}

编辑 1:输入一个人
您可以通过添加输入法来扩充 Person class。

class Person
{
  std::string m_first_name;
  std::string m_last_name;
  public:
    Person(const std::string& first_name = "",
           const std::string& last_name = "")
    : m_first_name(first_name),
      m_last_name(last_name)
    { }

    void Input_From_User(std::istream&  inp,
                         std::ostream&  out);
};

void
Person ::
Input_From_User(std::istream& inp,  std::ostream& out)
{
  out << "Input first name:\n";
  std::getline(inp, first_name);
  out << "\nInput last name:\n";
  std::getline(inp, last_name);
}

int main(void)
{
  Person somebody;
  somebody.Input_From_User(cin, cout);
  std::vector<Person> people;
  people.push_back(somebody);

  return EXIT_SUCCESS;
}

同样,您也可以增加 Person class 输出。
那留作 reader 的练习。 :-)