【问题标题】:How to convert this vector code into a class?如何将此向量代码转换为类?
【发布时间】:2022-01-08 00:44:35
【问题描述】:

我试图将此代码放入一个类中,但我无法做到。该函数的工作是从 .txt 文件中提取团队名称并将它们放入向量中。我认为主要问题是我无法选择正确的函数返回类型。

这是teams.txt:(“-”符号前的名字是球队。其他名字与我的问题无关,但他们是球队的教练。)

Trabzonspor-Abdullah Avcı+
Fenerbahçe-Vítor Pereira+
Beşiktaş-Sergen Yalçın+
Galatasaray-Fatih Terim+
İstanbul Başakşehir-Emre Belözeoğlu+
Alanyaspor-Bülent Korkmaz+
Fatih Karagümrük-Francesco Farioli+
Gaziantep-Erol Bulut+
Adana Demirspor-Vincenzo Montella+
Ankara Dinc-Nestor El Maestro+
Antalyaspor-Nuri Şahin+
Kayserispor-Hikmet Karaman+
Yeni Malatyaspor-Marius Sumudica+
Konyaspor-İlhan Palut+
Sivasspor-Rıza Çalımbay+
Hatayspor-Ömer Erdoğan+
Giresunspor-Hakan Keleş+
Kasımpaşa-Hakan Kutlu+

这是我将它们放入向量中的代码:

#include <iostream>
#include <fstream>
#include <string>
#include <vector>
using namespace std; //I know that's a bad practice but i just need to do this for while

std::string process(std::string const& s) //A function to seperate teams from the coaches
{
    string::size_type pos = s.find('-');
    if (pos != string::npos)
    {
        return s.substr(0, pos);
    }
    else
    {
        return s;
    }
}


int main() {

    ifstream readTeam("teams.txt");
    if (!readTeam) {  //checking that successfully opened the file.
        std::cerr << "Error while opening the file.\n";
        return 1;
    }
    vector<std::string> teams;
    string team;
    while (getline(readTeam, team)) {
        teams.push_back(process(team));
    }
    readTeam.close();

    int g = 1;//for printing the teams, just for displaying it. doesn't have to in a class.
    for (const auto& i : teams) {
        cout << g;
        cout << i << endl;
        g++;
    }

    return 0;
}

这就是我所做的(试图)使它成为一个类:

#include <iostream>
#include <fstream>
#include <string>
#include <vector>

using namespace std;

std::string process(std::string const& s)
{
    string::size_type pos = s.find('-');
    if (pos != string::npos)
    {
        return s.substr(0, pos);
    }
    else
    {
        return s;
    }
}


class readFile {
public:
    void setTxtName(string);
    vector<unsigned char> const& getTeam() const{
    }
    vector<string> teams;

private:
    string fileName;
    
};

int main() {
    
    readFile pullTeams;

    pullTeams.setTxtName("teams.txt");
    

    return 0;
}

void readFile::setTxtName(string txtName) {
    fileName = txtName;
}

 
vector<string> const& readFile::getTeam { //problem is defining it(I think). So I couldn't add my main code int it..
    
    return teams;
}

有什么帮助,谢谢!

【问题讨论】:

  • 请查阅有关构造函数的 C++ 课程材料。打开文件和 slurping 内容的逻辑位置是在 readFile 的构造函数中,这样 getTeams 可以只返回它的 teams 成员。

标签: c++ class vector fstream txt


【解决方案1】:

我根据 Botje 的 comment 做了一些不同的研究。我设法根据here. 谢谢。

#include <iostream>
#include <fstream>
#include <string>
#include <vector>
using namespace std;


std::string process(std::string const& s){
    string::size_type pos = s.find('-');
    if (pos != string::npos){
        return s.substr(0, pos);
    }
    else{
        return s;
    }
}


class readFile {
public:
    vector<string> getTxt();
    bool read(string);

private:
    vector<string> teams;
    string team;
    ifstream txt;
};

int main() {
    
    vector<string> teams;

    readFile team;
    if (team.read("teaams.txt") == true)
        teams = team.getTxt();

    int g = 1;//for printing the teams, just for displaying it. doesn't have to in a class.
    for (const auto& i : teams) {
        cout << g;
        cout << i << endl;
        g++;
    }
    

    return 0;
}


bool readFile::read(string txtName) {
    ifstream txt;
    string team;

    txt.open(txtName.c_str());

    if (!txt.is_open())
        return false;
    while (getline(txt, team))
        teams.push_back(process(team));
    return true;
 }

vector<string> readFile::getTxt() {
    return teams;
}

【讨论】:

    猜你喜欢
    • 2021-08-28
    • 2010-10-09
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-11-19
    • 2019-05-26
    • 2018-06-24
    相关资源
    最近更新 更多