【问题标题】:How to get random string in txt file (c++ ifstream)?如何在txt文件(c ++ ifstream)中获取随机字符串?
【发布时间】:2021-02-23 10:34:56
【问题描述】:

例如,我有带有单词列表的 txt db(在屏幕上),我需要在 ifstream C++ 中的这个文件中随机输出 3 行。我在for循环中尝试过这个,但它非常消耗

#include "gamewindow.h"
#include "ui_gamewindow.h"
#include <iostream>
#include <fstream>
#include <string>
#include <QLabel>
#include <ctime>
using namespace std;
string login;
bool start_is_clicked=false;
GameWindow::GameWindow(QWidget *parent) :
    QMainWindow(parent),
    ui(new Ui::GameWindow)
{
    ui->setupUi(this);
    ifstream logined("last_login.txt");
    getline(logined,login);
    QString qstring_login = QString::fromLocal8Bit(login.c_str());
    ui->login_label->setText(qstring_login);
    logined.close();
}

GameWindow::~GameWindow()
{
    delete ui;
}
int countStringsWords(){
    string current;
    int count = 0;
    ifstream words("word_db.txt");
    do{
        getline(words,current);
        count++;
    }while(!words.eof());
    words.close();
    return count;
}
void getLabelText(){
    srand(time(NULL));
    ifstream words("word_db.txt");
    int start=rand()%countStringsWords();
    words.close();
    cout<<start<<endl;
}
void GameWindow::on_Start_stop_clicked()
{
    if(ui->Start_stop->text()=="START"){
        ui->Start_stop->setText("STOP");
        getLabelText();
    }
    else if(ui->Start_stop->text()=="STOP"){
        ui->Start_stop->setText("START");
    }
    else{
        cout<<"Button error"<<endl;
    }
}

工作区:

int countStringsWords(){
    string current;
    int count = 0;
    ifstream words("word_db.txt");
    do{
        getline(words,current);
        count++;
    }while(!words.eof());
    words.close();
    return count;
}
void getLabelText(){
    srand(time(NULL));
    ifstream words("word_db.txt");
    int start=rand()%countStringsWords();
    words.close();
}

【问题讨论】:

  • 请出示您已经编写的代码。
  • 刚刚添加,检查。
  • 您能添加一个工作示例吗? (尽可能少的包含)
  • 刚刚也添加了
  • @Bogdan 我相信 Riccardo 要求的是一个可以工作的、可移植的示例,我们可以将其放入我们自己的环境中进行调试/测试。

标签: c++ qt file fstream txt


【解决方案1】:

升级您当前的解决方案:

您可以做的是使用动态列表(向量)用文件填充向量,然后生成 3 个随机索引并返回这三个随机字符串的数组。如果文本文件的长度为 n,这将是 O(n) 的空间和时间复杂度。

有更好的解决方案吗?

如果您想要 O(1) 时间和空间复杂度(不考虑您设备上的文件存储),那么我建议您将文件设为 XML 文件,因为这将允许您在本机的任何索引处访问该文件,这意味着您可以不需要将整个文件加载到数组中,对于较大的文件不建议这样做。

int * ReturnRandomWords(){
    string current;
    vector<std::string> WordsFromFile
    ifstream words("word_db.txt");
    do{
        getline(words,current);
        WordsFromFile.push(current);
    }while(!words.eof());
    words.close();
    string v1 = WordsFromFile.at( rand() % WordsFromFile.size());  
    string v2 = WordsFromFile.at( rand() % WordsFromFile.size());   
    string v3 = WordsFromFile.at( rand() % WordsFromFile.size()));
    string arr [3] = {v1, v2, v3};
    return arr;
}

【讨论】:

  • 谢谢,但我只是没有在变量 ifstream 单词中输入正确的文件名 xD
【解决方案2】:

添加到 ZanyCactus 的答案中。

如果您需要每个答案都是唯一的并且可以使用 C++17,您可以使用std::sample

std::vector<std::string> random_words;
constexpr size_t number_of_random_words = 3;
std::sample(
    WordsFromFile.begin(),
    WordsFromFile.end(),
    std::back_inserter(random_words),
    number_of_random_words,
    std::default_random_engine(std::random_device()())
);

【讨论】:

  • 谢谢,但我只是没有在变量 ifstream 单词中输入正确的文件名 xD
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2011-08-19
  • 2019-06-03
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多